chaingate
Version:
A complete TypeScript library for connecting to and making transactions on different blockchains
93 lines • 4.54 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
const TestUtils_1 = require("../../Utils/TestUtils");
const initializeWallet = __importStar(require("../../InitializeWallet"));
const PhraseWallet_1 = require("../implementations/PhraseWallet/PhraseWallet");
const SeedWallet_1 = require("../implementations/SeedWallet/SeedWallet");
(0, TestUtils_1.setupCryptoGetRandomValuesMock)();
describe('Wallet', () => {
it('serialization should match snapshot', async () => {
const wallet = await initializeWallet.fromPhrase({ phrase: 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about',
// eslint-disable-next-line @typescript-eslint/no-unused-vars
encrypt: { password: '1234', askForPassword: async (_attempts, _reject) => { return '1234'; } } });
expect(await wallet.serialize()).toMatchSnapshot();
});
it('should create an encrypted wallet, serialize/deserialize it, and manage addresses correctly', async () => {
const encrypt = {
password: '',
askForPassword: throwAskForPassword,
};
// Create
const { wallet } = await initializeWallet.create({ encrypt });
await checkSerialization(wallet);
// From private key
const wallet2 = await initializeWallet.fromPrivateKey({ privateKey: '741745080050f2ce656aaa2a983a6b510caa706643e1ad05214feac6677ba657', encrypt });
await checkSerialization(wallet2);
// From seed
const wallet3 = await initializeWallet.fromSeed({ seed: '000102030405060708090a0b0c0d0e0f', encrypt });
await checkSerialization(wallet3);
// From phrase
const wallet4 = await initializeWallet.fromPhrase({ phrase: 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about', encrypt });
await checkSerialization(wallet4);
});
});
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async function throwAskForPassword(_attempts, _reject) {
throw new Error('Password prompt not implemented');
}
async function checkSerialization(wallet) {
// Access the Bitcoin currency
const btc = wallet.currency('bitcoin');
// Getting address
await btc.getAddress();
// Serialize the wallet
const serializedData = await wallet.serialize();
// Deserialize into a new wallet instance
const deserializedWallet = (await initializeWallet.deserialize({
serialized: serializedData,
askForPassword: throwAskForPassword,
}));
// Access Bitcoin again on the deserialized wallet
const btcDeserialized = deserializedWallet.currency('bitcoin');
await btcDeserialized.getAddress();
// Change derivation path to something not cached and expect it to throw
if (deserializedWallet instanceof PhraseWallet_1.PhraseWallet || deserializedWallet instanceof SeedWallet_1.SeedWallet) {
const btcDeserialized = deserializedWallet.currency('bitcoin');
btcDeserialized.setDerivationPath('m/0/0');
await expect(() => btcDeserialized.getAddress()).rejects.toThrow();
}
}
//# sourceMappingURL=Wallet.test.js.map