Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adonesky1 committed Nov 29, 2022
1 parent 6653a4f commit f51d40b
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 4 deletions.
7 changes: 3 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const { bytesToHex } = require('ethereum-cryptography/utils');
const {
stripHexPrefix,
privateToPublic,
bufferToHex,
publicToAddress,
ecsign,
arrToBufArr,
Expand Down Expand Up @@ -159,7 +158,7 @@ class HdKeyring {
// exportAccount should return a hex-encoded private key:
async exportAccount(address, opts = {}) {
const wallet = this._getWalletForAccount(address, opts);
return wallet.privateKey.toString('hex');
return bytesToHex(wallet.privKeyBytes);
}

// tx is an instance of the ethereumjs-transaction class.
Expand Down Expand Up @@ -214,7 +213,7 @@ class HdKeyring {
if (
!this._wallets
.map(({ publicKey }) =>
bufferToHex(publicToAddress(publicKey)).toLowerCase(),
this._AddressfromPublicKey(publicKey).toLowerCase(),
)
.includes(address.toLowerCase())
) {
Expand All @@ -223,7 +222,7 @@ class HdKeyring {

this._wallets = this._wallets.filter(
({ publicKey }) =>
bufferToHex(publicToAddress(publicKey)).toLowerCase() !==
this._AddressfromPublicKey(publicKey).toLowerCase() !==
address.toLowerCase(),
);
}
Expand Down
116 changes: 116 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -687,4 +687,120 @@ describe('hd-keyring', () => {
).rejects.toThrow('HD Keyring - Unable to find matching address.');
});
});

describe('#removeAccount', function () {
beforeEach(() => {
keyring = new HdKeyring({
mnemonic: sampleMnemonic,
numberOfAccounts: 1,
});
});

describe('if the account exists', function () {
it('should remove that account', async function () {
const addresses = await keyring.getAccounts();
expect(addresses).toHaveLength(1);
keyring.removeAccount(addresses[0]);
const addressesAfterRemoval = await keyring.getAccounts();
expect(addressesAfterRemoval).toHaveLength(0);
});
});

describe('if the account does not exist', function () {
it('should throw an error', function () {
const unexistingAccount = '0x0000000000000000000000000000000000000000';
expect(() => keyring.removeAccount(unexistingAccount)).toThrow(
`Address ${unexistingAccount} not found in this keyring`,
);
});
});
});

describe('getAppKeyAddress', function () {
beforeEach(() => {
keyring = new HdKeyring({
mnemonic: sampleMnemonic,
numberOfAccounts: 1,
});
});

it('should return a public address custom to the provided app key origin', async function () {
const appKeyAddress = await keyring.getAppKeyAddress(
firstAcct,
'someapp.origin.io',
);

expect(firstAcct).not.toBe(appKeyAddress);
expect(isValidAddress(appKeyAddress)).toBe(true);
});

it('should return different addresses when provided different app key origins', async function () {
const appKeyAddress1 = await keyring.getAppKeyAddress(
firstAcct,
'someapp.origin.io',
);

expect(isValidAddress(appKeyAddress1)).toBe(true);

const appKeyAddress2 = await keyring.getAppKeyAddress(
firstAcct,
'anotherapp.origin.io',
);

expect(isValidAddress(appKeyAddress2)).toBe(true);
expect(appKeyAddress1).not.toBe(appKeyAddress2);
});

it('should return the same address when called multiple times with the same params', async function () {
const appKeyAddress1 = await keyring.getAppKeyAddress(
firstAcct,
'someapp.origin.io',
);

expect(isValidAddress(appKeyAddress1)).toBe(true);

const appKeyAddress2 = await keyring.getAppKeyAddress(
firstAcct,
'someapp.origin.io',
);

expect(isValidAddress(appKeyAddress2)).toBe(true);
expect(appKeyAddress1).toBe(appKeyAddress2);
});

it('should throw error if the provided origin is not a string', async function () {
await expect(keyring.getAppKeyAddress(firstAcct, [])).rejects.toThrow(
`'origin' must be a non-empty string`,
);
});

it('should throw error if the provided origin is an empty string', async function () {
await expect(keyring.getAppKeyAddress(firstAcct, '')).rejects.toThrow(
`'origin' must be a non-empty string`,
);
});
});

describe('exportAccount', function () {
beforeEach(() => {
keyring = new HdKeyring({
mnemonic: sampleMnemonic,
numberOfAccounts: 1,
});
});

it('should return a hex-encoded private key', async function () {
const expectedPrivateKeyResult =
'0xd3cc16948a02a91b9fcf83735653bf3dfd82c86543fdd1e9a828bd25e8a7b68d';
const privKeyHexValue = await keyring.exportAccount(firstAcct);

expect(expectedPrivateKeyResult).toBe(`0x${privKeyHexValue}`);
});

it('throw error if account is not present', async function () {
await expect(keyring.exportAccount(notKeyringAddress)).rejects.toThrow(
'HD Keyring - Unable to find matching address.',
);
});
});
});

0 comments on commit f51d40b

Please sign in to comment.