Skip to content
This repository has been archived by the owner on Jul 17, 2023. It is now read-only.

Commit

Permalink
fix tests + add tests folder in tsconfig.json
Browse files Browse the repository at this point in the history
  • Loading branch information
Janaka-Steph committed Nov 14, 2022
1 parent edde0c8 commit 94f0e62
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 150 deletions.
22 changes: 17 additions & 5 deletions test/esplora.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,34 @@ import {
import { newRandomMnemonic } from './fixtures/wallet.keys';
// @ts-ignore
import { APIURL, faucet, sleep } from './_regtest';
import { ZKPInterface } from 'liquidjs-lib/src/confidential';
import secp256k1 from '@vulpemventures/secp256k1-zkp';

jest.setTimeout(80000);

describe('esplora', () => {
let txid: string;
let senderAddress: AddressInterface;
let unconfidentialSenderAddress: string;
let zkplib: ZKPInterface;

beforeAll(async () => {
const sender = newRandomMnemonic();
const sender = await newRandomMnemonic();
senderAddress = await sender.getNextAddress();
unconfidentialSenderAddress = address.fromConfidential(
senderAddress.confidentialAddress
).unconfidentialAddress;

txid = await faucet(senderAddress.confidentialAddress);
await faucet(unconfidentialSenderAddress);
zkplib = await secp256k1();
});

describe('fetchAndUnblindUtxos', () => {
it('should fetch the utxo prevout, even if unconfidential address is provided', async () => {
const senderUtxos = await fetchAndUnblindUtxos(
ecc,
zkplib,
[
{
confidentialAddress: senderAddress.confidentialAddress,
Expand All @@ -56,6 +61,7 @@ describe('esplora', () => {
it('should fetch the utxos, even if wrong blinding key is provided', async () => {
const senderUtxos = await fetchAndUnblindUtxos(
ecc,
zkplib,
[
{
confidentialAddress: senderAddress.confidentialAddress,
Expand All @@ -72,6 +78,7 @@ describe('esplora', () => {
it('should unblind utxos if the blinding key is provided', async () => {
const senderUtxos = await fetchAndUnblindUtxos(
ecc,
zkplib,
[
{
confidentialAddress: senderAddress.confidentialAddress,
Expand All @@ -88,6 +95,7 @@ describe('esplora', () => {
it('should skip unblinding step if the skip predicate returns true', async () => {
const senderUtxos = await fetchAndUnblindUtxos(
ecc,
zkplib,
[
{
confidentialAddress: senderAddress.confidentialAddress,
Expand Down Expand Up @@ -117,7 +125,8 @@ describe('esplora', () => {
return senderAddress.blindingPrivateKey;
} else return undefined;
},
APIURL
APIURL,
zkplib
);

const faucetTx = senderTxs.find(t => t.txid === txid);
Expand All @@ -128,6 +137,7 @@ describe('esplora', () => {
[senderAddress.confidentialAddress],
() => senderAddress.blindingPrivateKey,
APIURL,
zkplib,
tx => tx.txid === txid
);

Expand All @@ -147,7 +157,8 @@ describe('esplora', () => {
return senderAddress.blindingPrivateKey;
} else return undefined;
},
APIURL
APIURL,
zkplib
);

const faucetTx = senderTxs.find(t => t.txid === txid);
Expand All @@ -157,7 +168,7 @@ describe('esplora', () => {

describe('unspents from transactions', () => {
it('should compute utxos set from transactions', async () => {
const identity = Mnemonic.Random('regtest', ecc);
const identity = Mnemonic.Random('regtest', ecc, zkplib);
const address0 = await identity.getNextAddress();
const address1 = await identity.getNextAddress();

Expand All @@ -182,7 +193,8 @@ describe('esplora', () => {
return address1.blindingPrivateKey;
} else return undefined;
},
APIURL
APIURL,
zkplib
);

const utxos = utxosFromTransactions(
Expand Down
6 changes: 5 additions & 1 deletion test/fixtures/wallet.keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import ECPairFactory from 'ecpair';
import { networks, payments } from 'liquidjs-lib';
import { Mnemonic } from '../../src/identity/mnemonic';
import * as ecc from 'tiny-secp256k1';
import secp256k1 from '@vulpemventures/secp256k1-zkp';

const ECPair = ECPairFactory(ecc);
const network = networks.regtest;
Expand All @@ -16,7 +17,10 @@ const blindKeyPair2 = ECPair.fromWIF(
network
);

export const newRandomMnemonic = () => Mnemonic.Random('regtest', ecc);
export const newRandomMnemonic = async () => {
const zkplib = await secp256k1();
return Mnemonic.Random('regtest', ecc, zkplib);
};

// this is random address for who is receiving the withdrawal
export const recipientAddress = payments.p2wpkh({
Expand Down
112 changes: 62 additions & 50 deletions test/mnemonic.identity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,66 +27,77 @@ import * as ecc from 'tiny-secp256k1';
import { Restorer } from '../src';
// @ts-ignore
import { faucet, fetchTxHex, fetchUtxos } from './_regtest';
import BIP32Factory from 'bip32';
import { SLIP77Factory } from 'slip77';
import BIP32Factory, { BIP32Interface } from 'bip32';
import { SLIP77Factory, Slip77Interface } from 'slip77';
import secp256k1 from '@vulpemventures/secp256k1-zkp';

const network = networks.regtest;
const lbtc = AssetHash.fromHex(network.assetHash);

jest.setTimeout(500_000);

const validOpts: IdentityOpts<MnemonicOpts> = {
chain: 'regtest',
type: IdentityType.Mnemonic,
ecclib: ecc,
opts: {
mnemonic:
'turn manual grain tobacco pluck onion off chief drive amount slice forward',
},
};

const seedFromValidMnemonic = mnemonicToSeedSync(validOpts.opts.mnemonic);
const masterPrivateKeyFromValidMnemonic = BIP32Factory(ecc).fromSeed(
seedFromValidMnemonic,
network
);
const masterBlindingKeyFromValidMnemonic = SLIP77Factory(ecc).fromSeed(
seedFromValidMnemonic
);

const validOptsFrench: IdentityOpts<MnemonicOpts> = {
...validOpts,
opts: {
mnemonic:
'mutuel ourson soupape vertu atelier dynastie silicium absolu océan légume pyramide skier météore tulipe alchimie élargir gourmand étaler saboter cocotier aisance mairie jeton créditer',
language: 'french',
},
};

const unvalidLanguageOpts: IdentityOpts<MnemonicOpts> = {
...validOpts,
opts: {
...validOpts.opts,
language: 'corsican',
},
};

const unvalidTypeOpts: IdentityOpts<MnemonicOpts> = {
...validOpts,
type: IdentityType.PrivateKey,
};

const unvalidMnemonicOpts: IdentityOpts<MnemonicOpts> = {
...validOpts,
opts: {
mnemonic: 'tbh nigiri is awesome for Liquid / bitcoin unit testing',
},
};
let validOpts: IdentityOpts<MnemonicOpts>;
let validOptsFrench: IdentityOpts<MnemonicOpts>;
let unvalidLanguageOpts: IdentityOpts<MnemonicOpts>;
let unvalidTypeOpts: IdentityOpts<MnemonicOpts>;
let unvalidMnemonicOpts: IdentityOpts<MnemonicOpts>;
let seedFromValidMnemonic: Buffer;
let masterPrivateKeyFromValidMnemonic: BIP32Interface;
let masterBlindingKeyFromValidMnemonic: Slip77Interface;

describe('Identity: Mnemonic', () => {
beforeAll(async () => {
const zkplib = await secp256k1();
validOpts = {
chain: 'regtest',
type: IdentityType.Mnemonic,
ecclib: ecc,
zkplib: zkplib,
opts: {
mnemonic:
'turn manual grain tobacco pluck onion off chief drive amount slice forward',
},
};
validOptsFrench = {
...validOpts,
opts: {
mnemonic:
'mutuel ourson soupape vertu atelier dynastie silicium absolu océan légume pyramide skier météore tulipe alchimie élargir gourmand étaler saboter cocotier aisance mairie jeton créditer',
language: 'french',
},
};
unvalidLanguageOpts = {
...validOpts,
opts: {
...validOpts.opts,
language: 'corsican',
},
};
unvalidTypeOpts = {
...validOpts,
type: IdentityType.PrivateKey,
};
unvalidMnemonicOpts = {
...validOpts,
opts: {
mnemonic: 'tbh nigiri is awesome for Liquid / bitcoin unit testing',
},
};
seedFromValidMnemonic = mnemonicToSeedSync(validOpts.opts.mnemonic);
masterPrivateKeyFromValidMnemonic = BIP32Factory(ecc).fromSeed(
seedFromValidMnemonic,
network
);
masterBlindingKeyFromValidMnemonic = SLIP77Factory(ecc).fromSeed(
seedFromValidMnemonic
);
});

describe('Constructor', () => {
const validMnemonic = new Mnemonic(validOpts);
let validMnemonic: Mnemonic;
beforeAll(() => {
validMnemonic = new Mnemonic(validOpts);
});

it('should build a valid Mnemonic class if the constructor arguments are valid', () => {
assert.deepStrictEqual(validMnemonic instanceof Mnemonic, true);
Expand Down Expand Up @@ -496,6 +507,7 @@ const tests = [

describe('Mnemonic extended public key', () => {
for (const t of tests) {
// eslint-disable-next-line no-loop-func
it(`should derive the correct xpub with "${t.mnemonic}" and ${t.baseDerivationPath}`, async () => {
const mnemonicId = new Mnemonic({
...validOpts,
Expand Down
66 changes: 36 additions & 30 deletions test/multisig.identity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,38 +34,44 @@ const lbtc = AssetHash.fromHex(network.assetHash);

jest.setTimeout(60000);

const cosigners: Mnemonic[] = [
Mnemonic.Random('regtest', ecc, DEFAULT_BASE_DERIVATION_PATH),
Mnemonic.Random('regtest', ecc, DEFAULT_BASE_DERIVATION_PATH),
];

const validOpts: IdentityOpts<MultisigOpts> = {
chain: 'regtest',
type: IdentityType.Multisig,
ecclib: ecc,
opts: {
signer: {
mnemonic: generateMnemonic(),
baseDerivationPath: DEFAULT_BASE_DERIVATION_PATH,
}, // 1 signer
cosigners: cosigners.map(m => m.getXPub()), // 2 co signers
requiredSignatures: 2, // need 2 signatures among 3 pubkeys
},
};

const invalidOpts: IdentityOpts<MultisigOpts> = {
...validOpts,
opts: {
signer: {
mnemonic: generateMnemonic(),
baseDerivationPath: DEFAULT_BASE_DERIVATION_PATH,
}, // 1 signer
cosigners: cosigners.map(m => m.getXPub()),
requiredSignatures: 10000000,
},
};
let cosigners: Mnemonic[];
let validOpts: IdentityOpts<MultisigOpts>;
let invalidOpts: IdentityOpts<MultisigOpts>;

describe('Identity: Multisig', () => {
beforeAll(async () => {
const zkplib = await secp256k1();
cosigners = [
Mnemonic.Random('regtest', ecc, zkplib, DEFAULT_BASE_DERIVATION_PATH),
Mnemonic.Random('regtest', ecc, zkplib, DEFAULT_BASE_DERIVATION_PATH),
];
validOpts = {
chain: 'regtest',
type: IdentityType.Multisig,
ecclib: ecc,
zkplib: zkplib,
opts: {
signer: {
mnemonic: generateMnemonic(),
baseDerivationPath: DEFAULT_BASE_DERIVATION_PATH,
}, // 1 signer
cosigners: cosigners.map(m => m.getXPub()), // 2 co signers
requiredSignatures: 2, // need 2 signatures among 3 pubkeys
},
};
invalidOpts = {
...validOpts,
opts: {
signer: {
mnemonic: generateMnemonic(),
baseDerivationPath: DEFAULT_BASE_DERIVATION_PATH,
}, // 1 signer
cosigners: cosigners.map(m => m.getXPub()),
requiredSignatures: 10000000,
},
};
});

describe('Constructor', () => {
it('should build a valid Multisig class instance if the constructor arguments are valid', () => {
const multisig = new Multisig(validOpts);
Expand Down
Loading

0 comments on commit 94f0e62

Please sign in to comment.