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.
-
#id ⇒ String
Returns the User ID.
-
#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.
-
#load_wallets_from_local ⇒ Map<String>Coinbase::Wallet
Loads all wallets belonging to the User with backup persisted to the local file system.
-
#save_wallet_locally!(wallet, encrypt: false) ⇒ Coinbase::Wallet
Saves a wallet to local file system.
-
#to_s ⇒ String
Returns a string representation of the User.
-
#wallet_ids ⇒ Array<String>
Lists the IDs of the Wallets belonging to the User.
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 |
#id ⇒ String
Returns the User ID.
18 19 20 |
# File 'lib/coinbase/user.rb', line 18 def id @model.id end |
#import_wallet(data) ⇒ Coinbase::Wallet
Imports a Wallet belonging to the User.
43 44 45 |
# File 'lib/coinbase/user.rb', line 43 def import_wallet(data) Wallet.import(data) end |
#inspect ⇒ String
Same as to_s.
141 142 143 |
# File 'lib/coinbase/user.rb', line 141 def inspect to_s end |
#load_wallets_from_local ⇒ Map<String>Coinbase::Wallet
Loads all wallets belonging to the User with backup persisted to the local file system.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/coinbase/user.rb', line 102 def load_wallets_from_local 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_locally!(wallet, encrypt: false) ⇒ Coinbase::Wallet
Saves a wallet to local file system. Wallet saved this way can be re-instantiated with load_wallets_from_local 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_locally! 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.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/coinbase/user.rb', line 68 def save_wallet_locally!(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.
135 136 137 |
# File 'lib/coinbase/user.rb', line 135 def to_s "Coinbase::User{user_id: '#{id}'}" end |
#wallet_ids ⇒ Array<String>
Lists the IDs of the Wallets belonging to the User.
49 50 51 52 53 54 55 |
# File 'lib/coinbase/user.rb', line 49 def wallet_ids wallets = Coinbase.call_api do wallets_api.list_wallets end wallets.data.map(&:id) end |