Skip to content

Commit

Permalink
core/asset, core/signers: correctly record xpubs for a signer with mu…
Browse files Browse the repository at this point in the history
…ltiple keys; avoid Go loop pitfall

This is #716 +
#717.

Closes #718
  • Loading branch information
bobg authored and iampogo committed Mar 7, 2017
1 parent 461fdc0 commit c70bfd2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
1 change: 1 addition & 0 deletions core/asset/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func Annotated(a *Asset) (*query.AnnotatedAsset, error) {
pubkeys, quorum, err := vmutil.ParseP2SPMultiSigProgram(a.IssuanceProgram)
if err == nil {
for _, pubkey := range pubkeys {
pubkey := pubkey
aa.Keys = append(aa.Keys, &query.AssetKey{
AssetPubkey: chainjson.HexBytes(pubkey[:]),
})
Expand Down
1 change: 1 addition & 0 deletions core/signers/signers.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func Create(ctx context.Context, db pg.DB, typ string, xpubs []chainkd.XPub, quo

var xpubBytes [][]byte
for _, key := range xpubs {
key := key
xpubBytes = append(xpubBytes, key[:])
}

Expand Down
42 changes: 38 additions & 4 deletions core/signers/signers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,45 @@ func TestCreate(t *testing.T) {
want: nil,
}}

for _, c := range cases {
_, got := Create(ctx, db, c.typ, c.xpubs, c.quorum, "")
for i, c := range cases {
s, gotErr := Create(ctx, db, c.typ, c.xpubs, c.quorum, "")

if errors.Root(got) != c.want {
t.Errorf("Create(%s, %v, %d) = %q want %q", c.typ, c.xpubs, c.quorum, errors.Root(got), c.want)
if errors.Root(gotErr) != c.want {
t.Errorf("case %d: Create(%s, %v, %d) = %q want %q", i, c.typ, c.xpubs, c.quorum, errors.Root(gotErr), c.want)
continue
}
if c.want != nil {
continue
}

id := s.ID
var err error
s, err = Find(ctx, db, c.typ, id)
if err != nil {
t.Errorf("case %d: cannot Find new signer %s", i, id)
continue
}
if len(s.XPubs) != len(c.xpubs) {
t.Errorf("case %d: signer created with %d xpub(s) now has %d xpub(s)", i, len(c.xpubs), len(s.XPubs))
continue
}
for _, key1 := range c.xpubs {
var found bool
for _, key2 := range s.XPubs {
if key1 == key2 {
found = true
break
}
}
if !found {
t.Errorf("case %d: list of xpubs mismatch", i)
for j, key := range c.xpubs {
t.Logf("want %d: %x", j, key[:])
}
for j, key := range s.XPubs {
t.Logf("got %d: %x", j, key[:])
}
}
}
}
}
Expand Down

0 comments on commit c70bfd2

Please sign in to comment.