Skip to content
Snippets Groups Projects
Commit 9d78a9dd authored by lunfardo314's avatar lunfardo314
Browse files

- exported NewBLSignature

- addded Transaction.PutSignature
parent 37400faa
No related branches found
No related tags found
No related merge requests found
...@@ -95,7 +95,7 @@ func (sigscheme *blsSignatureScheme) Sign(data []byte) Signature { ...@@ -95,7 +95,7 @@ func (sigscheme *blsSignatureScheme) Sign(data []byte) Signature {
if err != nil { if err != nil {
panic(err) panic(err)
} }
return newBLSSignature(pubKeyBin, sig) return NewBLSSignature(pubKeyBin, sig)
} }
func (sigscheme *blsSignatureScheme) String() string { func (sigscheme *blsSignatureScheme) String() string {
...@@ -136,7 +136,7 @@ func BLSSignatureFromBytes(data []byte) (result *BLSSignature, consumedBytes int ...@@ -136,7 +136,7 @@ func BLSSignatureFromBytes(data []byte) (result *BLSSignature, consumedBytes int
return return
} }
func newBLSSignature(pubKey, signature []byte) *BLSSignature { func NewBLSSignature(pubKey, signature []byte) *BLSSignature {
var ret BLSSignature var ret BLSSignature
ret[0] = address.VersionBLS ret[0] = address.VersionBLS
copy(ret.pubKey(), pubKey) copy(ret.pubKey(), pubKey)
...@@ -223,7 +223,7 @@ func AggregateBLSSignatures(sigs ...Signature) (Signature, error) { ...@@ -223,7 +223,7 @@ func AggregateBLSSignatures(sigs ...Signature) (Signature, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
return newBLSSignature(pubKeyBin, sigBin), nil return NewBLSSignature(pubKeyBin, sigBin), nil
} }
// interface contract (allow the compiler to check if the implementation has all of the required methods). // interface contract (allow the compiler to check if the implementation has all of the required methods).
......
package transaction package transaction
import ( import (
"errors"
"fmt" "fmt"
"sync" "sync"
...@@ -279,6 +280,16 @@ func (transaction *Transaction) Sign(signature signaturescheme.SignatureScheme) ...@@ -279,6 +280,16 @@ func (transaction *Transaction) Sign(signature signaturescheme.SignatureScheme)
return transaction return transaction
} }
// PutSignature validates and adds signature to the transaction
func (transaction *Transaction) PutSignature(signature signaturescheme.Signature) error {
if !signature.IsValid(transaction.EssenceBytes()) {
return errors.New("PutSignature: invalid signature")
}
transaction.signatures.Add(signature.Address(), signature)
return nil
}
// String returns a human readable version of this Transaction (for debug purposes). // String returns a human readable version of this Transaction (for debug purposes).
func (transaction *Transaction) String() string { func (transaction *Transaction) String() string {
id := transaction.ID() id := transaction.ID()
......
...@@ -124,3 +124,69 @@ func TestMarshalingDataPayload(t *testing.T) { ...@@ -124,3 +124,69 @@ func TestMarshalingDataPayload(t *testing.T) {
assert.Equal(t, true, bytes.Equal(tx1.ID().Bytes(), tx.ID().Bytes())) assert.Equal(t, true, bytes.Equal(tx1.ID().Bytes(), tx.ID().Bytes()))
} }
func TestPutSignatureValid(t *testing.T) {
sigScheme := signaturescheme.RandBLS()
addr := sigScheme.Address()
o1 := NewOutputID(addr, RandomID())
inputs := NewInputs(o1)
bal := balance.New(balance.ColorIOTA, 1)
outputs := NewOutputs(map[address.Address][]*balance.Balance{addr: {bal}})
tx := New(inputs, outputs)
dataPayload := []byte("data payload test")
err := tx.SetDataPayload(dataPayload)
assert.Equal(t, nil, err)
signature := sigScheme.Sign(tx.EssenceBytes())
assert.Equal(t, signature.IsValid(tx.EssenceBytes()), true)
err = tx.PutSignature(signature)
assert.Equal(t, nil, err)
check := tx.SignaturesValid()
assert.Equal(t, true, check)
}
func TestPutSignatureInvalid(t *testing.T) {
sigScheme := signaturescheme.RandBLS()
addr := sigScheme.Address()
o1 := NewOutputID(addr, RandomID())
inputs := NewInputs(o1)
bal := balance.New(balance.ColorIOTA, 1)
outputs := NewOutputs(map[address.Address][]*balance.Balance{addr: {bal}})
tx := New(inputs, outputs)
dataPayload := []byte("data payload test")
err := tx.SetDataPayload(dataPayload)
assert.Equal(t, nil, err)
signatureValid := sigScheme.Sign(tx.EssenceBytes())
assert.Equal(t, true, signatureValid.IsValid(tx.EssenceBytes()))
sigBytes := make([]byte, len(signatureValid.Bytes()))
copy(sigBytes, signatureValid.Bytes())
// inverse last byte --> corrupt the signatureValid
sigBytes[len(sigBytes)-1] = sigBytes[len(sigBytes)-1] ^ sigBytes[len(sigBytes)-1]
sigCorrupted, consumed, err := signaturescheme.BLSSignatureFromBytes(sigBytes)
assert.Equal(t, nil, err)
assert.Equal(t, consumed, len(sigBytes))
assert.Equal(t, false, sigCorrupted.IsValid(tx.EssenceBytes()))
err = tx.PutSignature(sigCorrupted)
// error expected
assert.Equal(t, true, err != nil)
// 0 signatures is not valid
assert.Equal(t, true, !tx.SignaturesValid())
err = tx.PutSignature(signatureValid)
// no error expected
assert.Equal(t, nil, err)
// valid signatures expected
assert.Equal(t, true, tx.SignaturesValid())
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment