forked from bitbegin/eth-wallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eth-wallet.red
96 lines (87 loc) · 1.88 KB
/
eth-wallet.red
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
Red [
Title: "eth-wallet"
Author: "bitbegin"
File: %eth-wallet.red
Tabs: 4
License: "BSD-3 - https://github.com/red/red/blob/master/BSD-3-License.txt"
]
#include %bip32.red
#include %rlp.red
eth-wallet: context [
private-key: none
seeds: none
; set the bip32 path of the wallet
; type: block!
; e.g [8000002Ch 8000003Ch 80000000h 0 idx]
bip32-path: [8000002Ch 8000003Ch 80000000h 0] ;-- default: ETH coin, account 0, change 0
init: func [
"create the master private key"
seed [block! none!] ;-- 24-word seed, if none, create a random one
password [string!]
return: [block!] ;-- return [words entropy seed]
][
seeds: either seed [
Mnemonic/from-words seed password
][
Mnemonic/new 'Type24Words password
]
seeds/1
]
get-address: func [
idx [integer! none!]
return: [string!]
][
bip32key/pubkey-to-address get-public idx
]
; tx: [
; nonce [integer!]
; gas-price [binary!]
; gas-limit [integer!]
; to-address [binary!]
; amount [binary!] ;-- Wei
; data [binary!]
; ]
sign-transaction: func [
idx [integer! none!]
tx [block!]
chain-id [integer!]
return: [binary!]
/local key raw hash sig
][
key: either integer? idx [get-private idx][private-key]
append tx reduce [chain-id 0 0]
raw: rlp/encode tx
hash: secp256/sha3-256 raw
sig: secp256/sign hash key
poke tx 7 chain-id * 2 + 35 + sig/1
poke tx 8 sig/2
poke tx 9 sig/3
rlp/encode tx
]
get-public: func [
idx [integer! none!]
return: [binary!]
/local path xpub
][
either idx = none [
path: copy bip32-path
][
path: append copy bip32-path idx
]
xpub: bip32key/derive seeds/3 path false
xpub/6
]
get-private: func [
idx [integer! none!]
return: [binary!]
/local path xprv
][
either idx = none [
path: copy bip32-path
][
path: append copy bip32-path idx
]
xprv: bip32key/derive seeds/3 path true
xprv/6
]
]