Skip to content

Commit

Permalink
refactor: Remove unnecessary log statements and clean up code
Browse files Browse the repository at this point in the history
Co-authored-by: keiff3r <[email protected]>
  • Loading branch information
Z4karia committed Aug 23, 2024
1 parent 6116a4f commit 8fc7f21
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 71 deletions.
39 changes: 39 additions & 0 deletions APDUs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
Get_extended_pubkey:
=>e10000001600058000005480000001800000000000000000000000
<=74707562444742337234346d37666a654c36676a6b75456e6e5a6e586677646e7a71524b6b713961636576354d464755653631766179326e6e3270427a56654e546d613563745733415746524a644c65707a737a35503332676a4d426268687837684b6434664a4d587361635a364c9000

Get_wallet_address:
=> e10300004600893198490079bb0667594bfa902274b1cd28d7ed9df140121dc69021bead679f00000000000000000000000000000000000000000000000000000000000000000000000000
<= 4000893198490079bb0667594bfa902274b1cd28d7ed9df140121dc69021bead679fe000
=> f80100002e2c2c01000877706b682840302901ac0efe99ff9d292d0e7bd2eab1b24480f963c52afe2e655d966827d3efbbe340
<= 41ac0efe99ff9d292d0e7bd2eab1b24480f963c52afe2e655d966827d3efbbe3400100e000
=> f801000022ac0efe99ff9d292d0e7bd2eab1b24480f963c52afe2e655d966827d3efbbe3400000
<= 4000ac0efe99ff9d292d0e7bd2eab1b24480f963c52afe2e655d966827d3efbbe340e000
=> f8010000898787005b66356163633266642f3834272f31272f30275d747075624443744b66734e795268554c6a5a39584d5334564b4b7456635064564469384d4b5562635344394d4a44796a52753141324e44354d6969706f7a797973704254396267387570457037613845416746784e78586e316437516b64624c35325479356a69534c6378507431502f2a2a
<= 41ac0efe99ff9d292d0e7bd2eab1b24480f963c52afe2e655d966827d3efbbe3400100e000
=> f801000022ac0efe99ff9d292d0e7bd2eab1b24480f963c52afe2e655d966827d3efbbe3400000
<= 4000ac0efe99ff9d292d0e7bd2eab1b24480f963c52afe2e655d966827d3efbbe340e000
=> f8010000898787005b66356163633266642f3834272f31272f30275d747075624443744b66734e795268554c6a5a39584d5334564b4b7456635064564469384d4b5562635344394d4a44796a52753141324e44354d6969706f7a797973704254396267387570457037613845416746784e78586e316437516b64624c35325479356a69534c6378507431502f2a2a
<= 746231717a647237733273723064776d6b777830333372346e756a7a6b38367530637936666d7a666a6b9000

Screen at the end of get wallet address:
{"text": "Address"}
{"text": "tb1qzdr7s2sr0dwmkwx033r4nujzk86u0cy6fmzfjk"}


Get_master_fingerprint:
=> e105000000¸
<= f5acc2fd9000

Current test:
=> e10000001600058000005480000001800000000000000000000000
<= 74707562444742337234346d37666a654c36676a6b75456e6e5a6e586677646e7a71524b6b713961636576354d464755653631766179326e6e3270427a56654e546d613563745733415746524a644c65707a737a35503332676a4d426268687837684b6434664a4d587361635a364c9000
=> e10000000e0003800000548000000180000000
<= 747075624443744b66734e795268554c6a5a39584d5334564b4b7456635064564469384d4b5562635344394d4a44796a52753141324e44354d6969706f7a797973704254396267387570457037613845416746784e78586e316437516b64624c35325479356a69534c6378507431509000
=> e105000000
<= f5acc2fd9000
=> e10300004600893198490079bb0667594bfa902274b1cd28d7ed9df140121dc69021bead679f00000000000000000000000000000000000000000000000000000000000000000000000000
<= 4000893198490079bb0667594bfa902274b1cd28d7ed9df140121dc69021bead679fe000
=> f80100002e2c2c01000877706b682840302901ac0efe99ff9d292d0e7bd2eab1b24480f963c52afe2e655d966827d3efbbe340
<= b007

6 changes: 1 addition & 5 deletions src/BtcNew.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ export default class BtcNew {
if (!isPathNormal(path)) {
throw Error(`non-standard path: ${path}`);
}
log("getWalletPublicKey", 'workok');
const pathElements: number[] = pathStringToArray(path);
const xpub = await this.client.getExtendedPubkey(false, pathElements);

Expand Down Expand Up @@ -158,23 +157,20 @@ export default class BtcNew {
if (accountPath.length + 2 != pathElements.length) {
return "";
}
log("getWalletAddress", 'workok');
const accountXpub = await this.client.getExtendedPubkey(false, accountPath);
const masterFingerprint = await this.client.getMasterFingerprint();
const policy = new WalletPolicy(
descrTempl,
createKey(masterFingerprint, accountPath, accountXpub),
);
const changeAndIndex = pathElements.slice(-2, pathElements.length);
let rtn = this.client.getWalletAddress(
return this.client.getWalletAddress(
policy,
Buffer.alloc(32, 0),
changeAndIndex[0],
changeAndIndex[1],
display,
);
// log("data getWalletAddress", rtn);
return rtn;
}

/**
Expand Down
12 changes: 0 additions & 12 deletions src/newops/appClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,7 @@ export class AppClient {
data: Buffer,
cci?: ClientCommandInterpreter,
): Promise<Buffer> {
// CONCAT CLA INS P1 P2 AND data
const APDU = Buffer.concat([Buffer.from([CLA_BTC, ins, 0, 0]), data]);
log("apdu IN:", APDU.toString("hex"));
log('apdu IN', "workokok")
console.log("apdu IN:", APDU.toString("hex"));
let response: Buffer = await this.transport.send(CLA_BTC, ins, 0, 0, data, [0x9000, 0xe000]);
console.log("apdu OUT:", response.toString("hex"));
while (response.readUInt16BE(response.length - 2) === 0xe000) {
if (!cci) {
throw new Error("Unexpected SW_INTERRUPTED_EXECUTION");
Expand All @@ -56,11 +50,6 @@ export class AppClient {
const hwRequest = response.slice(0, -2);
const commandResponse = cci.execute(hwRequest);

// CONCAT CLA INS P1 P2 AND CommandResponse
const APDU = Buffer.concat([Buffer.from([CLA_BTC, ins, 0, 0]), commandResponse]);
log("apdu IN:", APDU.toString("hex"));
console.log("apdu IN:", APDU.toString("hex"));

response = await this.transport.send(
CLA_FRAMEWORK,
FrameworkIns.CONTINUE_INTERRUPTED,
Expand Down Expand Up @@ -91,7 +80,6 @@ export class AppClient {
addressIndex: number,
display: boolean,
): Promise<string> {
log("in client getWalletAddress", "workokok")
if (change !== 0 && change !== 1) throw new Error("Change can only be 0 or 1");
if (addressIndex < 0 || !Number.isInteger(addressIndex))
throw new Error("Invalid address index");
Expand Down
162 changes: 108 additions & 54 deletions tests/newops/BtcNew.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,35 @@ import {
wrappedP2wpkh,
wrappedP2wpkhTwoInputs,
} from "./testtx";
import { AppClient } from "../../src/newops/appClient";
import { listen, log } from "@ledgerhq/logs";
listen(log => console.log(log));

// test("getWalletPublicKey p2pkh", async () => {
// await testGetWalletPublicKey("m/44'/1'/0'", "pkh(@0)");
// await testGetWalletPublicKey("m/44'/0'/17'", "pkh(@0)");
// });
test("getWalletPublicKey p2pkh", async () => {
await testGetWalletPublicKey("m/44'/1'/0'", "pkh(@0)");
await testGetWalletPublicKey("m/44'/0'/17'", "pkh(@0)");
});
test("getWalletPublicKey p2wpkh", async () => {
await testGetWalletPublicKey("m/84'/1'/0'", "wpkh(@0)");
await testGetWalletPublicKey("m/84'/0'/17'", "wpkh(@0)");
});
// test("getWalletPublicKey wrapped p2wpkh", async () => {
// await testGetWalletPublicKey("m/49'/1'/0'", "sh(wpkh(@0))");
// await testGetWalletPublicKey("m/49'/0'/17'", "sh(wpkh(@0))");
// });
// test("getWalletPublicKey p2tr", async () => {
// await testGetWalletPublicKey("m/86'/1'/0'", "tr(@0)");
// await testGetWalletPublicKey("m/86'/0'/17'", "tr(@0)");
// });
test("testGetWalletPublicKeyRealClient p2wpkh", async () => {
await testGetWalletPublicKeyRealClient("m/84'/1'/0'", "wpkh(@0)");
});
test("getWalletPublicKey wrapped p2wpkh", async () => {
await testGetWalletPublicKey("m/49'/1'/0'", "sh(wpkh(@0))");
await testGetWalletPublicKey("m/49'/0'/17'", "sh(wpkh(@0))");
});
test("getWalletPublicKey p2tr", async () => {
await testGetWalletPublicKey("m/86'/1'/0'", "tr(@0)");
await testGetWalletPublicKey("m/86'/0'/17'", "tr(@0)");
});

// test("getWalletXpub normal path", async () => {
// await testGetWalletXpub("m/11'/12'");
// await testGetWalletXpub("m/11");
// await testGetWalletXpub("m/44'/0'/0'");
// });
test("getWalletXpub normal path", async () => {
await testGetWalletXpub("m/11'/12'");
await testGetWalletXpub("m/11");
await testGetWalletXpub("m/44'/0'/0'");
});

function testPaths(type: StandardPurpose): { ins: string[]; out?: string } {
const basePath = `m/${type}/1'/0'/`;
Expand All @@ -63,47 +67,47 @@ function testPaths(type: StandardPurpose): { ins: string[]; out?: string } {
return { ins };
}

// test("Sign p2pkh", async () => {
// const changePubkey = "037ed58c914720772c59f7a1e7e76fba0ef95d7c5667119798586301519b9ad2cf";
// await runSignTransactionTest(p2pkh, StandardPurpose.p2pkh, changePubkey);
// });
// test("Sign p2wpkh wrapped", async () => {
// let changePubkey = "03efc6b990c1626d08bd176aab0e545a4f55c627c7ddee878d12bbbc46a126177a";
// await runSignTransactionTest(wrappedP2wpkh, StandardPurpose.p2wpkhInP2sh, changePubkey);
// changePubkey = "031175a985c56e310ce3496a819229b427a2172920fd20b5972dda62758c6def09";
// await runSignTransactionTest(wrappedP2wpkhTwoInputs, StandardPurpose.p2wpkhInP2sh, changePubkey);
// });
test("Sign p2pkh", async () => {
const changePubkey = "037ed58c914720772c59f7a1e7e76fba0ef95d7c5667119798586301519b9ad2cf";
await runSignTransactionTest(p2pkh, StandardPurpose.p2pkh, changePubkey);
});
test("Sign p2wpkh wrapped", async () => {
let changePubkey = "03efc6b990c1626d08bd176aab0e545a4f55c627c7ddee878d12bbbc46a126177a";
await runSignTransactionTest(wrappedP2wpkh, StandardPurpose.p2wpkhInP2sh, changePubkey);
changePubkey = "031175a985c56e310ce3496a819229b427a2172920fd20b5972dda62758c6def09";
await runSignTransactionTest(wrappedP2wpkhTwoInputs, StandardPurpose.p2wpkhInP2sh, changePubkey);
});
test("Sign p2wpkh", async () => {
await runSignTransactionTest(p2wpkh, StandardPurpose.p2wpkh);
});
// test("Sign p2tr", async () => {
// // This tx uses locktime, so this test verifies that locktime is propagated to/from
// // the psbt correctly.
// await runSignTransactionTest(p2tr, StandardPurpose.p2tr);
// });
test("Sign p2tr", async () => {
// This tx uses locktime, so this test verifies that locktime is propagated to/from
// the psbt correctly.
await runSignTransactionTest(p2tr, StandardPurpose.p2tr);
});

// test("Sign p2tr with sigHashType", async () => {
// const testTx = JSON.parse(JSON.stringify(p2tr));
// testTx.vin.forEach((input: CoreInput) => {
// // Test SIGHASH_SINGLE | SIGHASH_ANYONECANPAY, 0x83
// const sig = input.txinwitness![0] + "83";
// input.txinwitness = [sig];
// });
// await runSignTransactionNoVerification(testTx, StandardPurpose.p2tr);
// // The verification of the sighashtype is done in MockClient.signPsbt
// });
test("Sign p2tr with sigHashType", async () => {
const testTx = JSON.parse(JSON.stringify(p2tr));
testTx.vin.forEach((input: CoreInput) => {
// Test SIGHASH_SINGLE | SIGHASH_ANYONECANPAY, 0x83
const sig = input.txinwitness![0] + "83";
input.txinwitness = [sig];
});
await runSignTransactionNoVerification(testTx, StandardPurpose.p2tr);
// The verification of the sighashtype is done in MockClient.signPsbt
});

// test("Sign p2tr sequence 0", async () => {
// const testTx = JSON.parse(JSON.stringify(p2tr));
// testTx.vin.forEach((input: CoreInput) => {
// input.sequence = 0;
// });
// const tx = await runSignTransactionNoVerification(testTx, StandardPurpose.p2tr);
// const txObj = splitTransaction(tx, true);
// txObj.inputs.forEach(input => {
// expect(input.sequence.toString("hex")).toEqual("00000000");
// });
// });
test("Sign p2tr sequence 0", async () => {
const testTx = JSON.parse(JSON.stringify(p2tr));
testTx.vin.forEach((input: CoreInput) => {
input.sequence = 0;
});
const tx = await runSignTransactionNoVerification(testTx, StandardPurpose.p2tr);
const txObj = splitTransaction(tx, true);
txObj.inputs.forEach(input => {
expect(input.sequence.toString("hex")).toEqual("00000000");
});
});

async function runSignTransactionTest(
testTx: CoreTx,
Expand Down Expand Up @@ -163,7 +167,6 @@ async function testGetWalletPublicKey(
);

const btcNew = new BtcNew(client);
log('test', 'workok')
const addressFormat = addressFormatFromDescriptorTemplate(expectedDescriptorTemplate);
const result = await btcNew.getWalletPublicKey(path, { format: addressFormat });
log('address', result.bitcoinAddress)
Expand All @@ -173,6 +176,57 @@ async function testGetWalletPublicKey(
verifyGetWalletPublicKeyResult(resultAccount, accountXpub);
}

async function testGetWalletPublicKeyRealClient(
accountPath: string,
expectedDescriptorTemplate: DefaultDescriptorTemplate,
) {
const transport = await openTransportReplayer(RecordStore.fromString(`
=> e10000001600058000005480000001800000000000000000000000
<= 74707562444742337234346d37666a654c36676a6b75456e6e5a6e586677646e7a71524b6b713961636576354d464755653631766179326e6e3270427a56654e546d613563745733415746524a644c65707a737a35503332676a4d426268687837684b6434664a4d587361635a364c9000
=> e10000000e0003800000548000000180000000
<= 747075624443744b66734e795268554c6a5a39584d5334564b4b7456635064564469384d4b5562635344394d4a44796a52753141324e44354d6969706f7a797973704254396267387570457037613845416746784e78586e316437516b64624c35325479356a69534c6378507431509000
=> e105000000
<= f5acc2fd9000
=> e10300004600893198490079bb0667594bfa902274b1cd28d7ed9df140121dc69021bead679f00000000000000000000000000000000000000000000000000000000000000000000000000
<= 4000893198490079bb0667594bfa902274b1cd28d7ed9df140121dc69021bead679fe000
=> f80100002e2c2c01000877706b682840302901ac0efe99ff9d292d0e7bd2eab1b24480f963c52afe2e655d966827d3efbbe340
<= 41ac0efe99ff9d292d0e7bd2eab1b24480f963c52afe2e655d966827d3efbbe3400100e000
=> f801000022ac0efe99ff9d292d0e7bd2eab1b24480f963c52afe2e655d966827d3efbbe3400000
<= 4000ac0efe99ff9d292d0e7bd2eab1b24480f963c52afe2e655d966827d3efbbe340e000
=> f8010000898787005b66356163633266642f3834272f31272f30275d747075624443744b66734e795268554c6a5a39584d5334564b4b7456635064564469384d4b5562635344394d4a44796a52753141324e44354d6969706f7a797973704254396267387570457037613845416746784e78586e316437516b64624c35325479356a69534c6378507431502f2a2a
<= 41ac0efe99ff9d292d0e7bd2eab1b24480f963c52afe2e655d966827d3efbbe3400100e000
=> f801000022ac0efe99ff9d292d0e7bd2eab1b24480f963c52afe2e655d966827d3efbbe3400000
<= 4000ac0efe99ff9d292d0e7bd2eab1b24480f963c52afe2e655d966827d3efbbe340e000
=> f8010000898787005b66356163633266642f3834272f31272f30275d747075624443744b66734e795268554c6a5a39584d5334564b4b7456635064564469384d4b5562635344394d4a44796a52753141324e44354d6969706f7a797973704254396267387570457037613845416746784e78586e316437516b64624c35325479356a69534c6378507431502f2a2a
<= 746231717a647237733273723064776d6b777830333372346e756a7a6b38367530637936666d7a666a6b9000
=> e10000000e0003800000548000000180000000
<= 747075624443744b66734e795268554c6a5a39584d5334564b4b7456635064564469384d4b5562635344394d4a44796a52753141324e44354d6969706f7a797973704254396267387570457037613845416746784e78586e316437516b64624c35325479356a69534c6378507431509000
`));
const client = new AppClient(transport);
const path = accountPath + "/0/0";
// const accountXpub =
// "tpubDCwYjpDhUdPGP5rS3wgNg13mTrrjBuG8V9VpWbyptX6TRPbNoZVXsoVUSkCjmQ8jJycjuDKBb9eataSymXakTTaGifxR6kmVsfFehH1ZgJT";
// const keyXpub =
// "tpubDHcN44A4UHqdHJZwBxgTbu8Cy87ZrZkN8tQnmJGhcijHqe4rztuvGcD4wo36XSviLmiqL5fUbDnekYaQ7LzAnaqauBb9RsyahsTTFHdeJGd";
// client.getPubkeyResponse(accountPath, accountXpub);
// client.mockGetPubkeyResponse(path, keyXpub);
// const key = `[${masterFingerprint.toString("hex")}${accountPath.substring(1)}]${accountXpub}/**`;
// client.mockGetWalletAddressResponse(
// new WalletPolicy(expectedDescriptorTemplate, key),
// 0,
// 0,
// "testaddress",
// );

const btcNew = new BtcNew(client);
const addressFormat = addressFormatFromDescriptorTemplate(expectedDescriptorTemplate);
const result = await btcNew.getWalletPublicKey(path, { format: addressFormat });
log('address', result.bitcoinAddress)
// verifyGetWalletPublicKeyResult(result, keyXpub, "testaddress");
const resultAccount = await btcNew.getWalletPublicKey(accountPath);
// verifyGetWalletPublicKeyResult(resultAccount, accountXpub);
}

function verifyGetWalletPublicKeyResult(
result: { publicKey: string; bitcoinAddress: string; chainCode: string },
expectedXpub: string,
Expand Down

0 comments on commit 8fc7f21

Please sign in to comment.