Skip to content

Commit

Permalink
feat: Added Verify and VerifyTransaction to PublicKey
Browse files Browse the repository at this point in the history
  • Loading branch information
andrix10 committed Jan 7, 2021
1 parent 2a57b35 commit 7d1aaf4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
7 changes: 6 additions & 1 deletion account_delete_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ func TestAccountDeleteTransaction_Execute(t *testing.T) {
FreezeWith(client)
assert.NoError(t, err)

resp, err = tx.Sign(newKey).Execute(client)
tx = tx.Sign(newKey)

assert.True(t, newKey.PublicKey().VerifyTransaction(tx.Transaction))
assert.False(t, client.GetOperatorPublicKey().VerifyTransaction(tx.Transaction))

resp, err = tx.Execute(client)
assert.NoError(t, err)

_, err = resp.GetReceipt(client)
Expand Down
30 changes: 29 additions & 1 deletion crypto.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package hedera

import (
"bytes"
"crypto/hmac"
"crypto/rand"
"crypto/sha512"

"encoding/binary"
"encoding/hex"
"encoding/pem"
Expand Down Expand Up @@ -364,3 +364,31 @@ func (sk PrivateKey) SignTransaction(transaction Transaction) ([]byte, error) {

return signature, nil
}

func (pk PublicKey) Verify(message []byte, signature []byte) bool {
return ed25519.Verify(pk.Bytes(), message, signature)
}

func (pk PublicKey) VerifyTransaction(transaction Transaction) bool {
if len(transaction.signedTransactions) == 0 {
return false
}

for _, transaction := range transaction.signedTransactions {
found := false
for _, sigPair := range transaction.SigMap.GetSigPair() {
if bytes.Equal(sigPair.GetPubKeyPrefix(), pk.Bytes()) {
found = true
if !pk.Verify(transaction.BodyBytes, sigPair.GetEd25519()) {
return false
}
}
}

if !found {
return false
}
}

return true
}

0 comments on commit 7d1aaf4

Please sign in to comment.