Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose some PublicKey methods #1521

Merged
merged 2 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});