-
Notifications
You must be signed in to change notification settings - Fork 248
/
signature.go
104 lines (88 loc) · 3.17 KB
/
signature.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
package web3provider
import (
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common/hexutil"
signercore "github.com/ethereum/go-ethereum/signer/core/apitypes"
"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/services/typeddata"
"github.com/status-im/status-go/transactions"
)
// signMessage checks the pwd vs the selected account and signs a message
func (api *API) signMessage(data interface{}, address string, password string) (types.HexBytes, error) {
account, err := api.getVerifiedWalletAccount(address, password)
if err != nil {
return types.HexBytes{}, err
}
var dBytes []byte
switch d := data.(type) {
case string:
dBytes = []byte(d)
case []byte:
dBytes = d
case byte:
dBytes = []byte{d}
}
hash := crypto.TextHash(dBytes)
sig, err := crypto.Sign(hash, account.AccountKey.PrivateKey)
if err != nil {
return types.HexBytes{}, err
}
sig[64] += 27 // Transform V from 0/1 to 27/28 according to the yellow paper
return types.HexBytes(sig), err
}
// signTypedData accepts data and password. Gets verified account and signs typed data.
func (api *API) signTypedData(typed typeddata.TypedData, address string, password string) (types.HexBytes, error) {
account, err := api.getVerifiedWalletAccount(address, password)
if err != nil {
return types.HexBytes{}, err
}
chain := new(big.Int).SetUint64(api.s.config.NetworkID)
sig, err := typeddata.Sign(typed, account.AccountKey.PrivateKey, chain)
if err != nil {
return types.HexBytes{}, err
}
return types.HexBytes(sig), err
}
// signTypedDataV4 accepts data and password. Gets verified account and signs typed data.
func (api *API) signTypedDataV4(typed signercore.TypedData, address string, password string) (types.HexBytes, error) {
account, err := api.getVerifiedWalletAccount(address, password)
if err != nil {
return types.HexBytes{}, err
}
chain := new(big.Int).SetUint64(api.s.config.NetworkID)
sig, err := typeddata.SignTypedDataV4(typed, account.AccountKey.PrivateKey, chain)
if err != nil {
return types.HexBytes{}, err
}
return types.HexBytes(sig), err
}
// SendTransaction creates a new transaction and waits until it's complete.
func (api *API) sendTransaction(chainID uint64, sendArgs transactions.SendTxArgs, password string) (hash types.Hash, err error) {
verifiedAccount, err := api.getVerifiedWalletAccount(sendArgs.From.String(), password)
if err != nil {
return hash, err
}
hash, err = api.s.transactor.SendTransactionWithChainID(chainID, sendArgs, verifiedAccount)
if err != nil {
return
}
go api.s.rpcFiltersSrvc.TriggerTransactionSentToUpstreamEvent(hash)
return
}
func (api *API) EcRecover(data hexutil.Bytes, sig hexutil.Bytes) (types.Address, error) {
if len(sig) != 65 {
return types.Address{}, fmt.Errorf("signature must be 65 bytes long")
}
if sig[64] != 27 && sig[64] != 28 {
return types.Address{}, fmt.Errorf("invalid Ethereum signature (V is not 27 or 28)")
}
sig[64] -= 27 // Transform yellow paper V from 27/28 to 0/1
hash := crypto.TextHash(data)
rpk, err := crypto.SigToPub(hash, sig)
if err != nil {
return types.Address{}, err
}
return crypto.PubkeyToAddress(*rpk), nil
}