Skip to content

Commit

Permalink
Merge pull request #2195 from dedis/warn_signature_2
Browse files Browse the repository at this point in the history
ClientTransaction version warning
  • Loading branch information
Gaylor Bosson authored Feb 5, 2020
2 parents 0a5c6bf + f40a806 commit d9a2f99
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 110 deletions.
8 changes: 8 additions & 0 deletions byzcoin/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,14 @@ func (c *Client) AddTransactionAndWait(tx ClientTransaction, wait int) (*AddTxRe
}
}

for _, inst := range tx.Instructions {
if inst.version != CurrentVersion {
return nil, xerrors.New(
"got instruction with wrong version - please use byzcoin." +
"NewClientTransaction")
}
}

// As we fetch the genesis if required, this will never be
// nil but either the genesis or the latest.
latest := c.getLatestKnownBlock()
Expand Down
16 changes: 8 additions & 8 deletions byzcoin/contract_darc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestSecureDarc(t *testing.T) {
invokeEvolve := darc.Action("invoke:" + ContractDarcID + "." + cmdDarcEvolve)
invokeEvolveUnrestricted := darc.Action("invoke:" + ContractDarcID + "." + cmdDarcEvolveUnrestriction)

log.Info("spawn a new secure darc with spawn:insecure_darc - fail")
log.Lvl1("spawn a new secure darc with spawn:insecure_darc - fail")
secDarc := gDarc.Copy()
require.NoError(t, secDarc.Rules.AddRule("spawn:insecure_darc", []byte(restrictedSigner.Identity().String())))
secDarcBuf, err := secDarc.ToProto()
Expand All @@ -53,7 +53,7 @@ func TestSecureDarc(t *testing.T) {
_, err = cl.AddTransactionAndWait(ctx, 10)
require.Error(t, err)

log.Info("do the same but without spawn:insecure_darc - pass")
log.Lvl1("do the same but without spawn:insecure_darc - pass")
require.NoError(t, secDarc.Rules.DeleteRules("spawn:insecure_darc"))
require.NoError(t, secDarc.Rules.UpdateRule(invokeEvolve, []byte(restrictedSigner.Identity().String())))
require.NoError(t, secDarc.Rules.UpdateRule(invokeEvolveUnrestricted, []byte(unrestrictedSigner.Identity().String())))
Expand All @@ -75,7 +75,7 @@ func TestSecureDarc(t *testing.T) {
_, err = cl.AddTransactionAndWait(ctx, 10)
require.NoError(t, err)

log.Info("spawn a darc with a version > 0 - fail")
log.Lvl1("spawn a darc with a version > 0 - fail")
secDarc.Version = 1
secDarcBuf, err = secDarc.ToProto()
ctx, err = cl.CreateTransaction(Instruction{
Expand All @@ -95,7 +95,7 @@ func TestSecureDarc(t *testing.T) {
require.Error(t, err)

secDarc.Version = 0
log.Info("evolve to add rules - fail")
log.Lvl1("evolve to add rules - fail")
{
secDarc2 := secDarc.Copy()
require.NoError(t, secDarc2.EvolveFrom(secDarc))
Expand All @@ -119,7 +119,7 @@ func TestSecureDarc(t *testing.T) {
require.Error(t, err)
}

log.Info("evolve to modify the unrestrict_evolve rule - fail")
log.Lvl1("evolve to modify the unrestrict_evolve rule - fail")
{
secDarc2 := secDarc.Copy()
require.NoError(t, secDarc2.EvolveFrom(secDarc))
Expand All @@ -146,7 +146,7 @@ func TestSecureDarc(t *testing.T) {

var barrier *skipchain.SkipBlock

log.Info("evolve to modify existing rules - pass")
log.Lvl1("evolve to modify existing rules - pass")
{
secDarc2 := secDarc.Copy()
require.NoError(t, secDarc2.EvolveFrom(secDarc))
Expand Down Expand Up @@ -180,7 +180,7 @@ func TestSecureDarc(t *testing.T) {
// should increase by one
require.Equal(t, myDarc.Version, gDarc.Version+1)

log.Info("evolve_unrestricted fails with the wrong signer")
log.Lvl1("evolve_unrestricted fails with the wrong signer")
{
myDarc2 := myDarc.Copy()
require.NoError(t, myDarc2.EvolveFrom(&myDarc))
Expand All @@ -204,7 +204,7 @@ func TestSecureDarc(t *testing.T) {
require.Error(t, err)
}

log.Info("evolve_unrestricted to add rules - pass")
log.Lvl1("evolve_unrestricted to add rules - pass")
{
myDarc2 := myDarc.Copy()
require.NoError(t, myDarc2.EvolveFrom(&myDarc))
Expand Down
19 changes: 2 additions & 17 deletions byzcoin/contracts/coins_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ func TestCoin_Spawn(t *testing.T) {
SignerIdentities: []darc.Identity{gsigner.Identity()},
SignerCounter: []uint64{1},
}
dummyCtxHash := []byte("dummy_ctx_hash")
err := inst.SignWith(dummyCtxHash, gsigner)
require.Nil(t, err)

// Spawn cannot use getContract because the instance ID is the genesis darc.
// We need a zero contractCoin instead.
Expand Down Expand Up @@ -88,10 +85,8 @@ func TestCoin_InvokeMint(t *testing.T) {
SignerIdentities: []darc.Identity{gsigner.Identity()},
SignerCounter: []uint64{1},
}
dummyCtxHash := []byte("dummy_ctx_hash")
err := inst.SignWith(dummyCtxHash, gsigner)
require.Nil(t, err)
_, _, err = ct.getContract(inst.InstanceID).Invoke(ct, inst, []byzcoin.Coin{})
_, _, err := ct.getContract(inst.InstanceID).Invoke(ct, inst,
[]byzcoin.Coin{})
require.Error(t, err)

inst = byzcoin.Instruction{
Expand All @@ -103,8 +98,6 @@ func TestCoin_InvokeMint(t *testing.T) {
SignerIdentities: []darc.Identity{gsigner.Identity()},
SignerCounter: []uint64{1},
}
err = inst.SignWith(dummyCtxHash, gsigner)
require.Nil(t, err)

sc, co, err := ct.getContract(inst.InstanceID).Invoke(ct, inst, []byzcoin.Coin{})
require.Nil(t, err)
Expand Down Expand Up @@ -136,8 +129,6 @@ func TestCoin_InvokeOverflow(t *testing.T) {
SignerIdentities: []darc.Identity{gsigner.Identity()},
SignerCounter: []uint64{1},
}
dummyCtxHash := []byte("dummy_ctx_hash")
require.Nil(t, inst.SignWith(dummyCtxHash, gsigner))

sc, co, err := ct.getContract(inst.InstanceID).Invoke(ct, inst, []byzcoin.Coin{})
require.Error(t, err)
Expand All @@ -162,8 +153,6 @@ func TestCoin_InvokeStoreFetch(t *testing.T) {
SignerIdentities: []darc.Identity{gsigner.Identity()},
SignerCounter: []uint64{1},
}
dummyCtxHash := []byte("dummy_ctx_hash")
require.Nil(t, inst.SignWith(dummyCtxHash, gsigner))

c1 := byzcoin.Coin{Name: CoinName, Value: 1}
notOlCoin := iid("notOlCoin")
Expand All @@ -186,7 +175,6 @@ func TestCoin_InvokeStoreFetch(t *testing.T) {
SignerIdentities: []darc.Identity{gsigner.Identity()},
SignerCounter: []uint64{1},
}
require.Nil(t, inst.SignWith(dummyCtxHash, gsigner))

// Try once with not enough coins available.
sc, co, err = ct.getContract(inst.InstanceID).Invoke(ct, inst, nil)
Expand Down Expand Up @@ -232,8 +220,6 @@ func TestCoin_InvokeTransfer(t *testing.T) {
SignerCounter: []uint64{1},
}

dummyCtxHash := []byte("dummy_ctx_hash")
require.Nil(t, inst.SignWith(dummyCtxHash, gsigner))
sc, co, err := ct.getContract(inst.InstanceID).Invoke(ct, inst, []byzcoin.Coin{})
require.Error(t, err)

Expand All @@ -250,7 +236,6 @@ func TestCoin_InvokeTransfer(t *testing.T) {
SignerCounter: []uint64{1},
}

require.Nil(t, inst.SignWith(dummyCtxHash, gsigner))
sc, co, err = ct.getContract(inst.InstanceID).Invoke(ct, inst, []byzcoin.Coin{})
require.Nil(t, err)
require.Equal(t, 0, len(co))
Expand Down
35 changes: 14 additions & 21 deletions byzcoin/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,7 @@ func TestService_AddTransaction_ValidInvalid(t *testing.T) {
instr2 := createSpawnInstr(s.darc.GetBaseID(), dummyContract, "data", dcID)
instr2.SignerIdentities = []darc.Identity{s.signer.Identity()}
instr2.SignerCounter = []uint64{3}
tx2 := ClientTransaction{
Instructions: []Instruction{instr1, instr2},
}
tx2 := NewClientTransaction(CurrentVersion, instr1, instr2)
h := tx2.Instructions.Hash()
for i := range tx2.Instructions {
err := tx2.Instructions[i].SignWith(h, s.signer)
Expand All @@ -390,9 +388,7 @@ func TestService_AddTransaction_ValidInvalid(t *testing.T) {
instr2 = createSpawnInstr(s.darc.GetBaseID(), dummyContract, "data", dcID2)
instr2.SignerCounter = []uint64{3}
instr2.SignerIdentities = []darc.Identity{s.signer.Identity()}
tx3 := ClientTransaction{
Instructions: []Instruction{instr1, instr2},
}
tx3 := NewClientTransaction(CurrentVersion, instr1, instr2)
tx3.SignWith(s.signer)
atx = &AddTxRequest{
Version: CurrentVersion,
Expand Down Expand Up @@ -664,18 +660,16 @@ func TestService_DarcProxy(t *testing.T) {

// now set the signer with the correct callback
signer = darc.NewSignerProxy(email, ed.Public, ga)
ctx := ClientTransaction{
Instructions: []Instruction{
{
InstanceID: NewInstanceID(d2.GetBaseID()),
Spawn: &Spawn{
ContractID: "dummy",
Args: Arguments{{Name: "data", Value: []byte("nothing in particular")}},
},
SignerCounter: []uint64{1},
ctx := NewClientTransaction(CurrentVersion,
Instruction{
InstanceID: NewInstanceID(d2.GetBaseID()),
Spawn: &Spawn{
ContractID: "dummy",
Args: Arguments{{Name: "data", Value: []byte("nothing in particular")}},
},
SignerCounter: []uint64{1},
},
}
)

err = ctx.FillSignersAndSignWith(signer)
require.NoError(t, err)
Expand All @@ -687,7 +681,6 @@ func TestService_DarcProxy(t *testing.T) {
InclusionWait: 10,
})
transactionOK(t, resp, err)
require.NoError(t, err)
}

func TestService_WrongSigner(t *testing.T) {
Expand Down Expand Up @@ -1436,8 +1429,8 @@ func TestService_DarcSpawn(t *testing.T) {
require.NoError(t, err)
require.True(t, darc2.Equal(darc2Copy))

ctx := ClientTransaction{
Instructions: []Instruction{{
ctx := NewClientTransaction(CurrentVersion,
Instruction{
InstanceID: NewInstanceID(s.darc.GetBaseID()),
Spawn: &Spawn{
ContractID: ContractDarcID,
Expand All @@ -1448,8 +1441,8 @@ func TestService_DarcSpawn(t *testing.T) {
},
SignerIdentities: []darc.Identity{s.signer.Identity()},
SignerCounter: []uint64{1},
}},
}
},
)
require.Nil(t, ctx.Instructions[0].SignWith(ctx.Instructions.Hash(), s.signer))

s.sendTx(t, ctx)
Expand Down
6 changes: 6 additions & 0 deletions byzcoin/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ func (ctx *ClientTransaction) SignWith(signers ...darc.Signer) error {

// NewClientTransaction creates a transaction compatible with the version passed
// in arguments. Depending on the version, the hash will have a different value.
// Most common usage is:
// byzcoin.NewClientTransaction(byzcoin.CurrentVersion, instr...)
func NewClientTransaction(v Version, instrs ...Instruction) ClientTransaction {
ctx := ClientTransaction{Instructions: instrs}
ctx.Instructions.SetVersion(v)
Expand Down Expand Up @@ -298,6 +300,10 @@ func (instr *Instruction) SignWith(msg []byte, signers ...darc.Signer) error {
if len(signers) != len(instr.SignerCounter) {
return xerrors.New("the number of signers does not match the number of counters")
}
if instr.version != CurrentVersion {
return xerrors.New("cannot sign previous versions - please use" +
" byzcoin.NewClientTransaction")
}
instr.Signatures = make([][]byte, len(signers))
for i := range signers {
signerID := signers[i].Identity()
Expand Down
4 changes: 1 addition & 3 deletions byzcoin/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,7 @@ func combineInstrsAndSign(signer darc.Signer, instrs ...Instruction) (ClientTran
for i := range instrs {
instrs[i].SignerIdentities = []darc.Identity{signer.Identity()}
}
t := ClientTransaction{
Instructions: instrs,
}
t := NewClientTransaction(CurrentVersion, instrs...)
h := t.Instructions.Hash()
for i := range t.Instructions {
if err := t.Instructions[i].SignWith(h, signer); err != nil {
Expand Down
28 changes: 13 additions & 15 deletions calypso/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ func (c *Client) CreateLTS(ltsRoster *onet.Roster, darcID darc.ID, signers []dar
},
SignerCounter: counters,
}
tx := byzcoin.ClientTransaction{
Instructions: []byzcoin.Instruction{inst},
}
tx := byzcoin.NewClientTransaction(byzcoin.CurrentVersion, inst)
if err := tx.FillSignersAndSignWith(signers...); err != nil {
return nil, xerrors.Errorf("signing txn: %v", err)
}
Expand Down Expand Up @@ -167,17 +165,17 @@ func (c *Client) AddWrite(write *Write, signer darc.Signer, signerCtr uint64,
if err != nil {
return nil, xerrors.Errorf("encoding Write message: %v", err)
}
ctx := byzcoin.ClientTransaction{
Instructions: byzcoin.Instructions{{
ctx := byzcoin.NewClientTransaction(byzcoin.CurrentVersion,
byzcoin.Instruction{
InstanceID: byzcoin.NewInstanceID(darc.GetBaseID()),
Spawn: &byzcoin.Spawn{
ContractID: ContractWriteID,
Args: byzcoin.Arguments{{
Name: "write", Value: writeBuf}},
},
SignerCounter: []uint64{signerCtr},
}},
}
},
)
//Sign the transaction
err = ctx.FillSignersAndSignWith(signer)
if err != nil {
Expand Down Expand Up @@ -216,16 +214,16 @@ func (c *Client) AddRead(proof *byzcoin.Proof, signer darc.Signer, signerCtr uin
return nil, xerrors.Errorf("encoding Read message: %v", err)
}

ctx := byzcoin.ClientTransaction{
Instructions: byzcoin.Instructions{{
ctx := byzcoin.NewClientTransaction(byzcoin.CurrentVersion,
byzcoin.Instruction{
InstanceID: byzcoin.NewInstanceID(proof.InclusionProof.Key()),
Spawn: &byzcoin.Spawn{
ContractID: ContractReadID,
Args: byzcoin.Arguments{{Name: "read", Value: readBuf}},
},
SignerCounter: []uint64{signerCtr},
}},
}
},
)
err = ctx.FillSignersAndSignWith(signer)
if err != nil {
return nil, xerrors.Errorf("signing txn: %v", err)
Expand Down Expand Up @@ -258,8 +256,8 @@ func (c *Client) SpawnDarc(signer darc.Signer, signerCtr uint64,
return nil, xerrors.Errorf("serializing darc to protobuf: %v", err)
}

ctx := byzcoin.ClientTransaction{
Instructions: []byzcoin.Instruction{{
ctx := byzcoin.NewClientTransaction(byzcoin.CurrentVersion,
byzcoin.Instruction{
InstanceID: byzcoin.NewInstanceID(controlDarc.GetBaseID()),
Spawn: &byzcoin.Spawn{
ContractID: byzcoin.ContractDarcID,
Expand All @@ -269,8 +267,8 @@ func (c *Client) SpawnDarc(signer darc.Signer, signerCtr uint64,
}},
},
SignerCounter: []uint64{signerCtr},
}},
}
},
)
err = ctx.FillSignersAndSignWith(signer)
if err != nil {
return nil, xerrors.Errorf("signing txn: %v", err)
Expand Down
4 changes: 1 addition & 3 deletions calypso/csadmin/clicontracts/lts.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ func LTSSpawn(c *cli.Context) error {
SignerCounter: []uint64{counters.Counters[0] + 1},
}

tx := byzcoin.ClientTransaction{
Instructions: []byzcoin.Instruction{inst},
}
tx := byzcoin.NewClientTransaction(byzcoin.CurrentVersion, inst)

err = tx.FillSignersAndSignWith(*signer)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions calypso/csadmin/clicontracts/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,16 @@ func ReadSpawn(c *cli.Context) error {
return xerrors.Errorf("failed to get the signer counters: %v", err)
}

ctx := byzcoin.ClientTransaction{
Instructions: byzcoin.Instructions{{
ctx := byzcoin.NewClientTransaction(byzcoin.CurrentVersion,
byzcoin.Instruction{
InstanceID: byzcoin.NewInstanceID(proof.InclusionProof.Key()),
Spawn: &byzcoin.Spawn{
ContractID: calypso.ContractReadID,
Args: byzcoin.Arguments{{Name: "read", Value: readBuf}},
},
SignerCounter: []uint64{counters.Counters[0] + 1},
}},
}
},
)

err = ctx.FillSignersAndSignWith(*signer)
if err != nil {
Expand Down
Loading

0 comments on commit d9a2f99

Please sign in to comment.