Skip to content

Commit

Permalink
updated tests and fixed issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Dapiguabc committed Nov 26, 2021
1 parent 95fdaf8 commit 8202806
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 60 deletions.
52 changes: 27 additions & 25 deletions dist/cjs/lamden.js
Original file line number Diff line number Diff line change
Expand Up @@ -2902,28 +2902,28 @@ function new_wallet(seed = null) {

/**
*
* @param mnemonic 24 word seed phrase
* @param seed Bip39 seed phrase (128 characters in hex)
* @param derivationIndex bip32 derivation key index
* @returns {{derivationIndex: number, vk: string, sk: string, mnemonic: string}}
* derivationIndex: bip32 derivation key index
* vk: Verify Key (VK) represented as a 64 character hex string
* sk: Signing Key (SK) represented as a 64 character hex string
* mnemonic: 24 word seed phrase
* seed: Bip39 seed phrase (128 characters in hex)
* mnemonic: Bip39 24 words mnemonic
*/
function generate_keys_bip39(mnemonic = undefined, derivationIndex = 0) {
let finalMnemonic;

if (mnemonic !== undefined) {
finalMnemonic = mnemonic;
} else {
finalMnemonic = bip39__namespace.generateMnemonic(256);
}

const seed = bip39__namespace.mnemonicToSeedSync(finalMnemonic).toString("hex");
function generate_keys_bip39(seed = undefined, derivationIndex = 0) {
let finalSeed;
let finalMnemonic;

if (seed !== undefined){
finalSeed = seed;
}else {
finalMnemonic = bip39__namespace.generateMnemonic(256);
finalSeed = bip39__namespace.mnemonicToSeedSync(finalMnemonic).toString('hex');
}

const derivationPath = "m/44'/789'/" + derivationIndex + "'/0'/0'";
const { key, chainCode } = bip32__default["default"].derivePath(derivationPath, seed, 0x80000000);
const derivationPath = "m/44'/789'/" + derivationIndex + "'/0'/0'";
const { key, chainCode } = bip32__default["default"].derivePath(derivationPath, finalSeed, 0x80000000);

const privateKey = key.toString("hex");
const publicKey = bip32__default["default"].getPublicKey(key, false).toString("hex");
Expand All @@ -2932,26 +2932,28 @@ function generate_keys_bip39(mnemonic = undefined, derivationIndex = 0) {
throw Error("Bip32 public key does not match with Lamden public key!");
}

return {
sk: privateKey,
vk: publicKey,
derivationIndex: derivationIndex,
mnemonic: finalMnemonic,
};
return {
sk: privateKey,
vk: publicKey,
derivationIndex: derivationIndex,
seed: seed !== undefined ? null : finalSeed,
mnemonic: seed !== undefined ? null : finalMnemonic,
}
}

/**
* @param mnemonic 24 word seed phrase
* @param seed Bip39 seed phrase (128 characters in hex)
* @param derivationIndex bip32 derivation key index
*
* @return {{derivationIndex: number, vk: string, sk: string, mnemonic: (string|undefined)}} { sk, vk, derivationIndex, mnemonic }
* sk: Signing Key (SK) represented as a 64 character hex string
* vk: Verify Key (VK) represented as a 64 character hex string
* derivationIndex: Bip32 derivation index
* mnemonic: 24 word seed phrase
* seed: Bip39 seed phrase (128 characters in hex)
* mnemonic: Bip39 24 words mnemonic
*/
function new_wallet_bip39(mnemonic = undefined, derivationIndex = 0) {
return generate_keys_bip39(mnemonic, derivationIndex);
function new_wallet_bip39(seed = undefined, derivationIndex = 0) {
return generate_keys_bip39(seed, derivationIndex);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion dist/esm/lamden.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"main": "dist/cjs/lamden.js",
"module": "dist/esm/lamden.js",
"scripts": {
"test": "npm run build && mocha --recursive --timeout 10000",
"test": "npm run build && mocha --recursive --timeout 30000",
"tests": "npm run test",
"test-network": "npm run build && mocha test/network-test.js",
"test-masternode-api": "npm run build && mocha test/masternode_api-test.js",
"test-network": "npm run build && mocha test/network-test.js --timeout 10000",
"test-masternode-api": "npm run build && mocha test/masternode_api-test.js --timeout 10000",
"test-transaction-builder": "npm run build && mocha test/transactionBuilder-test.js --timeout 10000",
"test-transaction-batcher": "npm run build && mocha test/transactionBatcher-test.js --timeout 60000",
"test-wallet": "npm run build && mocha test/wallet-test.js",
Expand Down
4 changes: 2 additions & 2 deletions src/js/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export function new_wallet(seed = null) {

/**
*
* @param mnemonic 24 word seed phrase
* @param seed Bip39 seed phrase (128 characters in hex)
* @param derivationIndex bip32 derivation key index
* @returns {{derivationIndex: number, vk: string, sk: string, mnemonic: string}}
* derivationIndex: bip32 derivation key index
Expand Down Expand Up @@ -165,7 +165,7 @@ function generate_keys_bip39(seed = undefined, derivationIndex = 0) {
}

/**
* @param mnemonic 24 word seed phrase
* @param seed Bip39 seed phrase (128 characters in hex)
* @param derivationIndex bip32 derivation key index
*
* @return {{derivationIndex: number, vk: string, sk: string, mnemonic: (string|undefined)}} { sk, vk, derivationIndex, mnemonic }
Expand Down
18 changes: 7 additions & 11 deletions test/browsers/wallet-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,18 @@ describe("Browsers Tests: Test Lamden Wallet methods", async () => {
expect(validateTypes.isNumber(newWallet.derivationIndex)).to.be(true);
expect(newWallet.derivationIndex).to.be(0);
}),
it("creates a bip39 / bip32 compatible lamden keypair from mnemonic", async () => {
const mnemonic =
"ripple junk access broom element fitness side example ramp flush model creek nest face rent jacket ahead come short find over family wise comfort";
it("creates a bip39 / bip32 compatible lamden keypair from seed", async () => {
const seed = 'd3ad26bd89d54d0c22bb32d34ea9f06c567ba060d8e1518974d807180b886c643bfb7f455bd3db2c767a17c089aab20db97cf0f0184d730b9d20be0c7b6cc6cc'
const derivationIndex = 127;

let newWallet = await driver.executeScript(`return Lamden.wallet.new_wallet_bip39('${mnemonic}', ${derivationIndex})`);
let newWallet = await driver.executeScript(`return Lamden.wallet.new_wallet_bip39('${seed}', ${derivationIndex})`);

expect(validateTypes.isStringHex(newWallet.vk)).to.be(true);
expect(newWallet.vk).to.be(
"d0d2de909bf7c2be3bafbcb3af0b1c50487b80ba48b5700bff35bb927921c607"
);
expect(newWallet.vk).to.be("d0d2de909bf7c2be3bafbcb3af0b1c50487b80ba48b5700bff35bb927921c607");
expect(validateTypes.isStringHex(newWallet.sk)).to.be(true);
expect(newWallet.sk).to.be(
"86c77748edc039c672cf761d2db1e52d6255b16cd4d626d4b66c67eb224287a8"
);
expect(newWallet.mnemonic).to.be(mnemonic);
expect(newWallet.sk).to.be("86c77748edc039c672cf761d2db1e52d6255b16cd4d626d4b66c67eb224287a8");
expect(newWallet.mnemonic).to.be(null);
expect(newWallet.seed).to.be( null )
expect(validateTypes.isNumber(newWallet.derivationIndex)).to.be(true);
expect(newWallet.derivationIndex).to.be(127);
});
Expand Down
2 changes: 1 addition & 1 deletion test/transactionBuilder-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ describe("Test TransactionBuilder class", () => {
expect(resultInfo.type).to.equal("success");
});
it("Sends transactions and can get hash result from masternode", async function () {
this.timeout(20000);
this.timeout(30000);
await newTx1.checkForTransactionResult();
let txBlockResult = newTx1.txBlockResult;
expect(txBlockResult.hash).to.equal(newTx1.txSendResult.hash);
Expand Down
17 changes: 0 additions & 17 deletions test/wallet-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,6 @@ describe("Test Lamden Wallet methods", () => {
expect(validateTypes.isNumber(newWallet.derivationIndex)).to.be(true);
expect(newWallet.derivationIndex).to.be(0);
}),
it("creates a bip39 / bip32 compatible lamden keypair from mnemonic", () => {
const mnemonic =
"ripple junk access broom element fitness side example ramp flush model creek nest face rent jacket ahead come short find over family wise comfort";
const derivationIndex = 127;
let newWallet = wallet.new_wallet_bip39(mnemonic, derivationIndex);

expect( validateTypes.isStringHex(newWallet.vk) ).to.be( true )
expect( newWallet.vk.length ).to.be( 64 )
expect( validateTypes.isStringHex(newWallet.sk) ).to.be( true )
expect( newWallet.sk.length ).to.be( 64 )
expect( validateTypes.isStringWithValue(newWallet.mnemonic) ).to.be( true )
expect( validateTypes.isNumber(newWallet.derivationIndex) ).to.be( true )
expect( newWallet.derivationIndex ).to.be( 0 )
expect( validateTypes.isStringHex(newWallet.seed) ).to.be( true )
expect( newWallet.seed.length ).to.be( 128 )
}),

it('creates a bip39 / bip32 compatible lamden keypair from seed', () => {
const seed = 'd3ad26bd89d54d0c22bb32d34ea9f06c567ba060d8e1518974d807180b886c643bfb7f455bd3db2c767a17c089aab20db97cf0f0184d730b9d20be0c7b6cc6cc'
const derivationIndex = 127
Expand Down

0 comments on commit 8202806

Please sign in to comment.