-
Notifications
You must be signed in to change notification settings - Fork 2
/
bursa.go
252 lines (230 loc) · 6.69 KB
/
bursa.go
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// Copyright 2023 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bursa
import (
"encoding/json"
"fmt"
// TODO: replace these w/ gOuroboros (blinklabs-io/gouroboros#364)
"github.com/fivebinaries/go-cardano-serialization/address"
"github.com/fivebinaries/go-cardano-serialization/bip32"
"github.com/fivebinaries/go-cardano-serialization/network"
"github.com/fxamacker/cbor/v2"
bip39 "github.com/tyler-smith/go-bip39"
"github.com/blinklabs-io/bursa/internal/config"
)
type KeyFile struct {
Type string `json:"type"`
Description string `json:"description"`
CborHex string `json:"cborHex"`
}
type Wallet struct {
Mnemonic string `json:"mnemonic"`
PaymentAddress string `json:"payment_address"`
StakeAddress string `json:"stake_address"`
PaymentVKey KeyFile `json:"payment_kvey"`
PaymentSKey KeyFile `json:"payment_skey"`
PaymentExtendedSKey KeyFile `json:"payment_extended_skey"`
StakeVKey KeyFile `json:"stake_vkey"`
StakeSKey KeyFile `json:"stake_skey"`
StakeExtendedSKey KeyFile `json:"stake_extended_skey"`
}
func NewWallet(
mnemonic, network string,
accountId uint,
paymentId, stakeId, addressId uint32,
) (*Wallet, error) {
rootKey, err := GetRootKeyFromMnemonic(mnemonic)
if err != nil {
return nil, fmt.Errorf("failed to get root key from mnemonic: %s", err)
}
accountKey := GetAccountKey(rootKey, accountId)
paymentKey := GetPaymentKey(accountKey, paymentId)
stakeKey := GetStakeKey(accountKey, stakeId)
addr := GetAddress(accountKey, network, addressId)
w := &Wallet{
Mnemonic: mnemonic,
PaymentAddress: addr.String(),
StakeAddress: addr.ToReward().String(),
PaymentVKey: GetPaymentVKey(paymentKey),
PaymentSKey: GetPaymentSKey(paymentKey),
PaymentExtendedSKey: GetPaymentExtendedSKey(paymentKey),
StakeVKey: GetStakeVKey(stakeKey),
StakeSKey: GetStakeSKey(stakeKey),
StakeExtendedSKey: GetStakeExtendedSKey(stakeKey),
}
return w, nil
}
func NewDefaultWallet(mnemonic string) (*Wallet, error) {
cfg := config.GetConfig()
w, err := NewWallet(mnemonic, cfg.Network, 0, 0, 0, 0)
if err != nil {
return nil, fmt.Errorf("failed to create default wallet: %s", err)
}
return w, nil
}
func NewMnemonic() (string, error) {
entropy, err := bip39.NewEntropy(256)
if err != nil {
return "", err
}
mnemonic, err := bip39.NewMnemonic(entropy)
if err != nil {
return "", err
}
return mnemonic, nil
}
func GetRootKeyFromMnemonic(mnemonic string) (bip32.XPrv, error) {
entropy, err := bip39.EntropyFromMnemonic(mnemonic)
if err != nil {
return nil, err
}
rootKey := GetRootKey(entropy, []byte{}) // TODO: support password
return rootKey, nil
}
func GetRootKey(entropy []byte, password []byte) bip32.XPrv {
return bip32.FromBip39Entropy(entropy, password)
}
func GetAccountKey(rootKey bip32.XPrv, num uint) bip32.XPrv {
const harden = 0x80000000
return rootKey.
Derive(uint32(harden + 1852)).
Derive(uint32(harden + 1815)).
Derive(uint32(harden + num))
}
func GetPaymentKey(accountKey bip32.XPrv, num uint32) bip32.XPrv {
return accountKey.Derive(0).Derive(num)
}
func GetPaymentVKey(paymentKey bip32.XPrv) KeyFile {
keyCbor, err := cbor.Marshal(paymentKey.Public().PublicKey())
if err != nil {
panic(err)
}
return KeyFile{
Type: "PaymentVerificationKeyShelley_ed25519",
Description: "Payment Verification Key",
CborHex: fmt.Sprintf("%x", keyCbor),
}
}
func GetPaymentSKey(paymentKey bip32.XPrv) KeyFile {
keyCbor, err := cbor.Marshal(paymentKey)
if err != nil {
panic(err)
}
return KeyFile{
Type: "PaymentSigningKeyShelley_ed25519",
Description: "Payment Signing Key",
CborHex: fmt.Sprintf("%x", keyCbor),
}
}
func GetPaymentExtendedSKey(paymentKey bip32.XPrv) KeyFile {
keyCbor, err := cbor.Marshal(
GetExtendedPrivateKey(paymentKey, paymentKey.Public().PublicKey()),
)
if err != nil {
panic(err)
}
return KeyFile{
Type: "PaymentExtendedSigningKeyShelley_ed25519_bip32",
Description: "Payment Extended Signing Key (BIP32)",
CborHex: fmt.Sprintf("%x", keyCbor),
}
}
func GetStakeKey(accountKey bip32.XPrv, num uint32) bip32.XPrv {
return accountKey.Derive(2).Derive(num)
}
func GetStakeVKey(stakeKey bip32.XPrv) KeyFile {
keyCbor, err := cbor.Marshal(stakeKey.Public().PublicKey())
if err != nil {
panic(err)
}
return KeyFile{
Type: "StakeVerificationKeyShelley_ed25519",
Description: "Stake Verification Key",
CborHex: fmt.Sprintf("%x", keyCbor),
}
}
func GetStakeSKey(stakeKey bip32.XPrv) KeyFile {
keyCbor, err := cbor.Marshal(stakeKey)
if err != nil {
panic(err)
}
return KeyFile{
Type: "StakeSigningKeyShelley_ed25519",
Description: "Stake Signing Key",
CborHex: fmt.Sprintf("%x", keyCbor),
}
}
func GetStakeExtendedSKey(stakeKey bip32.XPrv) KeyFile {
keyCbor, err := cbor.Marshal(
GetExtendedPrivateKey(stakeKey, stakeKey.Public().PublicKey()),
)
if err != nil {
panic(err)
}
return KeyFile{
Type: "StakeExtendedSigningKeyShelley_ed25519_bip32",
Description: "Stake Extended Signing Key (BIP32)",
CborHex: fmt.Sprintf("%x", keyCbor),
}
}
func GetAddress(
accountKey bip32.XPrv,
net string,
num uint32,
) *address.BaseAddress {
nw := network.TestNet()
if net == "mainnet" {
nw = network.MainNet()
}
paymentKeyPublicHash := GetPaymentKey(
accountKey,
num,
).Public().
PublicKey().
Hash()
stakeKeyPublicHash := GetStakeKey(
accountKey,
num,
).Public().
PublicKey().
Hash()
addr := address.NewBaseAddress(
nw,
&address.StakeCredential{
Kind: address.KeyStakeCredentialType,
Payload: paymentKeyPublicHash[:],
},
&address.StakeCredential{
Kind: address.KeyStakeCredentialType,
Payload: stakeKeyPublicHash[:],
},
)
return addr
}
func GetExtendedPrivateKey(privateKey []byte, publicKey []byte) bip32.XPrv {
xprv := bip32.XPrv{}
xprv = append(xprv, privateKey[:64]...)
xprv = append(xprv, publicKey...)
xprv = append(xprv, privateKey[64:]...)
return xprv
}
func GetKeyFile(keyFile KeyFile) string {
// Use 4 spaces for indent
ret, err := json.MarshalIndent(keyFile, "", " ")
if err != nil {
return ""
}
// Append newline
return fmt.Sprintf("%s\n", ret)
}