Skip to content

Commit

Permalink
✨ Export tx file for ckb-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
doitian committed Mar 5, 2024
1 parent c799815 commit b5ad858
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
1 change: 0 additions & 1 deletion src/TransactionPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ function ResolveInputs({ endpoint, transaction, resolveInputs }) {
);
setState({ isProcessing: false, error: null });
} catch (error) {
console.error(error.stack);
setState({
isProcessing: false,
error: `Failed to resolve inputs: ${error}`,
Expand Down
6 changes: 6 additions & 0 deletions src/lib/ckb-address.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,9 @@ export function decodeDeprecatedSecp256k1Address(address) {
// ignore
}
}

export function encodeDeprecatedSecp256k1Address(args) {
const data = [1, 0, ...Array.from(decodeHex(args.slice(2)))];
const words = bech32.toWords(data);
return bech32.encode("ckt", words, 1023);
}
40 changes: 39 additions & 1 deletion src/lib/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
SECP256K1_CODE_HASH,
SECP256K1_MULTISIG_CODE_HASH,
decodeCkbAddress,
encodeDeprecatedSecp256k1Address,
} from "./ckb-address.js";
import { importMultisigAddresses } from "./multisig-address.js";
import {
Expand Down Expand Up @@ -603,7 +604,7 @@ export function exportBuildingPacket(bp, format) {
for (const output of bp.value.payload.outputs) {
fee = fee - BigInt(output.capacity);
}
for (const output of bp.value.payload.outputs) {
for (const output of bp.value.resolved_inputs.outputs) {
const lockHash =
"0x" +
ckbHasher()
Expand Down Expand Up @@ -716,6 +717,43 @@ export function exportBuildingPacket(bp, format) {
status: "PartiallySigned",
context: [bp.value.payload],
};
} else if (format === "ckb-cli") {
// eslint-disable-next-line no-unused-vars
const { hash, ...transaction } = bp.value.payload;
const signatures = {};
const multisig_configs = {};
for (const output of bp.value.resolved_inputs.outputs) {
if (
output.lock.code_hash === SECP256K1_MULTISIG_CODE_HASH_HEX &&
!(output.lock.args in multisig_configs)
) {
const lockHash =
"0x" +
ckbHasher()
.update(Script.pack(Script.parse(output.lock)))
.digest("hex");
const action = bp.value.lock_actions.find(
(item) => item.script_hash === lockHash,
);
const da = toJson(
MultisigAction.unpack(decodeHex(action.data.slice(2))),
);
multisig_configs[output.lock.args] = {
sighash_addresses: da.config.signer_pubkey_hashes.map((args) =>
encodeDeprecatedSecp256k1Address(args),
),
require_first_n: parseInt(da.config.require_first_n, 16),
threshold: parseInt(da.config.threshold, 16),
};
signatures[output.lock.args] = da.signed.map((item) => item.signature);
}
}

return {
transaction,
signatures,
multisig_configs,
};
}

return bp;
Expand Down

0 comments on commit b5ad858

Please sign in to comment.