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

fix: Support exchange the raw tx from cli. #128

Merged
merged 1 commit into from
Jul 29, 2024
Merged
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
2 changes: 1 addition & 1 deletion packages/neuron/src/pages/tools/index.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const Download: NextPage<PageProps> = () => {
.text()
.then(res =>
exportTxToSign({
tx: JSON.parse(res),
json: JSON.parse(res),
nodeType: selectedNodeType,
}),
)
Expand Down
68 changes: 66 additions & 2 deletions packages/neuron/src/utils/export-tx-to-sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ interface Transaction {
}

interface JSONTx {
cellDeps: {
outPoint: {
txHash: string
index: string
}
depType: string
}[]
inputs: {
since: string
previousOutput: {
Expand All @@ -33,6 +40,31 @@ interface JSONTx {
}[]
}

interface JSONTxFromCli {
cell_deps: {
out_point: {
tx_hash: string
index: string
}
dep_type: string
}[]
inputs: {
since: string
previous_output: {
tx_hash: string
index: string
}
}[]
outputs: {
capacity: string
lock: {
code_hash: string
hash_type: string
args: string
}
}[]
}

function checkIsObject(obj: unknown): obj is object {
return typeof obj === 'object' && obj !== null
}
Expand All @@ -42,7 +74,7 @@ export class JSONFormatError extends Error {
type = JSON_FORMAT_ERROR_TYPE
}

function checkIsTx(json: unknown): json is JSONTx {
function checkIsTx(json: unknown): json is JSONTx | JSONTxFromCli {
if (!checkIsObject(json)) throw new JSONFormatError('JSON format is incorrect')
if (!('inputs' in json)) throw new JSONFormatError(`Tx doesn't have inputs field`)
if (!('outputs' in json)) throw new JSONFormatError(`Tx doesn't have outputs field`)
Expand All @@ -51,9 +83,36 @@ function checkIsTx(json: unknown): json is JSONTx {
return true
}

export default async function exportTxToSign({ tx, nodeType }: { tx: unknown; nodeType: string }) {
const deepCamelizeKeys = (item: unknown): unknown => {
if (Array.isArray(item)) {
return item.map((el: unknown) => deepCamelizeKeys(el))
} else if (typeof item === 'function' || item !== Object(item)) {
return item
}
return Object.fromEntries(
Object.entries(item as Record<string, unknown>).map(([key, value]: [string, unknown]) => [
key.replace(/([-_][a-z])/gi, c => c.toUpperCase().replace(/[-_]/g, '')),
deepCamelizeKeys(value),
]),
)
}

function isJSONTxFromCli(tx: JSONTxFromCli | JSONTx): tx is JSONTxFromCli {
return !!tx.inputs[0] && 'previous_output' in tx.inputs[0]
}

export default async function exportTxToSign({ json, nodeType }: { json: unknown; nodeType: string }) {
const ckbNodeUrl: string = nodeType === 'mainnet' ? CKB_MAINNET_NODE_URL : CKB_TESTNET_NODE_URL
let tx
if (checkIsObject(json) && 'transaction' in json) {
tx = json.transaction
} else {
tx = json
}
if (!checkIsTx(tx)) return
if (isJSONTxFromCli(tx)) {
tx = deepCamelizeKeys(tx) as JSONTx
}
const context = await fetch(ckbNodeUrl, {
method: 'POST',
headers: {
Expand Down Expand Up @@ -85,6 +144,7 @@ export default async function exportTxToSign({ tx, nodeType }: { tx: unknown; no
args: output.lock.args,
}
return {
...input,
capacity: output.capacity,
lock: lockScript,
lockHash: utils.computeScriptHash(lockScript as Script),
Expand All @@ -100,6 +160,10 @@ export default async function exportTxToSign({ tx, nodeType }: { tx: unknown; no
fee: (outputCapacity - inputsCapacity).toString(),
...tx,
inputs,
cellDeps: tx.cellDeps.map(v => ({
...v,
depType: v.depType === 'dep_group' ? 'depGroup' : v.depType,
})),
},
context,
type: 'Regular',
Expand Down