-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransaction_input.go
54 lines (44 loc) · 1.17 KB
/
transaction_input.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
package kcoin
import (
"bytes"
"crypto/ecdsa"
"crypto/elliptic"
"math/big"
)
type TransactionInput struct {
TxID []byte
OutputIndex int
XYAppendedPubKey []byte
XYAppendedSignature []byte
}
func (txInput *TransactionInput) GetSignature() (*big.Int, *big.Int) {
r := big.Int{}
s := big.Int{}
sigLen := len(txInput.XYAppendedSignature)
r.SetBytes(txInput.XYAppendedSignature[:(sigLen / 2)])
s.SetBytes(txInput.XYAppendedSignature[(sigLen / 2):])
return &r, &s
}
func (txInput *TransactionInput) GetPublickKey() *ecdsa.PublicKey {
curve := elliptic.P256()
x := big.Int{}
y := big.Int{}
keyLen := len(txInput.XYAppendedPubKey)
x.SetBytes(txInput.XYAppendedPubKey[:(keyLen / 2)])
y.SetBytes(txInput.XYAppendedPubKey[(keyLen / 2):])
return &ecdsa.PublicKey{
Curve: curve,
X: &x,
Y: &y,
}
}
func (txInput *TransactionInput) UsesKey(pubKeyHash []byte) bool {
if len(txInput.XYAppendedPubKey) == 0 {
return false
}
lockingHash := hashPubKey(*txInput.GetPublickKey())
return bytes.Compare(lockingHash, pubKeyHash) == 0
}
func getXYAppendedPubKey(pubKey ecdsa.PublicKey) []byte {
return append(pubKey.X.Bytes(), pubKey.Y.Bytes()...)
}