Use WrapXxx to safelt construct XxxS structs
This commit is contained in:
parent
1a54c6adb6
commit
57356beab6
|
@ -123,7 +123,7 @@ func sendTxCmd(cmd *cobra.Command, args []string) error {
|
|||
|
||||
// sign that puppy
|
||||
signBytes := tx.SignBytes(chainIDFlag)
|
||||
tx.Inputs[0].Signature = crypto.SignatureS{privKey.Sign(signBytes)}
|
||||
tx.Inputs[0].Signature = crypto.WrapSignature(privKey.Sign(signBytes))
|
||||
|
||||
fmt.Println("Signed SendTx:")
|
||||
fmt.Println(string(wire.JSONBytes(tx)))
|
||||
|
@ -179,7 +179,7 @@ func AppTx(name string, data []byte) error {
|
|||
Data: data,
|
||||
}
|
||||
|
||||
tx.Input.Signature = crypto.SignatureS{privKey.Sign(tx.SignBytes(chainIDFlag))}
|
||||
tx.Input.Signature = crypto.WrapSignature(privKey.Sign(tx.SignBytes(chainIDFlag)))
|
||||
|
||||
fmt.Println("Signed AppTx:")
|
||||
fmt.Println(string(wire.JSONBytes(tx)))
|
||||
|
|
|
@ -70,7 +70,7 @@ imports:
|
|||
- name: github.com/tendermint/go-config
|
||||
version: 620dcbbd7d587cf3599dedbf329b64311b0c307a
|
||||
- name: github.com/tendermint/go-crypto
|
||||
version: 0ca2c6fdb0706001ca4c4b9b80c9f428e8cf39da
|
||||
version: 8ff4ce222d32c2328ff0d2bf121fcd34254c03c1
|
||||
- name: github.com/tendermint/go-data
|
||||
version: e7fcc6d081ec8518912fcdc103188275f83a3ee5
|
||||
- name: github.com/tendermint/go-db
|
||||
|
|
|
@ -53,7 +53,7 @@ func TestCounterPlugin(t *testing.T) {
|
|||
signBytes := tx.SignBytes(chainID)
|
||||
// t.Logf("Sign bytes: %X\n", signBytes)
|
||||
sig := test1PrivAcc.Sign(signBytes)
|
||||
tx.Input.Signature = crypto.SignatureS{sig}
|
||||
tx.Input.Signature = crypto.WrapSignature(sig)
|
||||
// t.Logf("Signed TX bytes: %X\n", wire.BinaryBytes(struct{ types.Tx }{tx}))
|
||||
|
||||
// Write request
|
||||
|
|
|
@ -67,7 +67,7 @@ func main() {
|
|||
// Sign request
|
||||
signBytes := tx.SignBytes(chainID)
|
||||
sig := root.Sign(signBytes)
|
||||
tx.Inputs[0].Signature = crypto.SignatureS{sig}
|
||||
tx.Inputs[0].Signature = crypto.WrapSignature(sig)
|
||||
//fmt.Println("tx:", tx)
|
||||
|
||||
// Write request
|
||||
|
@ -118,7 +118,7 @@ func main() {
|
|||
// Sign request
|
||||
signBytes := tx.SignBytes(chainID)
|
||||
sig := privAccountA.Sign(signBytes)
|
||||
tx.Inputs[0].Signature = crypto.SignatureS{sig}
|
||||
tx.Inputs[0].Signature = crypto.WrapSignature(sig)
|
||||
//fmt.Println("tx:", tx)
|
||||
|
||||
// Write request
|
||||
|
|
|
@ -50,7 +50,7 @@ func TestSendTx(t *testing.T) {
|
|||
signBytes := tx.SignBytes(chainID)
|
||||
// t.Log("Sign bytes: %X\n", signBytes)
|
||||
sig := test1PrivAcc.Sign(signBytes)
|
||||
tx.Inputs[0].Signature = crypto.SignatureS{sig}
|
||||
tx.Inputs[0].Signature = crypto.WrapSignature(sig)
|
||||
// t.Log("Signed TX bytes: %X\n", wire.BinaryBytes(types.TxS{tx}))
|
||||
|
||||
// Write request
|
||||
|
@ -102,7 +102,7 @@ func TestSequence(t *testing.T) {
|
|||
// Sign request
|
||||
signBytes := tx.SignBytes(chainID)
|
||||
sig := test1PrivAcc.Sign(signBytes)
|
||||
tx.Inputs[0].Signature = crypto.SignatureS{sig}
|
||||
tx.Inputs[0].Signature = crypto.WrapSignature(sig)
|
||||
// t.Log("ADDR: %X -> %X\n", tx.Inputs[0].Address, tx.Outputs[0].Address)
|
||||
|
||||
// Write request
|
||||
|
@ -146,7 +146,7 @@ func TestSequence(t *testing.T) {
|
|||
// Sign request
|
||||
signBytes := tx.SignBytes(chainID)
|
||||
sig := privAccountA.Sign(signBytes)
|
||||
tx.Inputs[0].Signature = crypto.SignatureS{sig}
|
||||
tx.Inputs[0].Signature = crypto.WrapSignature(sig)
|
||||
// t.Log("ADDR: %X -> %X\n", tx.Inputs[0].Address, tx.Outputs[0].Address)
|
||||
|
||||
// Write request
|
||||
|
|
|
@ -12,9 +12,9 @@ import (
|
|||
func PrivAccountFromSecret(secret string) PrivAccount {
|
||||
privKey := crypto.GenPrivKeyEd25519FromSecret([]byte(secret))
|
||||
privAccount := PrivAccount{
|
||||
PrivKeyS: crypto.PrivKeyS{privKey},
|
||||
PrivKeyS: crypto.WrapPrivKey(privKey),
|
||||
Account: Account{
|
||||
PubKey: crypto.PubKeyS{privKey.PubKey()},
|
||||
PubKey: crypto.WrapPubKey(privKey.PubKey()),
|
||||
},
|
||||
}
|
||||
return privAccount
|
||||
|
@ -31,9 +31,9 @@ func RandAccounts(num int, minAmount int64, maxAmount int64) []PrivAccount {
|
|||
}
|
||||
|
||||
privKey := crypto.GenPrivKeyEd25519()
|
||||
pubKey := crypto.PubKeyS{privKey.PubKey()}
|
||||
pubKey := crypto.WrapPubKey(privKey.PubKey())
|
||||
privAccs[i] = PrivAccount{
|
||||
PrivKeyS: crypto.PrivKeyS{privKey},
|
||||
PrivKeyS: crypto.WrapPrivKey(privKey),
|
||||
Account: Account{
|
||||
PubKey: pubKey,
|
||||
Balance: Coins{Coin{"", balance}},
|
||||
|
|
30
types/tx.go
30
types/tx.go
|
@ -104,13 +104,7 @@ func NewTxInput(pubKey crypto.PubKey, coins Coins, sequence int) TxInput {
|
|||
Sequence: sequence,
|
||||
}
|
||||
if sequence == 1 {
|
||||
// safely wrap if needed
|
||||
// TODO: extract this as utility function?
|
||||
ps, ok := pubKey.(crypto.PubKeyS)
|
||||
if !ok {
|
||||
ps = crypto.PubKeyS{pubKey}
|
||||
}
|
||||
input.PubKey = ps
|
||||
input.PubKey = crypto.WrapPubKey(pubKey)
|
||||
}
|
||||
return input
|
||||
}
|
||||
|
@ -151,25 +145,21 @@ type SendTx struct {
|
|||
func (tx *SendTx) SignBytes(chainID string) []byte {
|
||||
signBytes := wire.BinaryBytes(chainID)
|
||||
sigz := make([]crypto.Signature, len(tx.Inputs))
|
||||
for i, input := range tx.Inputs {
|
||||
sigz[i] = input.Signature.Signature
|
||||
tx.Inputs[i].Signature.Signature = nil
|
||||
for i := range tx.Inputs {
|
||||
sigz[i] = tx.Inputs[i].Signature
|
||||
tx.Inputs[i].Signature = crypto.Signature{}
|
||||
}
|
||||
signBytes = append(signBytes, wire.BinaryBytes(tx)...)
|
||||
for i := range tx.Inputs {
|
||||
tx.Inputs[i].Signature.Signature = sigz[i]
|
||||
tx.Inputs[i].Signature = sigz[i]
|
||||
}
|
||||
return signBytes
|
||||
}
|
||||
|
||||
func (tx *SendTx) SetSignature(addr []byte, sig crypto.Signature) bool {
|
||||
sigs, ok := sig.(crypto.SignatureS)
|
||||
if !ok {
|
||||
sigs = crypto.SignatureS{sig}
|
||||
}
|
||||
for i, input := range tx.Inputs {
|
||||
if bytes.Equal(input.Address, addr) {
|
||||
tx.Inputs[i].Signature = sigs
|
||||
tx.Inputs[i].Signature = crypto.WrapSignature(sig)
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
@ -193,18 +183,14 @@ type AppTx struct {
|
|||
func (tx *AppTx) SignBytes(chainID string) []byte {
|
||||
signBytes := wire.BinaryBytes(chainID)
|
||||
sig := tx.Input.Signature
|
||||
tx.Input.Signature.Signature = nil
|
||||
tx.Input.Signature = crypto.WrapSignature(nil)
|
||||
signBytes = append(signBytes, wire.BinaryBytes(tx)...)
|
||||
tx.Input.Signature = sig
|
||||
return signBytes
|
||||
}
|
||||
|
||||
func (tx *AppTx) SetSignature(sig crypto.Signature) bool {
|
||||
sigs, ok := sig.(crypto.SignatureS)
|
||||
if !ok {
|
||||
sigs = crypto.SignatureS{sig}
|
||||
}
|
||||
tx.Input.Signature = sigs
|
||||
tx.Input.Signature = crypto.WrapSignature(sig)
|
||||
return true
|
||||
}
|
||||
|
||||
|
|
|
@ -109,7 +109,7 @@ func TestSendTxJSON(t *testing.T) {
|
|||
sig := test1PrivAcc.Sign(signBytes)
|
||||
// we handle both raw sig and wrapped sig the same
|
||||
tx.SetSignature(test1PrivAcc.PubKey.Address(), sig)
|
||||
tx2.SetSignature(test1PrivAcc.PubKey.Address(), crypto.SignatureS{sig})
|
||||
tx2.SetSignature(test1PrivAcc.PubKey.Address(), crypto.WrapSignature(sig))
|
||||
assert.Equal(tx, tx2)
|
||||
|
||||
// let's marshal / unmarshal this with signature
|
||||
|
|
Loading…
Reference in New Issue