Skip to content

Commit

Permalink
test: add new suites and cases
Browse files Browse the repository at this point in the history
  • Loading branch information
vvava committed Jan 17, 2025
1 parent c6cea67 commit 6b445f9
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { getAccountPrivateKeyFromMnemonic } from './getAccountPrivateKeyFromMnemonic';
import {
DerivationPath,
getWalletFromMnemonic,
} from '@avalabs/core-wallets-sdk';

jest.mock('@avalabs/core-wallets-sdk', () => ({
...jest.requireActual('@avalabs/core-wallets-sdk'),
getWalletFromMnemonic: jest.fn(() => ({ path: 'path', privateKey: '0xASD' })),
}));
describe('getAccountPrivateKeyFromMnemonic(', () => {
it('should throw an error because the signer is falsy', async () => {
(getWalletFromMnemonic as jest.Mock).mockReturnValueOnce(undefined);
try {
getAccountPrivateKeyFromMnemonic('random', 0, DerivationPath.BIP44);
} catch (e) {
expect(e).toEqual(new Error('The requested path is missing'));
}
});
it('should return with a privateKey', () => {
const privateKey = getAccountPrivateKeyFromMnemonic(
'random',
0,
DerivationPath.BIP44,
);
expect(privateKey).toBe('0xASD');
});
});
21 changes: 21 additions & 0 deletions src/background/services/secrets/utils/getAddressForHvm.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { addressBytesFromPubKey, getAddressForHvm } from './getAddressForHvm';

jest.mock('@noble/hashes/sha256', () => ({
sha256: jest.fn(() => new Uint8Array(1)),
}));
jest.mock('@noble/curves/ed25519', () => ({
ed25519: { getPublicKey: jest.fn(() => new Uint8Array(1)) },
}));

describe('getAddressForHvm()', () => {
it('should get the address bytes from publick key', () => {
const result = addressBytesFromPubKey(new Uint8Array(1));
expect(JSON.stringify(result)).toEqual(
JSON.stringify(new Uint8Array([0x00, ...new Uint8Array(1)])),
);
});
it('should get the address', () => {
const address = getAddressForHvm('0xASD');
expect(address).toBe('0x000000');
});
});
2 changes: 1 addition & 1 deletion src/background/services/secrets/utils/getAddressForHvm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { bytesToHex } from '@noble/hashes/utils';

const ED25519_AUTH_ID = 0x00;

const addressBytesFromPubKey = (pubKey: Uint8Array): Uint8Array => {
export const addressBytesFromPubKey = (pubKey: Uint8Array): Uint8Array => {
return new Uint8Array([ED25519_AUTH_ID, ...sha256(pubKey)]);
};

Expand Down
40 changes: 40 additions & 0 deletions src/background/services/wallet/HVMWallet.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { DerivationPath } from '@avalabs/core-wallets-sdk';
import { HVMWallet } from './HVMWallet';
import { TransactionPayload, VMABI } from 'hypersdk-client';
const returnedPublickKey = new Uint8Array(1);
const returnedSign = new Uint8Array(2);

jest.mock('@avalabs/core-wallets-sdk', () => ({
...jest.requireActual('@avalabs/core-wallets-sdk'),
getWalletFromMnemonic: jest.fn(() => ({ path: 'path', privateKey: '0xASD' })),
}));
jest.mock('@noble/curves/ed25519', () => ({
ed25519: {
getPublicKey: jest.fn(() => returnedPublickKey),
sign: jest.fn(() => returnedSign),
},
}));
jest.mock('hypersdk-client/dist/Marshaler', () => ({
Marshaler: jest.fn(() => ({
encodeTransaction: jest.fn(() => 'asd'),
})),
}));
describe('HVMWallet', () => {
it('should return the wallet from mnemonic', () => {
const wallet = HVMWallet.fromMnemonic('mnemonic', 0, DerivationPath.BIP44);
expect(wallet).toBeInstanceOf(HVMWallet);
});
it('should get the public key', () => {
const wallet = HVMWallet.fromMnemonic('mnemonic', 0, DerivationPath.BIP44);
const pubKey = wallet.getPublicKey();
expect(JSON.stringify(pubKey)).toBe(JSON.stringify(returnedPublickKey));
});
it('should get the sign with `signEd25519`', async () => {
const wallet = HVMWallet.fromMnemonic('mnemonic', 0, DerivationPath.BIP44);
const sign = await wallet.signEd25519(
{} as TransactionPayload,
{} as VMABI,
);
expect(sign).toBe('1111111');
});
});
11 changes: 9 additions & 2 deletions src/background/services/wallet/HVMWallet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { getWalletFromMnemonic } from '@avalabs/core-wallets-sdk';
import {
DerivationPath,
getWalletFromMnemonic,
} from '@avalabs/core-wallets-sdk';
import { ed25519 } from '@noble/curves/ed25519';
import { strip0x } from '@avalabs/core-utils-sdk';
import { base58 } from '@scure/base';
Expand All @@ -11,7 +14,11 @@ import {

export class HVMWallet {
#privateKey: string;
static fromMnemonic(mnemonic, accountIndex, derivationPath) {
static fromMnemonic(
mnemonic: string,
accountIndex: number,
derivationPath: DerivationPath,
) {
const signer = getWalletFromMnemonic(
mnemonic,
accountIndex,
Expand Down

0 comments on commit 6b445f9

Please sign in to comment.