/
tx_signature.go
107 lines (89 loc) · 2.98 KB
/
tx_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
105
106
107
// Copyright 2019 The klaytn Authors
// This file is part of the klaytn library.
//
// The klaytn library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The klaytn library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the klaytn library. If not, see <http://www.gnu.org/licenses/>.
package types
import (
"crypto/ecdsa"
"encoding/json"
"math/big"
"github.com/klaytn/klaytn/common"
"github.com/klaytn/klaytn/common/hexutil"
"github.com/klaytn/klaytn/crypto"
)
// TxSignature contains a signature of tx (V, R, S).
type TxSignature struct {
V *big.Int
R *big.Int
S *big.Int
}
// TxSignature contains a signature of tx (V, R, S) as types of hexutil.Big.
type TxSignatureJSON struct {
V *hexutil.Big
R *hexutil.Big
S *hexutil.Big
}
func NewTxSignature() *TxSignature {
return &TxSignature{
big.NewInt(0),
big.NewInt(0),
big.NewInt(0),
}
}
func NewTxSignatureWithValues(signer Signer, txhash common.Hash, prv *ecdsa.PrivateKey) (*TxSignature, error) {
sig, err := crypto.Sign(txhash[:], prv)
if err != nil {
return nil, err
}
txsig := &TxSignature{}
txsig.R, txsig.S, txsig.V, err = signer.SignatureValues(sig)
if err != nil {
return nil, err
}
return txsig, nil
}
func (t *TxSignature) ChainId() *big.Int {
return deriveChainId(t.V)
}
func (t *TxSignature) RawSignatureValues() *TxSignature {
return t
}
func (t *TxSignature) ValidateSignature() bool {
return validateSignature(t.V, t.R, t.S)
}
// RecoverAddress returns an address with given parameters.
// txhash: a hash generated by tx.Hash().
// homestead: true if Homestead or later.
// vfunc: V in the signature is treated differently by Signer. This function is for the treatment.
func (t *TxSignature) RecoverAddress(txhash common.Hash, homestead bool, vfunc func(*big.Int) *big.Int) (common.Address, error) {
V := vfunc(t.V)
return recoverPlain(txhash, t.R, t.S, V, homestead)
}
// RecoverPubKey returns a list of public keys with given parameters.
// txhash: a hash generated by tx.Hash().
// homestead: true if Homestead or later.
// vfunc: V in the signature is treated differently by Signer. This function is for the treatment.
func (t *TxSignature) RecoverPubkey(txhash common.Hash, homestead bool, vfunc func(*big.Int) *big.Int) (*ecdsa.PublicKey, error) {
v := vfunc(t.V)
return recoverPlainPubkey(txhash, t.R, t.S, v, homestead)
}
func (t *TxSignature) equal(tb *TxSignature) bool {
return t.V.Cmp(tb.V) == 0 &&
t.R.Cmp(tb.R) == 0 &&
t.S.Cmp(tb.S) == 0
}
func (t *TxSignature) string() string {
b, _ := json.Marshal(t)
return string(b)
}