Class: Xpring::Wallet

Inherits:
Object
  • Object
show all
Defined in:
lib/xpring/wallet.rb

Overview

Representation of a XRP wallet

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(public_key, private_key, test) ⇒ Wallet

Returns a new instance of Wallet.

Parameters:

  • public_key (#to_s)
  • private_key (#to_s)
  • test (true, false)


55
56
57
58
59
# File 'lib/xpring/wallet.rb', line 55

def initialize(public_key, private_key, test)
  @public_key = public_key.to_s
  @private_key = private_key.to_s
  @test = test
end

Instance Attribute Details

#private_keyObject (readonly)

Returns the value of attribute private_key.



13
14
15
# File 'lib/xpring/wallet.rb', line 13

def private_key
  @private_key
end

#public_keyObject (readonly)

Returns the value of attribute public_key.



13
14
15
# File 'lib/xpring/wallet.rb', line 13

def public_key
  @public_key
end

#testObject (readonly)

Returns the value of attribute test.



13
14
15
# File 'lib/xpring/wallet.rb', line 13

def test
  @test
end

Class Method Details

.from_mnemonic(mnemonic, derivation_path: nil, test: false) ⇒ Xpring::Wallet

Parameters:

  • mnemonic (#to_s)
  • derivation_path (#to_s, nil) (defaults to: nil)
  • test (true, false) (defaults to: false)

Returns:

Raises:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/xpring/wallet.rb', line 20

def self.from_mnemonic(mnemonic, derivation_path: nil, test: false)
  result = Javascript.run do
    <<~JAVASCRIPT
      #{Javascript::ENTRY_POINT}.Wallet.generateWalletFromMnemonic(
        '#{mnemonic}',
        '#{derivation_path&.to_s}' ||
          #{Javascript::ENTRY_POINT}.Wallet.getDefaultDerivationPath(),
        #{test},
      );
    JAVASCRIPT
  end

  raise Error.new(INVALID_MNEMONIC_OR_DERIVATION_PATH_MSG) if result.nil?

  new(result[:publicKey], result[:privateKey], result[:test])
end

.from_seed(seed, test: false) ⇒ Xpring::Wallet

Parameters:

  • seed (#to_s)
  • test (true, false) (defaults to: false)

Returns:



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/xpring/wallet.rb', line 40

def self.from_seed(seed, test: false)
  result = Javascript.run do
    <<~JAVASCRIPT
      #{Javascript::ENTRY_POINT}.Wallet.generateWalletFromSeed(
        '#{seed}',
        #{test},
      );
    JAVASCRIPT
  end
  new(result[:publicKey], result[:privateKey], result[:test])
end

Instance Method Details

#addressString

Returns:

  • (String)


62
63
64
65
66
67
68
# File 'lib/xpring/wallet.rb', line 62

def address
  @address ||= Javascript.run do
    <<~JAVASCRIPT
      #{to_javascript}.getAddress();
    JAVASCRIPT
  end
end

#sign(input) ⇒ String

Parameters:

  • input (#to_s)

Returns:

  • (String)

Raises:



73
74
75
76
77
78
79
80
81
82
# File 'lib/xpring/wallet.rb', line 73

def sign(input)
  signed = Javascript.run do
    <<~JAVASCRIPT
      #{to_javascript}.sign('#{input}');
    JAVASCRIPT
  end
  raise Error.new(SIGN_ERROR_MSG) if signed.nil?

  signed
end

#to_javascriptString

Returns Javascript constructor expression for this Wallet.

Returns:

  • (String)

    Javascript constructor expression for this Wallet



96
97
98
99
100
101
102
103
104
# File 'lib/xpring/wallet.rb', line 96

def to_javascript
  <<~JAVASCRIPT
    new #{Javascript::ENTRY_POINT}.Wallet(
      '#{public_key}',
      '#{private_key}',
      #{test},
    )
  JAVASCRIPT
end

#valid?(message, signature) ⇒ true, false

Parameters:

  • message (#to_s)
  • signature (#to_s)

Returns:

  • (true, false)


87
88
89
90
91
92
93
# File 'lib/xpring/wallet.rb', line 87

def valid?(message, signature)
  Javascript.run do
    <<~JAVASCRIPT
      #{to_javascript}.verify('#{message}', '#{signature}');
    JAVASCRIPT
  end == true
end