Skip to content

Commit

Permalink
fix: PrivateKey now implements the Key interface
Browse files Browse the repository at this point in the history
  • Loading branch information
andrix10 committed Nov 17, 2020
1 parent 1cb80f4 commit 6709420
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
4 changes: 4 additions & 0 deletions crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,10 @@ func (pk PublicKey) Bytes() []byte {
return pk.keyData
}

func (sk PrivateKey) toProtoKey() *proto.Key {
return sk.PublicKey().toProtoKey()
}

func (pk PublicKey) toProtoKey() *proto.Key {
return &proto.Key{Key: &proto.Key_Ed25519{Ed25519: pk.keyData}}
}
Expand Down
37 changes: 37 additions & 0 deletions crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,40 @@ func TestPrivateKey_FromPemWithPassphrase(t *testing.T) {

assert.Equal(t, actualPrivateKey, privateKey)
}

func TestSetKeyUsesAnyKey(t *testing.T) {
client := newTestClient(t)

newKey, err := GeneratePrivateKey()
assert.NoError(t, err)

newBalance := NewHbar(1)

assert.Equal(t, HbarUnits.Hbar.numberOfTinybar(), newBalance.tinybar)

keys := make([]PrivateKey, 3)
pubKeys := make([]PublicKey, 3)

for i := range keys {
newKey, err := GeneratePrivateKey()
if err != nil {
panic(err)
}

keys[i] = newKey
pubKeys[i] = newKey.PublicKey()
}

thresholdKey := KeyListWithThreshold(2).
AddAllPublicKeys(pubKeys)

_, err = NewAccountCreateTransaction().
SetKey(newKey.PublicKey()).
SetKey(newKey).
SetKey(thresholdKey).
SetMaxTransactionFee(NewHbar(2)).
SetInitialBalance(newBalance).
Execute(client)
assert.NoError(t, err)

}

0 comments on commit 6709420

Please sign in to comment.