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::ENTRY_POINT}.Wallet.generateWalletFromMnemonic(\n        '\#{mnemonic}',\n        '\#{derivation_path&.to_s}' ||\n          \#{Javascript::ENTRY_POINT}.Wallet.getDefaultDerivationPath(),\n        \#{test},\n      );\n    JAVASCRIPT\n  end\n\n  raise Error.new(INVALID_MNEMONIC_OR_DERIVATION_PATH_MSG) if result.nil?\n\n  new(result[:publicKey], result[:privateKey], result[:test])\nend\n"

.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::ENTRY_POINT}.Wallet.generateWalletFromSeed(\n        '\#{seed}',\n        \#{test},\n      );\n    JAVASCRIPT\n  end\n  new(result[:publicKey], result[:privateKey], result[:test])\nend\n"

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
    "      \#{to_javascript}.getAddress();\n    JAVASCRIPT\n  end\nend\n"

#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
    "      \#{to_javascript}.sign('\#{input}');\n    JAVASCRIPT\n  end\n  raise Error.new(SIGN_ERROR_MSG) if signed.nil?\n\n  signed\nend\n"

#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
  "    new \#{Javascript::ENTRY_POINT}.Wallet(\n      '\#{public_key}',\n      '\#{private_key}',\n      \#{test},\n    )\n  JAVASCRIPT\nend\n"

#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
    "      \#{to_javascript}.verify('\#{message}', '\#{signature}');\n    JAVASCRIPT\n  end == true\nend\n"