-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsign.go
136 lines (119 loc) · 3.44 KB
/
sign.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
package kms
import (
"context"
"encoding/hex"
"fmt"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
"github.com/perme-io/vault-plugin-secrets-kms/chains"
)
func sign(privKeyString string, chainName chains.ChainName, serializedString string) (string, error) {
privKeyBytes, err := hex.DecodeString(privKeyString)
if err != nil {
return "", err
}
var chain chains.Chain
privateKey := secp256k1.PrivKeyFromBytes(privKeyBytes)
switch chainName {
case chains.ICON:
chain = chains.IconChain{PrivateKey: privateKey}
case chains.AERGO:
chain = chains.AergoChain{PrivateKey: privateKey}
default:
return "", fmt.Errorf("unknown chain name: %v", chainName)
}
if base64Sign, signErr := chain.SignCompact(serializedString); signErr != nil {
return "", signErr
} else {
return base64Sign, nil
}
}
func pathSign(b *kmsBackend) []*framework.Path {
return []*framework.Path{
{
Pattern: "wallet/sign",
Fields: map[string]*framework.FieldSchema{
"username": {
Type: framework.TypeString,
Description: "username of wallet",
Required: true,
},
"address": {
Type: framework.TypeString,
Description: "address of wallet",
Required: true,
},
"chainName": {
Type: framework.TypeString,
Description: "name of blockchain",
Required: true,
},
"txSerialized": {
Type: framework.TypeString,
Description: "serialized transaction data",
Required: true,
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.CreateOperation: &framework.PathOperation{
Callback: b.pathSignCreate,
},
logical.UpdateOperation: &framework.PathOperation{
Callback: b.pathSignCreate,
},
},
HelpSynopsis: pathSignHelpSynopsis,
HelpDescription: pathSignHelpDescription,
},
}
}
func (b *kmsBackend) pathSignCreate(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
var username string
if un, ok := d.GetOk("username"); ok {
username = un.(string)
} else if !ok {
return nil, fmt.Errorf("missing username in sign")
}
var address string
if addr, ok := d.GetOk("address"); ok {
address = addr.(string)
} else if !ok {
return nil, fmt.Errorf("missing address in sign")
}
var chainName chains.ChainName
if wtype, ok := d.GetOk("chainName"); ok {
chainName = chains.ChainName(wtype.(string))
} else if !ok {
return nil, fmt.Errorf("missing chainName in sign")
}
b.Logger().Debug("chainName:", chainName)
var txSerialized string
if ts, ok := d.GetOk("txSerialized"); ok {
txSerialized = ts.(string)
} else if !ok {
return nil, fmt.Errorf("missing txSerialized in sign")
}
walletPath := getWalletPath(username, address)
wallet, err := getWallet(ctx, req, walletPath)
if err != nil {
return nil, err
}
signature, err := sign(wallet.PrivateKey, chainName, txSerialized)
if err != nil {
return nil, fmt.Errorf("faild to sign: err=%v", err)
}
response := &logical.Response{
Data: map[string]interface{}{
"signature": signature,
},
}
return response, nil
}
const (
pathSignHelpSynopsis = `Manages the Vault signature for send transaction.`
pathSignHelpDescription = `
This path allows you to create signature used to send transaction.
You can get a signature to send transaction using user wallet by setting the username and txSeriailzed field.
`
)