-
Notifications
You must be signed in to change notification settings - Fork 4
/
path_transaction.go
147 lines (127 loc) · 3.45 KB
/
path_transaction.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
package btc
import (
"context"
"encoding/hex"
"errors"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
)
func pathTransaction(b *backend) *framework.Path {
return &framework.Path{
Pattern: "transaction/" + framework.GenericNameRegex("name"),
Fields: map[string]*framework.FieldSchema{
"name": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Wallet name",
},
"rawTx": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Raw transaction to be signed",
},
"mode": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Transaction type: standard | multisig | segwit ",
Default: "standard",
},
"token": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Auth token",
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.UpdateOperation: b.pathTransactionWrite,
},
HelpSynopsis: PathTransactionHelpSyn,
HelpDescription: PathTransactionHelpDesc,
}
}
func (b *backend) pathTransactionWrite(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
walletName := d.Get("name").(string)
if walletName == "" {
return nil, errors.New(MissingWalletNameError)
}
mode := d.Get("mode").(string)
walletType, err := parseMode(mode)
if err != nil {
return nil, err
}
t := d.Get("token").(string)
if t == "" {
return nil, errors.New(MissingTokenError)
}
// check if auth token is valid
token, err := b.GetToken(ctx, req.Storage, t, walletType)
if err != nil {
return nil, err
}
if token == nil || token.WalletName != walletName {
return nil, errors.New(InvalidTokenError)
}
w, err := getWalletByType(ctx, b, req.Storage, walletName, walletType)
if err != nil {
return nil, err
}
if w == nil {
return nil, errors.New(WalletNotFoundError)
}
rawTx := d.Get("rawTx").(string)
if rawTx == "" {
return nil, errors.New(MissingRawTxError)
}
seed := seedFromMnemonic(w.Mnemonic)
masterKey, err := getMasterKey(seed, w.Network)
if err != nil {
return nil, err
}
storePath := PathAddress
if walletType == SegWitType {
storePath = PathSegWitAddress
}
storePath += walletName
// derive key of last used address (for multisig is 0)
childnum, err := b.GetLastUsedAddressIndex(ctx, req.Storage, storePath)
if err != nil {
return nil, err
}
derivedPrivKey, err := derivePrivKey(masterKey, append(w.DerivationPath, uint32(childnum)))
privateKey, err := derivedPrivKey.ECPrivKey()
if err != nil {
return nil, err
}
// convert tx string to raw bytes
rawTxBytes, err := hex.DecodeString(rawTx)
if err != nil {
return nil, err
}
// double sha256 before signing
hashedRawTx := chainhash.DoubleHashB(rawTxBytes)
signatureBytes, err := privateKey.Sign(hashedRawTx)
if err != nil {
return nil, err
}
// convert signature raw bytes to string
signature := hex.EncodeToString(signatureBytes.Serialize())
// revoke auth token
err = b.RevokeToken(ctx, req.Storage, token, walletType)
if err != nil {
return nil, err
}
return &logical.Response{
Data: map[string]interface{}{
"signature": signature,
},
}, nil
}
func parseMode(s string) (int, error) {
switch s {
case "standard":
return StandardType, nil
case "multisig":
return MultiSigType, nil
case "segwit":
return SegWitType, nil
default:
return -1, errors.New(InvalidModeError)
}
}