Class: Coinbase::User
- Inherits:
-
Object
- Object
- Coinbase::User
- Defined in:
- lib/coinbase/user.rb
Overview
A representation of a User. Users have Wallets, which can hold balances of Assets. Access the default User through Coinbase#default_user.
Instance Method Summary collapse
-
#create_wallet ⇒ Coinbase::Wallet
Creates a new Wallet belonging to the User.
-
#import_wallet(data) ⇒ Coinbase::Wallet
Imports a Wallet belonging to the User.
-
#initialize(model) ⇒ User
constructor
Returns a new User object.
-
#inspect ⇒ String
Same as to_s.
-
#list_wallet_ids ⇒ Array<String>
Lists the IDs of the Wallets belonging to the User.
-
#load_wallets ⇒ Map<String>Coinbase::Wallet
Loads all wallets belonging to the User with backup persisted to the local file system.
-
#save_wallet(wallet, encrypt: false) ⇒ Coinbase::Wallet
Saves a wallet to local file system.
-
#to_s ⇒ String
Returns a string representation of the User.
-
#user_id ⇒ String
Returns the User ID.
Constructor Details
#initialize(model) ⇒ User
Returns a new User object. Do not use this method directly. Instead, use Coinbase#default_user.
12 13 14 |
# File 'lib/coinbase/user.rb', line 12 def initialize(model) @model = model end |
Instance Method Details
#create_wallet ⇒ Coinbase::Wallet
Creates a new Wallet belonging to the User.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/coinbase/user.rb', line 24 def create_wallet create_wallet_request = { wallet: { # TODO: Don't hardcode this. network_id: 'base-sepolia' } } opts = { create_wallet_request: create_wallet_request } model = Coinbase.call_api do wallets_api.create_wallet(opts) end Wallet.new(model) end |
#import_wallet(data) ⇒ Coinbase::Wallet
Imports a Wallet belonging to the User.
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/coinbase/user.rb', line 43 def import_wallet(data) model = Coinbase.call_api do wallets_api.get_wallet(data.wallet_id) end address_count = Coinbase.call_api do addresses_api.list_addresses(model.id).total_count end Wallet.new(model, seed: data.seed, address_count: address_count) end |
#inspect ⇒ String
Same as to_s.
149 150 151 |
# File 'lib/coinbase/user.rb', line 149 def inspect to_s end |
#list_wallet_ids ⇒ Array<String>
Lists the IDs of the Wallets belonging to the User.
57 58 59 60 61 62 63 |
# File 'lib/coinbase/user.rb', line 57 def list_wallet_ids wallets = Coinbase.call_api do wallets_api.list_wallets end wallets.data.map(&:id) end |
#load_wallets ⇒ Map<String>Coinbase::Wallet
Loads all wallets belonging to the User with backup persisted to the local file system.
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/coinbase/user.rb', line 110 def load_wallets existing_seeds_in_store = existing_seeds raise ArgumentError, 'Backup file not found' if existing_seeds_in_store == {} wallets = {} existing_seeds_in_store.each do |wallet_id, seed_data| seed = seed_data['seed'] raise ArgumentError, 'Malformed backup data' if seed.nil? || seed == '' if seed_data['encrypted'] shared_secret = store_encryption_key raise ArgumentError, 'Malformed encrypted seed data' if seed_data['iv'] == '' || seed_data['auth_tag'] == '' cipher = OpenSSL::Cipher.new('aes-256-gcm').decrypt cipher.key = OpenSSL::Digest.digest('SHA256', shared_secret) iv = [seed_data['iv']].pack('H*') cipher.iv = iv auth_tag = [seed_data['auth_tag']].pack('H*') cipher.auth_tag = auth_tag cipher.auth_data = '' hex_decoded_data = [seed_data['seed']].pack('H*') seed = cipher.update(hex_decoded_data) + cipher.final end data = Coinbase::Wallet::Data.new(wallet_id: wallet_id, seed: seed) wallets[wallet_id] = import_wallet(data) end wallets end |
#save_wallet(wallet, encrypt: false) ⇒ Coinbase::Wallet
Saves a wallet to local file system. Wallet saved this way can be re-instantiated with ‘load_wallets` function, provided the backup_file is available. This is an insecure method of storing wallet seeds and should only be used for development purposes. If you call save_wallet twice with wallets containing the same wallet_id, the backup will be overwritten during the second attempt. The default backup_file is `seeds.json` in the root folder. It can be configured by changing Coinbase.configuration.backup_file_path.
encrypted or not. Data is unencrypted by default.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/coinbase/user.rb', line 76 def save_wallet(wallet, encrypt: false) existing_seeds_in_store = existing_seeds data = wallet.export seed_to_store = data.seed auth_tag = '' iv = '' if encrypt shared_secret = store_encryption_key cipher = OpenSSL::Cipher.new('aes-256-gcm').encrypt cipher.key = OpenSSL::Digest.digest('SHA256', shared_secret) iv = cipher.random_iv cipher.iv = iv cipher.auth_data = '' encrypted_data = cipher.update(data.seed) + cipher.final auth_tag = cipher.auth_tag.unpack1('H*') iv = iv.unpack1('H*') seed_to_store = encrypted_data.unpack1('H*') end existing_seeds_in_store[data.wallet_id] = { seed: seed_to_store, encrypted: encrypt, auth_tag: auth_tag, iv: iv } File.open(Coinbase.configuration.backup_file_path, 'w') do |file| file.write(JSON.pretty_generate(existing_seeds_in_store)) end wallet end |
#to_s ⇒ String
Returns a string representation of the User.
143 144 145 |
# File 'lib/coinbase/user.rb', line 143 def to_s "Coinbase::User{user_id: '#{user_id}'}" end |
#user_id ⇒ String
Returns the User ID.
18 19 20 |
# File 'lib/coinbase/user.rb', line 18 def user_id @model.id end |