UNPKG

@funded-labs/plug-controller

Version:

Internet Computer Plug wallet's controller

107 lines (106 loc) 6.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const principal_1 = require("@dfinity/principal"); const errors_1 = require("../../errors"); const signature_1 = require("../signature"); const index_1 = require("./index"); describe('Account utils', () => { let globalAccount; let globalKeys; const MAX_ACCOUNTS = 5; beforeAll(() => { globalAccount = (0, index_1.createAccount)(); globalKeys = globalAccount.identity.getKeyPair(); }); describe('credentials creation', () => { it('should create a new account with mnemonic, secret and public keys', () => { const account = (0, index_1.createAccount)(); expect(account).toHaveProperty('mnemonic'); expect(account).toHaveProperty('identity'); expect(account).toHaveProperty('accountId'); expect(account.identity.getKeyPair()).toHaveProperty('secretKey'); expect(account.identity.getKeyPair()).toHaveProperty('publicKey'); }); it('should always create different new accounts', () => { const mnemonics = []; const secretKeys = []; const publicKeys = []; for (let i = 1; i < MAX_ACCOUNTS; i += 1) { const { mnemonic, identity } = (0, index_1.createAccount)(); const { publicKey, secretKey } = identity.getKeyPair(); expect(mnemonics).not.toContain(mnemonic); expect(secretKeys).not.toContain(secretKey); expect(publicKeys).not.toContain(publicKey); mnemonics.push(mnemonic); secretKeys.push(secretKey.toString()); publicKeys.push(publicKey.toString()); } }); it('should create a new account from a mnemonic, having new secret and public keys', () => { const account = (0, index_1.createAccountFromMnemonic)(globalAccount.mnemonic, 1); expect(account).toHaveProperty('mnemonic'); expect(account).toHaveProperty('identity'); expect(account).toHaveProperty('accountId'); expect(account.identity.getKeyPair()).toHaveProperty('secretKey'); expect(account.identity.getKeyPair()).toHaveProperty('publicKey'); const { mnemonic, identity, accountId } = account; const { publicKey, secretKey } = identity.getKeyPair(); // Mnemonic should be the same but keys and accountId shouldn't expect(mnemonic).toEqual(globalAccount.mnemonic); expect(accountId).not.toEqual(globalAccount.accountId); expect(secretKey).not.toEqual(globalKeys.secretKey.toString()); expect(publicKey).not.toEqual(globalKeys.publicKey.toDer().toString()); }); it('should always derive the same account given the same mnemonic and account ID', () => { for (let i = 0; i < MAX_ACCOUNTS; i += 1) { const account = (0, index_1.createAccountFromMnemonic)(globalAccount.mnemonic, i); const newAccount = (0, index_1.createAccountFromMnemonic)(globalAccount.mnemonic, i); const { secretKey, publicKey } = account.identity.getKeyPair(); const { secretKey: newSecret, publicKey: newPublic, } = newAccount.identity.getKeyPair(); expect(account.mnemonic).toEqual(newAccount.mnemonic); expect(account.accountId).toEqual(newAccount.accountId); expect(secretKey).toEqual(newSecret); expect(publicKey).toEqual(newPublic); } }); it('should generate the correct principal id', () => { const mnemonic = 'easily drift crazy brother trash green cricket peasant unhappy fruit behind pudding'; const principal = 'gkuhp-3onv2-yuitx-msib3-z4kyb-uw5ua-fehux-6ontl-47u47-iwuul-rae'; const { identity } = (0, index_1.createAccountFromMnemonic)(mnemonic, 0); expect(identity.getPrincipal().toText()).toEqual(principal); }); it('should fail if provided an invalid mnemonic', () => { const invalidMnemonic = 'Some invalid Mnemonic'; expect(() => (0, index_1.createAccountFromMnemonic)(invalidMnemonic, 1)).toThrow(errors_1.ERRORS.INVALID_MNEMONIC); }); it('should fail if provided an invalid account id', () => { const stringId = '1'; const negativeId = -1; expect(() => (0, index_1.createAccountFromMnemonic)(globalAccount.mnemonic, stringId)).toThrow(errors_1.ERRORS.INVALID_ACC_ID); expect(() => (0, index_1.createAccountFromMnemonic)(globalAccount.mnemonic, negativeId)).toThrow(errors_1.ERRORS.INVALID_ACC_ID); }); // This checks that this works on .js files as well as TS which auto-checks these things it('should fail if provided an empty mnemonic', () => { expect(() => (0, index_1.createAccountFromMnemonic)('', 1)).toThrow(errors_1.ERRORS.INVALID_MNEMONIC); expect(() => (0, index_1.createAccountFromMnemonic)(undefined, 1)).toThrow(errors_1.ERRORS.INVALID_MNEMONIC); expect(() => (0, index_1.createAccountFromMnemonic)(null, 1)).toThrow(errors_1.ERRORS.INVALID_MNEMONIC); }); }); describe('id generation', () => { it('should generate the correct account id', () => { const principal = principal_1.Principal.fromText('gkuhp-3onv2-yuitx-msib3-z4kyb-uw5ua-fehux-6ontl-47u47-iwuul-rae'); const accountId = '1f77688a6a9b2b85640d753d487209344cc9c9675c409bbef5e061710c7220ab'; const id = (0, index_1.getAccountId)(principal); expect(id).toEqual(accountId); }); }); describe('credentials utility', () => { test('should sign a message into an unreadable state and recover it using its keys', () => { const { secretKey, publicKey } = globalKeys; const message = 'This is a secret message!'; const signed = (0, signature_1.sign)(message, secretKey); const opened = (0, signature_1.verify)(message, signed, publicKey); expect(opened).toBe(true); }); }); });