-
Notifications
You must be signed in to change notification settings - Fork 12
/
signed_transaction.go
89 lines (73 loc) · 2.03 KB
/
signed_transaction.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
package sign
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"github.com/scorum/bitshares-go/encoding/transaction"
"github.com/scorum/bitshares-go/types"
"log"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcutil"
"github.com/pkg/errors"
)
type SignedTransaction struct {
*types.Transaction
}
func NewSignedTransaction(tx *types.Transaction) *SignedTransaction {
return &SignedTransaction{tx}
}
func (tx *SignedTransaction) Serialize() ([]byte, error) {
var b bytes.Buffer
encoder := transaction.NewEncoder(&b)
if err := encoder.Encode(tx.Transaction); err != nil {
return nil, err
}
return b.Bytes(), nil
}
func (tx *SignedTransaction) Digest(chain string) ([]byte, error) {
var msgBuffer bytes.Buffer
// Write the chain ID.
rawChainID, err := hex.DecodeString(chain)
if err != nil {
return nil, errors.Wrapf(err, "failed to decode chain ID: %v", chain)
}
if _, err := msgBuffer.Write(rawChainID); err != nil {
return nil, errors.Wrap(err, "failed to write chain ID")
}
// Write the serialized transaction.
rawTx, err := tx.Serialize()
if err != nil {
return nil, err
}
if _, err := msgBuffer.Write(rawTx); err != nil {
return nil, errors.Wrap(err, "failed to write serialized transaction")
}
msgBytes := msgBuffer.Bytes()
message := hex.EncodeToString(msgBytes)
log.Printf("[DEBUG] Digest final message:%s\n", message)
// Compute the digest.
digest := sha256.Sum256(msgBytes)
return digest[:], nil
}
func (tx *SignedTransaction) Sign(wifs []string, chain string) error {
digest, err := tx.Digest(chain)
if err != nil {
return err
}
privKeys := make([]*btcec.PrivateKey, len(wifs))
for index, wif := range wifs {
w, err := btcutil.DecodeWIF(wif)
if err != nil {
return err
}
privKeys[index] = w.PrivKey
}
// Set the signature array in the transaction.
sigsHex := make([]string, len(privKeys))
for index, privKey := range privKeys {
sig := SignBufferSha256(digest, privKey.ToECDSA())
sigsHex[index] = hex.EncodeToString(sig)
}
tx.Transaction.Signatures = sigsHex
return nil
}