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

KeyList now handles single key properly as first parameter #1348

Merged
merged 1 commit into from
Dec 5, 2022
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
8 changes: 7 additions & 1 deletion packages/cryptography/src/KeyList.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ export default class KeyList extends Key {
* @private
* @type {Key[]}
*/
this._keys = keys == null ? [] : keys;
// @ts-ignore
if (keys == null) this._keys = [];
//checks if the value for `keys` is passed as a single key
//rather than a list that contains just one key
else if (keys instanceof Key) this._keys = [keys];
else this._keys = keys;

/**
* @type {?number}
Expand Down Expand Up @@ -103,6 +108,7 @@ export default class KeyList extends Key {
* @returns {Key[]}
*/
toArray() {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return this._keys.slice();
}

Expand Down
2 changes: 1 addition & 1 deletion packages/cryptography/src/primitive/bip39.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as pbkdf2 from "./pbkdf2.js";
*/
export async function toSeed(words, passphrase) {
const input = words.join(" ");
const salt = (`mnemonic${passphrase}`).normalize("NFKD");
const salt = `mnemonic${passphrase}`.normalize("NFKD");

return pbkdf2.deriveKey(hmac.HashAlgorithm.Sha512, input, salt, 2048, 64);
}
14 changes: 8 additions & 6 deletions packages/cryptography/test/unit/bip39.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe("bip39", function () {
);
});

it("mnemonic passphrase NFKD normalization compliant test", async function (){
it("mnemonic passphrase NFKD normalization compliant test", async function () {
const words = [
"inmate",
"flip",
Expand All @@ -51,13 +51,15 @@ describe("bip39", function () {
"replace",
"reduce",
"plate",
"home"
"home",
];

const unicodePassphrase = "\u0070\u0061\u0073\u0073\u0070\u0068\u0072\u0061\u0073\u0065";
const expectedPrivateKey = "1ed95521b3406aa1e34db78be696db32f09d9f8ec3115fc12314082a44a3e8d6d4551a1905758b45bc315430f7d9c095da93645f1b0004c393370e0a878dfd4c";

const seed = await bip39.toSeed(words, unicodePassphrase)
const unicodePassphrase =
"\u0070\u0061\u0073\u0073\u0070\u0068\u0072\u0061\u0073\u0065";
const expectedPrivateKey =
"1ed95521b3406aa1e34db78be696db32f09d9f8ec3115fc12314082a44a3e8d6d4551a1905758b45bc315430f7d9c095da93645f1b0004c393370e0a878dfd4c";

const seed = await bip39.toSeed(words, unicodePassphrase);

expect(hex.encode(seed)).to.be.equal(expectedPrivateKey);
});
Expand Down
9 changes: 8 additions & 1 deletion src/KeyList.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ export default class KeyList extends Key {
* @private
* @type {Key[]}
*/
this._keys = keys == null ? [] : keys;
// @ts-ignore
if (keys == null) this._keys = [];
//checks if the value for `keys` is passed as a single key
//rather than a list that contains just one key
else if (keys instanceof Key) this._keys = [keys];
else this._keys = keys;

/**
* @type {?number}
Expand Down Expand Up @@ -131,6 +136,7 @@ export default class KeyList extends Key {
* @returns {Key[]}
*/
toArray() {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return this._keys.slice();
}

Expand All @@ -148,6 +154,7 @@ export default class KeyList extends Key {
* @returns {HashgraphProto.proto.IKey}
*/
_toProtobufKey() {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-return
const keys = this._keys.map((key) => key._toProtobufKey());

if (this.threshold == null) {
Expand Down
30 changes: 30 additions & 0 deletions test/integration/AccountCreateIntegrationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
PrivateKey,
Status,
TransactionId,
KeyList,
} from "../../src/exports.js";
import IntegrationTestEnv from "./client/NodeIntegrationTestEnv.js";

Expand Down Expand Up @@ -202,6 +203,35 @@ describe("AccountCreate", function () {
await (await transaction.execute(env.client)).getReceipt(env.client);
});

it("should create account with a single key passed to `KeyList`", async function() {
const env = await IntegrationTestEnv.new();
const publicKey = PrivateKey.generateED25519().publicKey;
const thresholdKey = new KeyList(publicKey, 1);

let transaction = new AccountCreateTransaction()
.setKey(thresholdKey)
.setInitialBalance(Hbar.fromTinybars(1))
.freezeWith(env.client);

const txAccountCreate = await transaction.execute(env.client);
const txAccountCreateReceipt = await txAccountCreate.getReceipt(
env.client
);
const accountId = txAccountCreateReceipt.accountId;

expect(accountId).to.not.be.null;

const info = await new AccountInfoQuery()
.setNodeAccountIds([txAccountCreate.nodeId])
.setAccountId(accountId)
.execute(env.client);

expect(info.accountId.toString()).to.be.equal(accountId.toString());
expect(info.key.toArray()[0].toString()).to.be.equal(
publicKey.toString()
);
});

after(async function () {
await env.close();
});
Expand Down