-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsample2.rb
More file actions
104 lines (93 loc) · 3.03 KB
/
sample2.rb
File metadata and controls
104 lines (93 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
require 'digest/sha2'
require 'xgt/ruby'
require 'base58'
require 'bigdecimal'
@wif = ENV['WIF'] || '5JNHfZYKGaomSFvd4NUdQ9qMcEAC43kujbfjueTHpVapX1Kzq2n'
@name = ENV['NAME'] || 'XGT0000000000000000000000000000000000000000'
@host = ENV['HOST'] || 'http://localhost:8751'
@address_prefix = ENV['ADDRESS_PREFIX'] || 'XGT'
@precision = ENV['PRECISION'] || 8
@bitcoin_key = ENV['BITCOIN_KEY'] || '5Kb8kLf9zgWQnogidDA76MzPL6TsZZY36hWXMssSzNydYXYB9KF'
def rpc
Xgt::Ruby::Rpc.new(@host || 'http://localhost:8751')
end
def config
rpc.call('database_api.get_config', {})
end
def chain_id
config['XGT_CHAIN_ID']
end
def transfer_operation(wallet_name, amount)
{
'type' => 'transfer_operation',
'value' => {
'amount' => {
'amount' => ( amount * (10 ** @precision) ).to_i.to_s,
'precision' => @precision,
'nai' => '@@000000021'
},
'from' => @name,
'to' => wallet_name,
'json_metadata' => '',
}
}
end
def wallet_update_operation(wallet_name, keys)
{
'type' => 'wallet_update_operation',
'value' => {
'wallet' => wallet_name,
'recovery' => {
'weight_threshold' => 1,
'account_auths' => [],
'key_auths' => [[keys['recovery_public'], 1]]
},
'money' => {
'weight_threshold' => 1,
'account_auths' => [],
'key_auths' => [[keys['money_public'], 1]]
},
'social' => {
'weight_threshold' => 1,
'account_auths' => [],
'key_auths' => [[keys['social_public'], 1]]
},
'memo_key' => keys['memo_public'],
'json_metadata' => '',
}
}
end
# Generate a set of keys from a parent wallet name and master key
def generate_keys(from_wallet_name, master_key)
ks = { 'master' => master_key }
%w(recovery money social memo).each do |role|
private_key = Xgt::Ruby::Auth.generate_wif(from_wallet_name, master_key, 'recovery')
public_key = Xgt::Ruby::Auth.wif_to_public_key(private_key, @address_prefix)
ks["#{role}_private"] = private_key
ks["#{role}_public"] = public_key
end
ks['wallet_name'] = Xgt::Ruby::Auth.generate_wallet_name(ks['recovery_public'])
ks
end
# Query the node for a wallet name given a public key
def fetch_wallet_name(public_key)
result = rpc.call('wallet_by_key_api.get_key_references', { 'keys' => [keys['recovery_public']] })
result&.fetch('wallets', [])&.first&.first
end
if __FILE__ == $0
# Generate keys
keys = generate_keys(@name, @bitcoin_key)
recovery_private = keys['recovery_private']
recovery_public = keys['recovery_public']
# Create lazy wallet
txn = { 'operations' => [ transfer_operation(wallet_name, BigDecimal('0.00000001')) ] }
id = rpc.broadcast_transaction(txn, [@wif], chain_id)
(puts 'Waiting...' or sleep 1) until rpc.transaction_ready?(id)
puts 'Done!'
# Redeem lazy wallet
txn = { 'operations' => [ wallet_update_operation(wallet_name, keys) ] }
id = rpc.broadcast_transaction(txn, [recovery_private], chain_id)
(puts 'Waiting...' or sleep 1) until fetch_wallet_name(recovery_public)
puts 'Done!'
p keys
end