-
Notifications
You must be signed in to change notification settings - Fork 1
/
key.go
85 lines (74 loc) · 1.65 KB
/
key.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
package main
import (
"encoding/hex"
"github.com/MixinNetwork/mixin/common"
"github.com/MixinNetwork/mixin/crypto"
"github.com/fox-one/mint-withdraw"
)
// Key key
type Key struct {
Spend crypto.Key
View crypto.Key
}
func parseKey(s string) (crypto.Key, error) {
var k crypto.Key
b, err := hex.DecodeString(s)
if err != nil {
return k, err
}
copy(k[:32], b[:32])
return k, nil
}
// NewKey create key
func NewKey(view, spend string) (*Key, error) {
key := &Key{}
{
k, err := parseKey(view)
if err != nil {
return nil, err
}
key.View = k
}
{
k, err := parseKey(spend)
if err != nil {
return nil, err
}
key.Spend = k
}
return key, nil
}
// Accounts accounts
func (k Key) Accounts() []common.Address {
addr := common.Address{
PrivateSpendKey: k.Spend,
PrivateViewKey: k.View,
PublicSpendKey: k.Spend.Public(),
PublicViewKey: k.View.Public(),
}
return []common.Address{addr}
}
// VerifyOutputs verify ouputs
func (k Key) VerifyOutputs(t *mint.Transaction) ([]int, error) {
var outputs = make([]int, 0, len(t.Outputs))
for idx, o := range t.Outputs {
for _, key := range o.Keys {
if crypto.ViewGhostOutputKey(&key, &k.View, &o.Mask, uint64(idx)).String() == k.Spend.Public().String() {
outputs = append(outputs, idx)
break
}
}
}
return outputs, nil
}
// Sign sign transaction, only for transaction, not for mint/deposit
func (k Key) Sign(out *common.Transaction, t *mint.Transaction) (*common.VersionedTransaction, error) {
signed := out.AsLatestVersion()
for i := range signed.Inputs {
err := signed.SignInput(t, i, k.Accounts())
if err != nil {
return nil, err
}
}
return signed, nil
}