Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force pubkey <> address ordering in 'prepareTransaction' within QiHDWallet #395

Open
wants to merge 6 commits into
base: alpha
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions src/_tests/unit/wallet.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@ import { loadTests } from '../utils.js';

import type { TestCaseAccount, TestCaseTypedData, TestCaseTransaction } from '../types.js';

import {
// hexlify,
// randomBytes,
Wallet,
} from '../../index.js';

// import type { QuaiHDWallet } from "../index.js";
import { Wallet } from '../../index.js';

describe('Test Private Key Wallet', function () {
const tests = loadTests<TestCaseAccount>('accounts');
Expand Down Expand Up @@ -39,10 +33,6 @@ describe('Test Transaction Signing', function () {
gasLimit: 0,
});
const signed = await wallet.signTransaction(txData);
// let parsed = Transaction.from(signed);
// // console.log('txData: ', JSON.stringify(parsed))
// // console.log('EXPECTED: ', test.signedEip155)
// // console.log("ACTUAL: ", signed)
assert.equal(signed, test.signed, 'signed');
});
}
Expand Down
56 changes: 30 additions & 26 deletions src/wallet/qi-hdwallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -697,23 +697,10 @@ export class QiHDWallet extends AbstractHDWallet<QiAddressInfo> {
const sendAddressesInfo = this.getUnusedBIP44Addresses(1, 0, 'BIP44:external', zone);
const sendAddresses = sendAddressesInfo.map((addressInfo) => addressInfo.address);
const changeAddresses: string[] = [];
const inputPubKeys = selection.inputs.map((input) => {
const addressInfo = this.locateAddressInfo(input.address);
if (!addressInfo) {
throw new Error(`Could not locate address info for address: ${input.address}`);
}
return addressInfo.pubKey;
});

// Proceed with creating and signing the transaction
const chainId = (await this.provider.getNetwork()).chainId;
const tx = await this.prepareTransaction(
selection,
inputPubKeys,
sendAddresses,
changeAddresses,
Number(chainId),
);
const tx = await this.prepareTransaction(selection, sendAddresses, changeAddresses, Number(chainId));

// Sign the transaction
const signedTx = await this.signTransaction(tx);
Expand Down Expand Up @@ -845,13 +832,7 @@ export class QiHDWallet extends AbstractHDWallet<QiAddressInfo> {

// Proceed with creating and signing the transaction
const chainId = (await this.provider.getNetwork()).chainId;
const tx = await this.prepareTransaction(
selection,
inputPubKeys.map((pubkey) => pubkey!),
sendAddresses,
changeAddresses,
Number(chainId),
);
const tx = await this.prepareTransaction(selection, sendAddresses, changeAddresses, Number(chainId));

// Sign the transaction
const signedTx = await this.signTransaction(tx);
Expand All @@ -872,17 +853,40 @@ export class QiHDWallet extends AbstractHDWallet<QiAddressInfo> {
*/
private async prepareTransaction(
selection: SelectedCoinsResult,
inputPubKeys: string[],
sendAddresses: string[],
changeAddresses: string[],
chainId: number,
): Promise<QiTransaction> {
const tx = new QiTransaction();
tx.txInputs = selection.inputs.map((input, index) => ({
txhash: input.txhash!,
index: input.index!,
pubkey: inputPubKeys[index],

interface InputWithPubKey {
utxo: UTXO;
pubKey: string;
}

const inputsWithPubKeys: InputWithPubKey[] = selection.inputs.map((input) => {
const addressInfo = this.locateAddressInfo(input.address);
if (!addressInfo?.pubKey) {
throw new Error(`Missing public key for input address: ${input.address}`);
}
return {
utxo: input,
pubKey: addressInfo.pubKey,
};
});

tx.txInputs = inputsWithPubKeys.map((input) => ({
txhash: input.utxo.txhash!,
index: input.utxo.index!,
pubkey: input.pubKey,
}));

// // 5.1 Create the "sender" inputs
// tx.txInputs = selection.inputs.map((input, index) => ({
// txhash: input.txhash!,
// index: input.index!,
// pubkey: inputPubKeys[index],
// }));
// 5.3 Create the "sender" outputs
const senderOutputs = selection.spendOutputs.map((output, index) => ({
address: sendAddresses[index],
Expand Down
Binary file modified testcases/qi-wallet-scan-and-convert-to-quai.json.gz
Binary file not shown.
Binary file modified testcases/qi-wallet-scan-and-send.json.gz
Binary file not shown.
Binary file modified testcases/quai-transaction.json.gz
Binary file not shown.
Binary file modified testcases/transactions.json.gz
Binary file not shown.