Skip to content

Commit

Permalink
chore: sign txn proto, test cases and flow update
Browse files Browse the repository at this point in the history
  • Loading branch information
Suraj-Tiwari committed Jul 14, 2023
1 parent 586ba64 commit 8980e19
Show file tree
Hide file tree
Showing 6 changed files with 225 additions and 144 deletions.
4 changes: 0 additions & 4 deletions packages/app-btc/src/operations/signTxn/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ export const assertSignTxnParams = (params: ISignTxnParams) => {
output.chainIndex,
`txn.outputs[${i}].chainIndex should be define when it's a change output`,
);
assert(
output.addressIndex,
`txn.outputs[${i}].addressIndex should be define when it's a change output`,
);
}
}
};
30 changes: 12 additions & 18 deletions packages/app-btc/src/operations/signTxn/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,52 +62,46 @@ export const signTxn = async (
meta: {
version: signTxnDefaultParams.version,
locktime: params.txn.locktime ?? signTxnDefaultParams.locktime,
inputSize: params.txn.inputs.length,
outputSize: params.txn.inputs.length,
hashType: params.txn.hashType ?? signTxnDefaultParams.hashtype,
inputCount: params.txn.inputs.length,
outputCount: params.txn.outputs.length,
sighash: params.txn.hashType ?? signTxnDefaultParams.hashtype,
},
});
const { metaAccepted } = await helper.waitForResult();
assertOrThrowInvalidResult(metaAccepted);

for (let i = 0; i < params.txn.inputs.length; i += 1) {
const { input: inputRequest } = await helper.waitForResult();
assertOrThrowInvalidResult(inputRequest);
assertOrThrowInvalidResult(inputRequest.index === i);

const input = params.txn.inputs[i];
await helper.sendQuery({
input: {
prevTxn: hexToUint8Array(input.prevTxn),
prevTxnHash: hexToUint8Array(input.prevTxnHash),
prevIndex: input.prevIndex,
prevOutputIndex: input.prevIndex,
scriptPubKey: hexToUint8Array(input.scriptPubKey),
value: input.value,
sequence: input.sequence ?? signTxnDefaultParams.input.sequence,
chainIndex: input.chainIndex,
changeIndex: input.chainIndex,
addressIndex: input.addressIndex,
},
});
const { inputAccepted } = await helper.waitForResult();
assertOrThrowInvalidResult(inputAccepted);
}

for (let i = 0; i < params.txn.outputs.length; i += 1) {
const { output: outputRequest } = await helper.waitForResult();
assertOrThrowInvalidResult(outputRequest);
assertOrThrowInvalidResult(outputRequest.index === i);

const output = params.txn.outputs[i];
await helper.sendQuery({
output: {
scriptPubKey: hexToUint8Array(output.scriptPubKey),
value: output.value,
isChange: output.isChange,
chainIndex: output.chainIndex,
addressIndex: output.addressIndex,
changesIndex: output.chainIndex,
},
});
const { outputAccepted } = await helper.waitForResult();
assertOrThrowInvalidResult(outputAccepted);
}

const { verified } = await helper.waitForResult();
assertOrThrowInvalidResult(verified);

forceStatusUpdate(SignTxnStatus.SIGN_TXN_STATUS_VERIFY);

const signatures: string[] = [];
Expand Down
8 changes: 4 additions & 4 deletions packages/app-btc/src/operations/signTxn/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { SignTxnStatus } from '../../proto/generated/types';

export type SignTxnEventHandler = (event: SignTxnStatus) => void;

export interface ISignTxnInput {
export interface ISignTxnInputData {
prevTxnHash: string;
prevIndex: number;
value: string;
Expand All @@ -15,7 +15,7 @@ export interface ISignTxnInput {
sequence?: number;
}

export interface ISignTxnOutput {
export interface ISignTxnOutputData {
value: string;
scriptPubKey: string;

Expand All @@ -30,8 +30,8 @@ export interface ISignTxnParams {
onEvent?: SignTxnEventHandler;

txn: {
inputs: ISignTxnInput[];
outputs: ISignTxnOutput[];
inputs: ISignTxnInputData[];
outputs: ISignTxnOutputData[];
locktime?: number;
hashType?: number;
};
Expand Down
Loading

0 comments on commit 8980e19

Please sign in to comment.