Skip to content

Commit

Permalink
Update Wallet::Data to be keyword initialized (#21)
Browse files Browse the repository at this point in the history
### What changed? Why?
This makes the Coinbase::Wallet::Data object keyword initialized.

Since users have to persist this data they will need to rehydrate this.
Having the ordering matter makes it more likely for developers to= run
into errors (as I did when integrating the SDK).

This also adds a regression test for this PR:
#20

#### Qualified Impact
<!-- Please evaluate what components could be affected and what the
impact would be if there was an
error. How would this error be resolved, e.g. rollback a deploy, push a
new fix, disable a feature
flag, etc... -->
  • Loading branch information
alex-stone authored Apr 30, 2024
1 parent 458f5b8 commit 7d202e8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
6 changes: 3 additions & 3 deletions lib/coinbase/wallet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def transfer(amount, asset_id, destination)
# Exports the Wallet's data to a Data object.
# @return [Data] The Wallet data
def export
Data.new(wallet_id, @master.seed_hex)
Data.new(wallet_id: wallet_id, seed: @master.seed_hex)
end

# The data required to recreate a Wallet.
Expand All @@ -159,7 +159,7 @@ class Data
# Returns a new Data object.
# @param wallet_id [String] The ID of the Wallet
# @param seed [String] The seed of the Wallet
def initialize(wallet_id, seed)
def initialize(wallet_id:, seed:)
@wallet_id = wallet_id
@seed = seed
end
Expand All @@ -174,7 +174,7 @@ def to_hash
# @param data [Hash] The Hash to create the Data object from
# @return [Data] The new Data object
def self.from_hash(data)
Data.new(data['wallet_id'], data['seed'])
Data.new(wallet_id: data['wallet_id'], seed: data['seed'])
end
end

Expand Down
31 changes: 16 additions & 15 deletions spec/unit/coinbase/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,31 +86,32 @@
let(:address_list_model) do
Coinbase::Client::AddressList.new({ 'data' => [address_model], 'total_count' => 1 })
end
let(:wallet_export_data) do
Coinbase::Wallet::Data.new(
wallet_id: wallet_id,
seed: MoneyTree::Master.new.seed_hex
)
end
subject(:imported_wallet) { user.import_wallet(wallet_export_data) }

before do
allow(Coinbase::Client::AddressesApi).to receive(:new).and_return(addresses_api)
allow(Coinbase::Client::WalletsApi).to receive(:new).and_return(wallets_api)
expect(wallets_api).to receive(:create_wallet).with(opts).and_return(wallet_model)
expect(addresses_api)
.to receive(:create_address)
.with(wallet_id, satisfy do |opts|
public_key_present = opts[:create_address_request][:public_key].is_a?(String)
attestation_present = opts[:create_address_request][:attestation].is_a?(String)
public_key_present && attestation_present
end)
expect(wallets_api).to receive(:get_wallet).with(wallet_id).and_return(wallet_model_with_default_address)
expect(wallets_api).to receive(:get_wallet).with(wallet_id).and_return(wallet_model)
expect(addresses_api).to receive(:list_addresses).with(wallet_id).and_return(address_list_model)
expect(addresses_api).to receive(:get_address).and_return(address_model)
end

it 'imports an exported Wallet' do
wallet = user.create_wallet
data = wallet.export
new_wallet = user.import_wallet(data)
it 'imports an exported wallet' do
expect(imported_wallet.wallet_id).to eq(wallet_id)
end

it 'loads the wallet addresses' do
expect(imported_wallet.list_addresses.length).to eq(address_list_model.total_count)
end

expect(new_wallet.wallet_id).to eq(wallet.wallet_id)
expect(new_wallet.list_addresses.length).to eq(wallet.list_addresses.length)
it 'contains the same seed when re-exported' do
expect(imported_wallet.export.seed).to eq(wallet_export_data.seed)
end
end

Expand Down

0 comments on commit 7d202e8

Please sign in to comment.