Class: Coinbase::Wallet

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/coinbase/wallet.rb,
lib/coinbase/wallet/data.rb

Overview

A representation of a Wallet. Wallets come with a single default Address, but can expand to have a set of Addresses, each of which can hold a balance of one or more Assets. Wallets can create new Addresses, list their addresses, list their balances, and transfer Assets to other Addresses.

Defined Under Namespace

Classes: Data

Constant Summary collapse

MAX_ADDRESSES =

The maximum number of addresses in a Wallet.

20

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, seed: nil) ⇒ Wallet

Returns a new Wallet object. Do not use this method directly. Instead use Coinbase::Wallet.create.

Parameters:

  • model (Coinbase::Client::Wallet)

    The underlying Wallet object

  • seed (String) (defaults to: nil)

    (Optional) The seed to use for the Wallet. Expects a 32-byte hexadecimal with no 0x prefix. If nil, a new seed will be generated. If the empty string, no seed is generated, and the Wallet will be instantiated without a seed and its corresponding private keys. with the Wallet. If not provided, the Wallet will derive the first default address.

Raises:

  • (ArgumentError)


135
136
137
138
139
140
141
142
143
# File 'lib/coinbase/wallet.rb', line 135

def initialize(model, seed: nil)
  raise ArgumentError, 'model must be a Wallet' unless model.is_a?(Coinbase::Client::Wallet)

  @model = model

  return if Coinbase.use_server_signer?

  @master = master_node(seed)
end

Class Method Details

.create(network: Coinbase.default_network, interval_seconds: 0.2, timeout_seconds: 20) ⇒ Coinbase::Wallet

Creates a new Wallet on the specified Network and generate a default address for it. have an active seed, if using a ServerSigner, in seconds create a seed for the Wallet, in seconds

Parameters:

  • network (Coinbase::Network, Symbol) (defaults to: Coinbase.default_network)

    (Optional) The network object or ID to create the Wallet on. When omitted this uses the SDK configured default network.

  • interval_seconds (Integer) (defaults to: 0.2)

    The interval at which to poll the CDPService for the Wallet to

  • timeout_seconds (Integer) (defaults to: 20)

    The maximum amount of time to wait for the ServerSigner to

Returns:



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/coinbase/wallet.rb', line 66

def create(network: Coinbase.default_network, interval_seconds: 0.2, timeout_seconds: 20)
  model = Coinbase.call_api do
    wallets_api.create_wallet(
      create_wallet_request: {
        wallet: {
          network_id: Coinbase.normalize_network(network),
          use_server_signer: Coinbase.use_server_signer?
        }
      }
    )
  end

  wallet = new(model)

  # When used with a ServerSigner, the Signer must first register
  # with the Wallet before addresses can be created.
  wait_for_signer(wallet.id, interval_seconds, timeout_seconds) if Coinbase.use_server_signer?

  wallet.create_address
  wallet
end

.fetch(wallet_id) ⇒ Coinbase::Wallet

Fetches a Wallet by its ID. The returned wallet can be immediately used for signing operations if backed by a server signer. If the wallet is not backed by a server signer, the wallet’s seed will need to be set before it can be used for signing operations.

Parameters:

  • wallet_id (String)

    The ID of the Wallet to fetch

Returns:



50
51
52
53
54
55
56
# File 'lib/coinbase/wallet.rb', line 50

def fetch(wallet_id)
  model = Coinbase.call_api do
    wallets_api.get_wallet(wallet_id)
  end

  new(model, seed: '')
end

.import(data) ⇒ Coinbase::Wallet

Imports a Wallet from previously exported wallet data.

Parameters:

Returns:

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
# File 'lib/coinbase/wallet.rb', line 24

def import(data)
  raise ArgumentError, 'data must be a Coinbase::Wallet::Data object' unless data.is_a?(Data)

  model = Coinbase.call_api do
    wallets_api.get_wallet(data.wallet_id)
  end

  new(model, seed: data.seed)
end

.listEnumerable<Coinbase::Wallet>

Enumerates the wallets for the requesting user. The result is an enumerator that lazily fetches from the server, and can be iterated over, converted to an array, etc…

Returns:



38
39
40
41
42
# File 'lib/coinbase/wallet.rb', line 38

def list
  Coinbase::Pagination.enumerate(method(:fetch_wallets_page)) do |wallet|
    Coinbase::Wallet.new(wallet, seed: '')
  end
end

Instance Method Details

#address(address_id) ⇒ Address

Returns the Address with the given ID.

Parameters:

  • address_id (String)

    The ID of the Address to retrieve

Returns:



339
340
341
# File 'lib/coinbase/wallet.rb', line 339

def address(address_id)
  addresses.find { |address| address.id == address_id }
end

#addressesArray<Coinbase::WalletAddress>

Returns the addresses belonging to the Wallet.

Returns:



244
245
246
247
248
# File 'lib/coinbase/wallet.rb', line 244

def addresses
  return @addresses unless @addresses.nil?

  set_addresses
end

#balance(asset_id) ⇒ BigDecimal

Returns the balance of the provided Asset. Balances are aggregated across all Addresses in the Wallet.

Parameters:

  • asset_id (Symbol)

    The ID of the Asset to retrieve the balance for

Returns:

  • (BigDecimal)

    The balance of the Asset



356
357
358
359
360
361
362
363
364
# File 'lib/coinbase/wallet.rb', line 356

def balance(asset_id)
  response = Coinbase.call_api do
    wallets_api.get_wallet_balance(id, Coinbase::Asset.primary_denomination(asset_id).to_s)
  end

  return BigDecimal('0') if response.nil?

  Coinbase::Balance.from_model_and_asset_id(response, asset_id).amount
end

#balancesBalanceMap

Returns the list of balances of this Wallet. Balances are aggregated across all Addresses in the Wallet.

Returns:

  • (BalanceMap)

    The list of balances. The key is the Asset ID, and the value is the balance.



345
346
347
348
349
350
351
# File 'lib/coinbase/wallet.rb', line 345

def balances
  response = Coinbase.call_api do
    wallets_api.list_wallet_balances(id)
  end

  Coinbase::BalanceMap.from_balances(response.data)
end

#can_sign?Boolean

Returns whether the Wallet has a seed with which to derive keys and sign transactions.

Returns:

  • (Boolean)

    Whether the Wallet has a seed with which to derive keys and sign transactions.



379
380
381
# File 'lib/coinbase/wallet.rb', line 379

def can_sign?
  !@master.nil?
end

#claim_stakeCoinbase::StakingOperation

Claims stake of the given amount of the given Asset for the default address.

Parameters:

  • amount (Integer, Float, BigDecimal)

    The amount of the Asset to claim_stake.

  • asset_id (Symbol)

    The ID of the Asset to claim_stake.

  • mode (Symbol)

    (Optional) The staking mode. Defaults to :default.

  • options (Hash)

    (Optional) Additional options for the unstaking operation.

Returns:



# File 'lib/coinbase/wallet.rb', line 188

#claimable_balanceBigDecimal

Retrieves the claimable balance for the supplied asset. Currently only the default_address is used to source the claimable balance.

Parameters:

  • asset_id (Symbol)

    The asset to retrieve the claimable balance for

  • mode (Symbol)

    The staking mode. Defaults to :default.

  • options (Hash)

    Additional options for the staking operation

Returns:

  • (BigDecimal)

    The claimable balance



# File 'lib/coinbase/wallet.rb', line 221

#create_addressAddress

Creates a new Address in the Wallet.

Returns:



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/coinbase/wallet.rb', line 292

def create_address
  req = {}

  # Ensure that the address cache is set before creating a new address.
  # This ensures that for a server signer, the addresses have been loaded and we
  # can create a new address and add it to a cache.
  set_addresses if @addresses.nil?

  unless Coinbase.use_server_signer?
    # The index for the next address is the number of addresses already registered.
    private_key_index = addresses.count

    key = derive_key(private_key_index)

    req = {
      public_key: key.public_key.compressed.unpack1('H*'),
      attestation: create_attestation(key),
      address_index: private_key_index
    }
  end

  address_model = Coinbase.call_api do
    addresses_api.create_address(id, { create_address_request: req })
  end

  # Default address can be nil because either this is the first address being
  # created for this wallet or the addresses cache has not yet been loaded.

  # If the default address is nil, we must reload the wallet model after creating
  # the address, in order for the default address to be set.
  reload if default_address.nil?

  # The addreses cache is already created, so we can add the new address to the cache.
  address = WalletAddress.new(address_model, key)
  @addresses << address
  address
end

#default_addressAddress

Returns the default address of the Wallet.

Returns:

  • (Address)

    The default address



332
333
334
# File 'lib/coinbase/wallet.rb', line 332

def default_address
  address(@model.default_address&.address_id)
end

#exportCoinbase::Wallet::Data

Exports the Wallet’s data to a Data object.

Returns:



368
369
370
371
372
373
374
375
# File 'lib/coinbase/wallet.rb', line 368

def export
  # TODO: Improve this check by relying on the backend data to decide whether a wallet is server-signer backed.
  raise 'Cannot export data for Server-Signer backed Wallet' if Coinbase.use_server_signer?

  raise 'Cannot export Wallet without loaded seed' if @master.nil?

  Data.new(wallet_id: id, seed: @master.seed_hex)
end

#faucetCoinbase::FaucetTransaction

Requests funds from the faucet for the Wallet’s default address and returns the faucet transaction. This is only supported on testnet networks.

Parameters:

  • asset_id (Symbol)

    The ID of the Asset to transfer to the wallet.

Returns:

Raises:



# File 'lib/coinbase/wallet.rb', line 164

#idString

Returns the Wallet ID.

Returns:

  • (String)

    The Wallet ID



252
253
254
# File 'lib/coinbase/wallet.rb', line 252

def id
  @model.id
end

#inspectString

Same as to_s.

Returns:

  • (String)

    a String representation of the Wallet



475
476
477
# File 'lib/coinbase/wallet.rb', line 475

def inspect
  to_s
end

#invoke_contractObject

Invokes a contract with the given ABI, method, and arguments.

Parameters:

  • abi (Array<Hash>)

    The ABI of the contract



239
240
# File 'lib/coinbase/wallet.rb', line 239

def_delegators :default_address, :transfer, :trade, :faucet, :stake, :unstake, :claim_stake, :staking_balances,
:stakeable_balance, :unstakeable_balance, :claimable_balance, :sign_payload, :invoke_contract

#load_seed(file_path) ⇒ String

Loads the seed of the Wallet from the given file.

Parameters:

  • file_path (String)

    The path of the file to load the seed from

Returns:

  • (String)

    A string indicating the success of the operation

Raises:

  • (ArgumentError)


426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# File 'lib/coinbase/wallet.rb', line 426

def load_seed(file_path)
  raise 'Wallet already has seed loaded' unless @master.nil?

  existing_seeds_in_store = existing_seeds(file_path)

  raise ArgumentError, "File #{file_path} does not contain seed data" if existing_seeds_in_store == {}

  if existing_seeds_in_store[id].nil?
    raise ArgumentError, "File #{file_path} does not contain seed data for wallet #{id}"
  end

  seed_data = existing_seeds_in_store[id]
  local_seed = seed_data['seed']

  raise ArgumentError, 'Seed data is malformed' if local_seed.nil? || local_seed == ''

  if seed_data['encrypted']
    raise ArgumentError, 'Encrypted seed data is malformed' if seed_data['iv'] == '' ||
                                                               seed_data['auth_tag'] == ''

    cipher = OpenSSL::Cipher.new('aes-256-gcm').decrypt
    cipher.key = OpenSSL::Digest.digest('SHA256', encryption_key)
    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*')
    local_seed = cipher.update(hex_decoded_data) + cipher.final
  end

  self.seed = local_seed

  "Successfully loaded seed for wallet #{id} from #{file_path}."
end

#networkCoinbase::Network

Returns the Network of the Wallet.

Returns:



258
259
260
# File 'lib/coinbase/wallet.rb', line 258

def network
  @network ||= Coinbase::Network.from_id(@model.network_id)
end

#save_seed!(file_path, encrypt: false) ⇒ String

Saves the seed of the Wallet to the given file. Wallets whose seeds are saved this way can be rehydrated using load_seed. A single file can be used for multiple Wallet seeds. This is an insecure method of storing Wallet seeds and should only be used for development purposes.

encrypted or not. Data is unencrypted by default.

Parameters:

  • file_path (String)

    The path of the file to save the seed to

  • encrypt (bool) (defaults to: false)

    (Optional) Whether the seed information persisted to the local file system should be

Returns:

  • (String)

    A string indicating the success of the operation



391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'lib/coinbase/wallet.rb', line 391

def save_seed!(file_path, encrypt: false)
  raise 'Wallet does not have seed loaded' if @master.nil?

  existing_seeds_in_store = existing_seeds(file_path)

  seed_to_store = @master.seed_hex
  auth_tag = ''
  iv = ''
  if encrypt
    cipher = OpenSSL::Cipher.new('aes-256-gcm').encrypt
    cipher.key = OpenSSL::Digest.digest('SHA256', encryption_key)
    iv = cipher.random_iv
    cipher.iv = iv
    cipher.auth_data = ''
    encrypted_data = cipher.update(@master.seed_hex) + 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[id] = {
    seed: seed_to_store,
    encrypted: encrypt,
    auth_tag: auth_tag,
    iv: iv
  }

  File.write(file_path, JSON.pretty_generate(existing_seeds_in_store))

  "Successfully saved seed for wallet #{id} to #{file_path}."
end

#seed=(seed) ⇒ Object

Sets the seed of the Wallet. This seed is used to derive keys and sign transactions.

Parameters:

  • seed (String)

    The seed to set. Expects a 32-byte hexadecimal with no 0x prefix.

Raises:

  • (ArgumentError)


270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/coinbase/wallet.rb', line 270

def seed=(seed)
  raise ArgumentError, 'Seed must not be empty' if seed.nil? || seed.empty?
  raise StandardError, 'Seed is already set' unless @master.nil?

  @master = master_node(seed)

  # If the addresses are not loaded the keys will be set on them whenever they are loaded.
  return if @addresses.nil?

  # If addresses are already loaded, set the keys on each address.
  addresses.each_with_index.each do |address, index|
    key = derive_key(index)

    # If we derive a key the derived address must match the address from the API.
    raise StandardError, 'Seed does not match wallet' unless address.id == key.address.to_s

    address.key = key
  end
end

#server_signer_statusSymbol

Returns the ServerSigner Status of the Wallet.

Returns:

  • (Symbol)

    The ServerSigner Status



264
265
266
# File 'lib/coinbase/wallet.rb', line 264

def server_signer_status
  Coinbase.to_sym(@model.server_signer_status)
end

#sign_payloadCoinbase::PayloadSignature

Signs the given unsigned payload.

Parameters:

  • unsigned_payload (String)

    The hex-encoded hashed unsigned payload for the Address to sign.

Returns:



# File 'lib/coinbase/wallet.rb', line 229

#stakeCoinbase::StakingOperation

Stakes the given amount of the given Asset for the default address.

Parameters:

  • amount (Integer, Float, BigDecimal)

    The amount of the Asset to stake.

  • asset_id (Symbol)

    The ID of the Asset to stake.

  • mode (Symbol)

    (Optional) The staking mode. Defaults to :default.

  • options (Hash)

    (Optional) Additional options for the staking operation.

Returns:



# File 'lib/coinbase/wallet.rb', line 172

#stakeable_balanceBigDecimal

Retrieves the stakeable balance of the supplied asset for the default address.

Parameters:

  • asset_id (Symbol)

    The asset to retrieve the stakeable balance for

  • mode (Symbol)

    The staking mode. Defaults to :default.

  • options (Hash)

    Additional options for the staking operation

Returns:

  • (BigDecimal)

    The stakeable balance



# File 'lib/coinbase/wallet.rb', line 206

#staking_balancesHash, BigDecimal

Retrieves the balances used for staking for the supplied asset for the default address.

Parameters:

  • asset_id (Symbol)

    The asset to retrieve staking balances for

  • mode (Symbol)

    The staking mode. Defaults to :default.

  • options (Hash)

    Additional options for the staking operation

Returns:

  • (Hash)

    The staking balances

  • (BigDecimal)

    :stakeable_balance The amount of the asset that can be staked

  • (BigDecimal)

    :unstakeable_balance The amount of the asset that is currently staked and cannot be unstaked

  • (BigDecimal)

    :claimable_balance The amount of the asset that can be claimed



# File 'lib/coinbase/wallet.rb', line 196

#to_sString

Returns a String representation of the Wallet.

Returns:

  • (String)

    a String representation of the Wallet



464
465
466
467
468
469
470
471
# File 'lib/coinbase/wallet.rb', line 464

def to_s
  Coinbase.pretty_print_object(
    self.class,
    id: id,
    network_id: network.id,
    default_address: @model.default_address&.address_id
  )
end

#tradeCoinbase::Trade

Trades the specified amount from one asset to another using the default address.

Parameters:

  • amount (Integer, Float, BigDecimal)

    The amount of the Asset to send.

  • from_asset_id (Symbol)

    The ID of the Asset to trade from.

  • to_asset_id (Symbol)

    The ID of the Asset to trade to. default address. If a String, interprets it as the address ID.

Returns:



# File 'lib/coinbase/wallet.rb', line 156

#transferCoinbase::Transfer

Transfers the amount of the Asset from the default address to the specified destination. (see Coinbase::Address::WalletAddress#transfer)

Parameters:

  • amount (Integer, Float, BigDecimal)

    The amount of the Asset to send

  • asset_id (Symbol)

    The ID of the Asset to send

  • destination (Wallet | Address | String)

    The destination of the transfer. If a Wallet, sends to the Wallet’s default address. If a String, interprets it as the address ID.

  • gasless (Boolean)

    Whether the transfer should be gasless. Defaults to false.

Returns:



# File 'lib/coinbase/wallet.rb', line 145

#unstakeCoinbase::StakingOperation

Unstakes the given amount of the given Asset on the default address.

Parameters:

  • amount (Integer, Float, BigDecimal)

    The amount of the Asset to unstake.

  • asset_id (Symbol)

    The ID of the Asset to unstake.

  • mode (Symbol)

    (Optional) The staking mode. Defaults to :default.

  • options (Hash)

    (Optional) Additional options for the unstaking operation.

Returns:



# File 'lib/coinbase/wallet.rb', line 180

#unstakeable_balanceBigDecimal

Retrieves the unstakeable balance for the supplied asset. Currently only the default_address is used to source the unstakeable balance.

Parameters:

  • asset_id (Symbol)

    The asset to retrieve the unstakeable balance for

  • mode (Symbol)

    The staking mode. Defaults to :default.

  • options (Hash)

    Additional options for the staking operation

Returns:

  • (BigDecimal)

    The unstakeable balance



# File 'lib/coinbase/wallet.rb', line 213