-
Notifications
You must be signed in to change notification settings - Fork 5
/
ethereum.go
50 lines (38 loc) 路 1001 Bytes
/
ethereum.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
package signer
import (
"encoding/hex"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
)
type EthereumWallet struct{}
func (ew *EthereumWallet) SignTransaction(privateKeys []string, unsignedTx *UnsignedTx) (*SignedTx, error) {
privateKeyBytes, err := hex.DecodeString(privateKeys[0])
if err != nil {
return nil, err
}
privKey, err := crypto.ToECDSA(privateKeyBytes)
if err != nil {
return nil, err
}
decodedUnsignedTxBytes, err := hex.DecodeString(string(unsignedTx.Data))
if err != nil {
return nil, err
}
tx := &types.Transaction{}
err = tx.UnmarshalBinary(decodedUnsignedTxBytes)
if err != nil {
return nil, err
}
signer := types.LatestSignerForChainID(tx.ChainId())
signedTx, err := types.SignTx(tx, signer, privKey)
if err != nil {
return nil, err
}
signedTxBytes, err := signedTx.MarshalBinary()
if err != nil {
return nil, err
}
return &SignedTx{
Data: []byte(hex.EncodeToString(signedTxBytes)),
}, nil
}