Skip to content

Commit

Permalink
Add #tokens method to Wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
Yamaguchi committed Nov 3, 2023
1 parent 53d0b7c commit 7b2e6f7
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 3 deletions.
4 changes: 4 additions & 0 deletions lib/glueby/internal/wallet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ def balance(only_finalized = true)
wallet_adapter.balance(id, only_finalized)
end

def tokens(color_id = Tapyrus::Color::ColorIdentifier.default, only_finalized = true)
wallet_adapter.tokens(id, color_id, only_finalized)
end

# @param only_finalized [Boolean] The flag to get a UTXO with status only finalized
# @param label [String] This label is used to filtered the UTXOs with labeled if a key or Utxo is labeled.
# - If label is nil or :unlabeled, only unlabeled UTXOs will be returned.
Expand Down
11 changes: 11 additions & 0 deletions lib/glueby/internal/wallet/abstract_wallet_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ def balance(wallet_id, only_finalized = true)
raise NotImplementedError, "You must implement #{self.class}##{__method__}"
end

# Returns all tokens with specified color_id
#
# @param [String] wallet_id - The wallet id that is offered by `create_wallet()` method.
# @param [String] color_id - The color id
# @param [Boolean] only_finalized - includes only finalized UTXO value if it
# is true. Default is true.
# @return [Array<Utxo>] The array of the utxos with specified color_id
def tokens(wallet_id, color_id = Tapyrus::Color::ColorIdentifier.default, only_finalized = true)
raise NotImplementedError, "You must implement #{self.class}##{__method__}"
end

# Returns the UTXOs that the wallet has.
# If label is specified, return UTXOs filtered with label
#
Expand Down
4 changes: 4 additions & 0 deletions lib/glueby/internal/wallet/active_record/wallet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def utxos
Glueby::Internal::Wallet::AR::Utxo.where(key: keys)
end

def tokens(color_id = Tapyrus::Color::ColorIdentifier.default)
utxos.where("lower(script_pubkey) like ?", "21#{color_id.to_hex}%")
end

private

def sign_tx_for_p2pkh(tx, index, key, script_pubkey, sighashtype)
Expand Down
7 changes: 7 additions & 0 deletions lib/glueby/internal/wallet/active_record_wallet_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@ def has_address?(wallet_id, address)
wallet.keys.exists?(script_pubkey: script_pubkey.to_hex)
end

def tokens(wallet_id, color_id = Tapyrus::Color::ColorIdentifier.default, only_finalized = true)
wallet = AR::Wallet.find_by(wallet_id: wallet_id)
utxos = wallet.tokens(color_id)
utxos = utxos.where(status: :finalized) if only_finalized
utxos
end

private

# Calculate commitment = H(P || contents)
Expand Down
4 changes: 4 additions & 0 deletions lib/glueby/wallet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ def balances(only_finalized = true)
end
end

def tokens(color_id = Tapyrus::Color::ColorIdentifier.default, only_finalized = true)
@internal_wallet.tokens(color_id, only_finalized)
end

private

def initialize(internal_wallet)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
end
end

describe '#tokens' do
it 'does not implemented' do
expect { adapter.tokens("wallet_id") }.to raise_error(NotImplementedError)
end
end

describe '#list_unspent' do
it 'does not implemented' do
expect { adapter.list_unspent("wallet_id") }.to raise_error(NotImplementedError)
Expand Down
66 changes: 63 additions & 3 deletions spec/glueby/internal/wallet/active_record/wallet_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

RSpec.describe 'Glueby::Internal::Wallet::AR::Wallet', active_record: true do
let(:wallet) { Glueby::Internal::Wallet::AR::Wallet.create(wallet_id: 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF') }
let(:color_id) { Tapyrus::Color::ColorIdentifier.parse_from_payload('c185856a84c483fb108b1cdf79ff53aa7d54d1a137a5178684bd89ca31f906b2bd'.htb) }
let(:key1) { wallet.keys.create(purpose: :receive) }
let(:key2) { wallet.keys.create(purpose: :receive) }

describe '#sign' do
subject { wallet.sign(tx, prevtxs, sighashtype: sighashtype) }

let(:sighashtype) { Tapyrus::SIGHASH_TYPE[:all] }
let(:key1) { wallet.keys.create(purpose: :receive) }
let(:key2) { wallet.keys.create(purpose: :receive) }
let(:tx) do
tx = Tapyrus::Tx.new
tx.inputs << Tapyrus::TxIn.new(out_point: Tapyrus::OutPoint.new('00' * 32, 0))
Expand All @@ -17,7 +18,6 @@
tx.outputs << Tapyrus::TxOut.new(value: 1, script_pubkey: Tapyrus::Script.new)
tx
end
let(:color_id) { Tapyrus::Color::ColorIdentifier.parse_from_payload('c185856a84c483fb108b1cdf79ff53aa7d54d1a137a5178684bd89ca31f906b2bd'.htb) }
let(:prevtxs) { [] }

before do
Expand Down Expand Up @@ -160,4 +160,64 @@
it { is_expected.to be_invalid }
end
end

describe '#tokens' do
subject { wallet.tokens(color_id) }

let(:color_id2) { Tapyrus::Color::ColorIdentifier.parse_from_payload('c11863143c14c5166804bd19203356da136c985678cd4d27a1b8c6329604903262'.htb) }

let(:utxo1) do
Glueby::Internal::Wallet::AR::Utxo.create(
txid: '0000000000000000000000000000000000000000000000000000000000000000',
index: 0,
value: 600,
script_pubkey: key1.to_p2pkh.to_hex,
status: :init,
key: key1
)
end
let(:utxo2) do
Glueby::Internal::Wallet::AR::Utxo.create(
txid: '1111111111111111111111111111111111111111111111111111111111111111',
index: 0,
value: 600,
script_pubkey: key2.to_p2pkh.to_hex,
status: :init,
key: key2
)
end
let(:utxo3) do
colored_script = key1.to_p2pkh.add_color(color_id)
Glueby::Internal::Wallet::AR::Utxo.create(
txid: '2222222222222222222222222222222222222222222222222222222222222222',
index: 0,
script_pubkey: colored_script.to_hex,
value: 1,
status: :finalized,
key: key1
)
end
let(:utxo4) do
colored_script = key1.to_p2pkh.add_color(color_id2)
Glueby::Internal::Wallet::AR::Utxo.create(
txid: '2222222222222222222222222222222222222222222222222222222222222222',
index: 0,
script_pubkey: colored_script.to_hex,
value: 1,
status: :finalized,
key: key1
)
end

before do
utxo1
utxo2
utxo3
utxo4
end

it do
expect(subject).to eq [utxo3]
end
end
end
81 changes: 81 additions & 0 deletions spec/glueby/internal/wallet/active_record_wallet_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,87 @@
end
end

describe '#tokens' do
subject { adapter.tokens(wallet_id, color_id, only_finalized) }

let(:wallet_id) { wallet.wallet_id }
let(:color_id) { Tapyrus::Color::ColorIdentifier.parse_from_payload('c185856a84c483fb108b1cdf79ff53aa7d54d1a137a5178684bd89ca31f906b2bd'.htb) }
let(:color_id2) { Tapyrus::Color::ColorIdentifier.parse_from_payload('c11863143c14c5166804bd19203356da136c985678cd4d27a1b8c6329604903262'.htb) }
let(:key1) { wallet.keys.create(purpose: :receive) }
let(:key2) { wallet.keys.create(purpose: :receive) }
let(:only_finalized) { true }

let(:utxo1) do
Glueby::Internal::Wallet::AR::Utxo.create(
txid: '0000000000000000000000000000000000000000000000000000000000000000',
index: 0,
value: 600,
script_pubkey: key1.to_p2pkh.to_hex,
status: :init,
key: key1
)
end
let(:utxo2) do
Glueby::Internal::Wallet::AR::Utxo.create(
txid: '1111111111111111111111111111111111111111111111111111111111111111',
index: 0,
value: 600,
script_pubkey: key2.to_p2pkh.to_hex,
status: :init,
key: key2
)
end
let(:utxo3) do
colored_script = key1.to_p2pkh.add_color(color_id)
Glueby::Internal::Wallet::AR::Utxo.create(
txid: '2222222222222222222222222222222222222222222222222222222222222222',
index: 0,
script_pubkey: colored_script.to_hex,
value: 1,
status: :finalized,
key: key1
)
end
let(:utxo4) do
colored_script = key1.to_p2pkh.add_color(color_id2)
Glueby::Internal::Wallet::AR::Utxo.create(
txid: '2222222222222222222222222222222222222222222222222222222222222222',
index: 3,
script_pubkey: colored_script.to_hex,
value: 1,
status: :finalized,
key: key1
)
end
let(:utxo5) do
colored_script = key1.to_p2pkh.add_color(color_id)
Glueby::Internal::Wallet::AR::Utxo.create(
txid: '2222222222222222222222222222222222222222222222222222222222222222',
index: 4,
script_pubkey: colored_script.to_hex,
value: 1,
status: :init,
key: key1
)
end

before do
utxo1
utxo2
utxo3
utxo4
utxo5
end

it { expect(subject.to_a).to eq [utxo3]}

context 'only_finalized = false' do
let(:only_finalized) { false }

it { expect(subject.to_a).to eq [utxo3, utxo5] }
end
end

describe '#create_pay_to_contract_private_key' do
subject { adapter.send(:create_pay_to_contract_private_key, wallet_id, payment_base, contents) }

Expand Down
14 changes: 14 additions & 0 deletions spec/glueby/internal/wallet_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -536,4 +536,18 @@ def load_wallet(wallet_id); end
expect(Glueby::Internal::Wallet.wallet_adapter).to have_received(:pay_to_contract_key).with(wallet.id, payment_base, contents)
end
end

describe "#tokens" do
subject { wallet.tokens(color_id) }

let(:color_id) { Tapyrus::Color::ColorIdentifier.parse_from_payload('c150ad685ec8638543b2356cb1071cf834fb1c84f5fa3a71699c3ed7167dfcdbb3'.htb) }

it "call WalletAdapter#tokens" do
allow(Glueby::Internal::Wallet.wallet_adapter).to receive(:tokens)

subject

expect(Glueby::Internal::Wallet.wallet_adapter).to have_received(:tokens).with(wallet.id, color_id, true)
end
end
end
18 changes: 18 additions & 0 deletions spec/glueby/wallet_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ def list_unspent(wallet_id, only_finalized = true, label = nil)
utxos
end
end


def tokens(wallet_id, color_id, only_finalized)
[]
end
end

before { Glueby::Internal::Wallet.wallet_adapter = TestWalletAdapter.new }
Expand Down Expand Up @@ -140,4 +145,17 @@ def list_unspent(wallet_id, only_finalized = true, label = nil)
it { is_expected.to eq expected }
end
end

describe '#tokens' do
subject { wallet.tokens(color_id, only_finalized) }

let(:wallet) { Glueby::Wallet.create }
let(:only_finalized) {true}
let(:color_id) { Tapyrus::Color::ColorIdentifier.parse_from_payload("c150ad685ec8638543b2356cb1071cf834fb1c84f5fa3a71699c3ed7167dfcdbb3".htb) }
it do
expect(TestWalletAdapter).to receive(:tokens)
subject
expect(TestWalletAdapter).to have_received(:tokens).with(wallet.id, color_id, only_finalized)
end
end
end

0 comments on commit 7b2e6f7

Please sign in to comment.