Skip to content

Commit

Permalink
Expose some PublicKey methods (#1521)
Browse files Browse the repository at this point in the history
* expose some PublicKey methods

Signed-off-by: Petar Tonev <[email protected]>

* add unit tests

Signed-off-by: Petar Tonev <[email protected]>

---------

Signed-off-by: Petar Tonev <[email protected]>
  • Loading branch information
petreze authored Mar 21, 2023
1 parent bd667b7 commit 63d1ac2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/PublicKey.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,26 @@ export default class PublicKey extends Key {
return new PublicKey(cryptography.PublicKey.fromString(text));
}

/**
* Parse an ECDSA public key from a string of hexadecimal digits.
*
* @param {string} text
* @returns {PublicKey}
*/
static fromStringECDSA(text) {
return new PublicKey(cryptography.PublicKey.fromStringECDSA(text));
}

/**
* Parse an ED25519 public key from a string of hexadecimal digits.
*
* @param {string} text
* @returns {PublicKey}
*/
static fromStringED25519(text) {
return new PublicKey(cryptography.PublicKey.fromStringED25519(text));
}

/**
* Verify a signature on a message with this public key.
*
Expand Down
19 changes: 19 additions & 0 deletions test/unit/PublicKey.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
AccountId,
PrivateKey,
PublicKey,
TransactionId,
TransferTransaction,
Transaction,
Expand Down Expand Up @@ -55,4 +56,22 @@ describe("PublicKey", function () {
}
}
});

it("verify `fromStringECDSA()` works", async function () {
const ecdsaStringKey =
"302d300706052b8104000a0322000245f219afdfc8e61b9f6879fa7c8a16a94b35471662afa302993d7d9a29564f81";
const publicKeyECDSA = PublicKey.fromStringECDSA(ecdsaStringKey);

expect(publicKeyECDSA.toString()).to.be.equal(ecdsaStringKey);
});

it("verify `fromStringED25519()` works", async function () {
const ed25519StringKey =
"302a300506032b6570032100bc46c36d8aeb94270064edb8d3d4d5d29446e1bb2f36cc47b2c9b755ef0aac25";
const publicKeyED25519 = PublicKey.fromStringED25519(
"302a300506032b6570032100bc46c36d8aeb94270064edb8d3d4d5d29446e1bb2f36cc47b2c9b755ef0aac25"
);

expect(publicKeyED25519.toString()).to.be.equal(ed25519StringKey);
});
});

0 comments on commit 63d1ac2

Please sign in to comment.