From 36773081e690ef105df96c972b6ef7e499baeb58 Mon Sep 17 00:00:00 2001 From: devchenyan Date: Mon, 27 May 2024 02:13:32 +0800 Subject: [PATCH 1/8] feat: compatible ckb-cli tx multisig file --- .../neuron-wallet/src/controllers/multisig.ts | 10 ++++- .../neuron-wallet/src/models/offline-sign.ts | 1 + packages/neuron-wallet/src/utils/hump.ts | 25 ++++++++++++ packages/neuron-wallet/src/utils/multisig.ts | 39 +++++++++++++++++++ 4 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 packages/neuron-wallet/src/utils/hump.ts diff --git a/packages/neuron-wallet/src/controllers/multisig.ts b/packages/neuron-wallet/src/controllers/multisig.ts index ea41a69bb1..d9211d06fd 100644 --- a/packages/neuron-wallet/src/controllers/multisig.ts +++ b/packages/neuron-wallet/src/controllers/multisig.ts @@ -5,6 +5,7 @@ import { t } from 'i18next' import { computeScriptHash as scriptToHash } from '@ckb-lumos/base/lib/utils' import { scriptToAddress, addressToScript } from '../utils/scriptAndAddress' import { ResponseCode } from '../utils/const' +import { parseMultisigTxJsonFromCkbCli } from '../utils/multisig' import MultisigConfig from '../database/chain/entities/multisig-config' import MultisigConfigModel from '../models/multisig-config' import MultisigService from '../services/multisig' @@ -214,7 +215,12 @@ export default class MultisigController { } const tx = result.json const lockHash = scriptToHash(addressToScript(fullPayload)) - if (tx.transaction.inputs.every(v => v.lockHash !== lockHash)) { + + if (tx.transaction && tx?.multisig_configs) { + tx.transaction = parseMultisigTxJsonFromCkbCli(tx) + } + + if (tx.transaction.inputs.every(v => v.lockHash && v.lockHash !== lockHash)) { ShowGlobalDialogSubject.next({ type: 'failed', title: t('common.error'), @@ -226,7 +232,7 @@ export default class MultisigController { } return { status: ResponseCode.Success, - result: result?.json, + result: tx, } } } diff --git a/packages/neuron-wallet/src/models/offline-sign.ts b/packages/neuron-wallet/src/models/offline-sign.ts index 2fd73a0d93..934f5d3ad9 100644 --- a/packages/neuron-wallet/src/models/offline-sign.ts +++ b/packages/neuron-wallet/src/models/offline-sign.ts @@ -44,6 +44,7 @@ export interface OfflineSignJSON { description?: string asset_account?: AssetAccount multisig_configs?: MultisigConfigs + signatures?: Signatures } export default class OfflineSign implements OfflineSignProps { diff --git a/packages/neuron-wallet/src/utils/hump.ts b/packages/neuron-wallet/src/utils/hump.ts new file mode 100644 index 0000000000..b618c5f918 --- /dev/null +++ b/packages/neuron-wallet/src/utils/hump.ts @@ -0,0 +1,25 @@ +export function hump(param: any) { + Object.keys(param).map(key => { + let item = param[key] + if (item instanceof Object || item instanceof Array) { + hump(item) + } + if (humpString(key) !== key) { + param[humpString(key)] = param[key] + delete param[key] + } + }) + return param +} + +export function humpString(key: string) { + let keyArr = key.split('_') + for (let i = 0; i < keyArr.length; i++) { + if (i !== 0) { + keyArr[i] = keyArr[i][0].toUpperCase() + keyArr[i].substr(1) + } + } + return keyArr.join('') +} + +export default { hump, humpString } diff --git a/packages/neuron-wallet/src/utils/multisig.ts b/packages/neuron-wallet/src/utils/multisig.ts index 4ef4e7c058..6254f6bb74 100644 --- a/packages/neuron-wallet/src/utils/multisig.ts +++ b/packages/neuron-wallet/src/utils/multisig.ts @@ -2,6 +2,12 @@ import { computeScriptHash as scriptToHash } from '@ckb-lumos/base/lib/utils' import Multisig from '../models/multisig' import MultisigConfigModel from '../models/multisig-config' import { Signatures, SignStatus } from '../models/offline-sign' +import { OfflineSignJSON } from 'src/models/offline-sign' +import Transaction from '../models/chain/transaction' +import SystemScriptInfo from '../models/system-script-info' +import Input from '../models/chain/input' +import { hump } from './hump' +import { DepType } from '../models/chain/cell-dep' export const getMultisigStatus = (multisigConfig: MultisigConfigModel, signatures: Signatures) => { const multisigLockHash = scriptToHash( @@ -27,6 +33,39 @@ export const getMultisigStatus = (multisigConfig: MultisigConfigModel, signature return SignStatus.Signed } +export const parseMultisigTxJsonFromCkbCli = (tx: OfflineSignJSON): Transaction => { + const { multisig_configs } = tx + const txObj = hump(tx.transaction) + + if (multisig_configs) { + const args = Object.keys(multisig_configs)[0] + const lock = SystemScriptInfo.generateMultiSignScript(args) + + txObj.inputs.forEach((input: Input) => { + if (!input?.lock) { + input.lock = lock + } + }) + } + + if (!tx.transaction?.signatures && tx?.signatures) { + tx.transaction.signatures = tx.signatures + delete tx.signatures + } + + if (tx.transaction.cellDeps) { + tx.transaction.cellDeps.forEach(item => { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-expect-error + if (item.depType === 'dep_group') { + item.depType = DepType.DepGroup + } + }) + } + return txObj +} + export default { getMultisigStatus, + parseMultisigTxJsonFromCkbCli, } From 5bfb7078b792d1248a310b072586a09fcaa512eb Mon Sep 17 00:00:00 2001 From: devchenyan Date: Mon, 27 May 2024 11:54:41 +0800 Subject: [PATCH 2/8] fix: comments --- .../src/models/chain/cell-dep.ts | 5 +- .../src/utils/{hump.ts => json-to-hump.ts} | 12 +-- packages/neuron-wallet/src/utils/multisig.ts | 26 ++--- .../tests/utils/json-to-hump/fixtures.json | 101 ++++++++++++++++++ .../tests/utils/json-to-hump/index.test.ts | 9 ++ 5 files changed, 125 insertions(+), 28 deletions(-) rename packages/neuron-wallet/src/utils/{hump.ts => json-to-hump.ts} (61%) create mode 100644 packages/neuron-wallet/tests/utils/json-to-hump/fixtures.json create mode 100644 packages/neuron-wallet/tests/utils/json-to-hump/index.test.ts diff --git a/packages/neuron-wallet/src/models/chain/cell-dep.ts b/packages/neuron-wallet/src/models/chain/cell-dep.ts index c8830809ed..4e128b469c 100644 --- a/packages/neuron-wallet/src/models/chain/cell-dep.ts +++ b/packages/neuron-wallet/src/models/chain/cell-dep.ts @@ -14,8 +14,9 @@ export default class CellDep { this.depType = depType } - public static fromObject({ outPoint, depType }: { outPoint: OutPoint; depType: DepType }): CellDep { - return new CellDep(OutPoint.fromObject(outPoint), depType) + public static fromObject({ outPoint, depType }: { outPoint: OutPoint; depType: DepType | 'dep_group' }): CellDep { + const _depType = depType == 'dep_group' ? DepType.DepGroup : depType + return new CellDep(OutPoint.fromObject(outPoint), _depType) } public toSDK(): CKBComponents.CellDep { diff --git a/packages/neuron-wallet/src/utils/hump.ts b/packages/neuron-wallet/src/utils/json-to-hump.ts similarity index 61% rename from packages/neuron-wallet/src/utils/hump.ts rename to packages/neuron-wallet/src/utils/json-to-hump.ts index b618c5f918..cc1dc6da93 100644 --- a/packages/neuron-wallet/src/utils/hump.ts +++ b/packages/neuron-wallet/src/utils/json-to-hump.ts @@ -1,18 +1,18 @@ -export function hump(param: any) { +export function jsonToHump(param: any) { Object.keys(param).map(key => { let item = param[key] if (item instanceof Object || item instanceof Array) { - hump(item) + jsonToHump(item) } - if (humpString(key) !== key) { - param[humpString(key)] = param[key] + if (stringToHump(key) !== key) { + param[stringToHump(key)] = param[key] delete param[key] } }) return param } -export function humpString(key: string) { +export function stringToHump(key: string) { let keyArr = key.split('_') for (let i = 0; i < keyArr.length; i++) { if (i !== 0) { @@ -22,4 +22,4 @@ export function humpString(key: string) { return keyArr.join('') } -export default { hump, humpString } +export default { jsonToHump, stringToHump } diff --git a/packages/neuron-wallet/src/utils/multisig.ts b/packages/neuron-wallet/src/utils/multisig.ts index 6254f6bb74..8eb11ebb70 100644 --- a/packages/neuron-wallet/src/utils/multisig.ts +++ b/packages/neuron-wallet/src/utils/multisig.ts @@ -6,8 +6,7 @@ import { OfflineSignJSON } from 'src/models/offline-sign' import Transaction from '../models/chain/transaction' import SystemScriptInfo from '../models/system-script-info' import Input from '../models/chain/input' -import { hump } from './hump' -import { DepType } from '../models/chain/cell-dep' +import { jsonToHump } from './json-to-hump' export const getMultisigStatus = (multisigConfig: MultisigConfigModel, signatures: Signatures) => { const multisigLockHash = scriptToHash( @@ -34,10 +33,9 @@ export const getMultisigStatus = (multisigConfig: MultisigConfigModel, signature } export const parseMultisigTxJsonFromCkbCli = (tx: OfflineSignJSON): Transaction => { - const { multisig_configs } = tx - const txObj = hump(tx.transaction) - - if (multisig_configs) { + const { multisig_configs, transaction } = tx + const txObj = Transaction.fromObject(jsonToHump(transaction)) + if (multisig_configs && Object.keys(multisig_configs).length) { const args = Object.keys(multisig_configs)[0] const lock = SystemScriptInfo.generateMultiSignScript(args) @@ -47,20 +45,8 @@ export const parseMultisigTxJsonFromCkbCli = (tx: OfflineSignJSON): Transaction } }) } - - if (!tx.transaction?.signatures && tx?.signatures) { - tx.transaction.signatures = tx.signatures - delete tx.signatures - } - - if (tx.transaction.cellDeps) { - tx.transaction.cellDeps.forEach(item => { - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-expect-error - if (item.depType === 'dep_group') { - item.depType = DepType.DepGroup - } - }) + if (!txObj?.signatures && tx?.signatures) { + txObj.signatures = tx.signatures } return txObj } diff --git a/packages/neuron-wallet/tests/utils/json-to-hump/fixtures.json b/packages/neuron-wallet/tests/utils/json-to-hump/fixtures.json new file mode 100644 index 0000000000..1919e853f7 --- /dev/null +++ b/packages/neuron-wallet/tests/utils/json-to-hump/fixtures.json @@ -0,0 +1,101 @@ +{ + "value": { + "version": "0x0", + "cell_deps": [ + { + "out_point": { + "tx_hash": "0xf8de3bb47d055cdf460d93a2a6e1b05f7432f9777c8c474abf4eec1d4aee5d37", + "index": "0x1" + }, + "dep_type": "dep_group" + } + ], + "header_deps": [], + "inputs": [ + { + "since": "0x0", + "previous_output": { + "tx_hash": "0x00bc60fd23dd556a9bd139791e4cb95d678550054953793b79c864abcc733eae", + "index": "0x1" + } + }, + { + "previous_output": { + "txHash": "0x8b37ded770fe457e1d3969e93722873308aef1e6810757d248f437de4d009d8f", + "index": "0x1" + }, + "since": "0x0" + } + ], + "outputs": [ + { + "capacity": "6600000000", + "lock": { + "args": "0x2d8765d2d0e007aa227e61d9bc1ebd7343360a58", + "code_hash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", + "hash_type": "type" + }, + "type": null + }, + { + "capacity": "10399999375", + "lock": { + "args": "0x5b34cc4f76644ec6da564c1ace41711ee0f1e071", + "code_hash": "0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8", + "hash_type": "type" + }, + "type": null + } + ], + "outputs_data": ["0x", "0x"], + "witnesses": [] + }, + "expected": { + "version": "0x0", + "inputs": [ + { + "since": "0x0", + "previousOutput": { + "index": "0x1", + "txHash": "0x00bc60fd23dd556a9bd139791e4cb95d678550054953793b79c864abcc733eae" + } + }, + { + "since": "0x0", + "previousOutput": { + "txHash": "0x8b37ded770fe457e1d3969e93722873308aef1e6810757d248f437de4d009d8f", + "index": "0x1" + } + } + ], + "outputs": [ + { + "capacity": "6600000000", + "lock": { + "args": "0x2d8765d2d0e007aa227e61d9bc1ebd7343360a58", + "codeHash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", + "hashType": "type" + }, + "type": null + }, + { + "capacity": "10399999375", + "lock": { + "args": "0x5b34cc4f76644ec6da564c1ace41711ee0f1e071", + "codeHash": "0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8", + "hashType": "type" + }, + "type": null + } + ], + "witnesses": [], + "cellDeps": [ + { + "outPoint": { "index": "0x1", "txHash": "0xf8de3bb47d055cdf460d93a2a6e1b05f7432f9777c8c474abf4eec1d4aee5d37" }, + "depType": "dep_group" + } + ], + "headerDeps": [], + "outputsData": ["0x", "0x"] + } +} diff --git a/packages/neuron-wallet/tests/utils/json-to-hump/index.test.ts b/packages/neuron-wallet/tests/utils/json-to-hump/index.test.ts new file mode 100644 index 0000000000..5e1c6956df --- /dev/null +++ b/packages/neuron-wallet/tests/utils/json-to-hump/index.test.ts @@ -0,0 +1,9 @@ +import { jsonToHump } from '../../../src/utils/json-to-hump' +import fixtures from './fixtures.json' + +describe('test json to hump', () => { + it('json to hump', () => { + const result = jsonToHump(fixtures.value) + expect(fixtures.expected).toEqual(result) + }) +}) From 625a274814175cd32828aca8d162268ed706844b Mon Sep 17 00:00:00 2001 From: devchenyan Date: Wed, 29 May 2024 21:52:00 +0800 Subject: [PATCH 3/8] fix: comments --- ...{json-to-hump.ts => deep-camelize-keys.ts} | 34 ++++++++++--------- packages/neuron-wallet/src/utils/multisig.ts | 5 +-- .../fixtures.json | 0 .../index.test.ts | 4 +-- 4 files changed, 23 insertions(+), 20 deletions(-) rename packages/neuron-wallet/src/utils/{json-to-hump.ts => deep-camelize-keys.ts} (50%) rename packages/neuron-wallet/tests/utils/{json-to-hump => deep-camelize-keys}/fixtures.json (100%) rename packages/neuron-wallet/tests/utils/{json-to-hump => deep-camelize-keys}/index.test.ts (56%) diff --git a/packages/neuron-wallet/src/utils/json-to-hump.ts b/packages/neuron-wallet/src/utils/deep-camelize-keys.ts similarity index 50% rename from packages/neuron-wallet/src/utils/json-to-hump.ts rename to packages/neuron-wallet/src/utils/deep-camelize-keys.ts index cc1dc6da93..9e0f5e21bd 100644 --- a/packages/neuron-wallet/src/utils/json-to-hump.ts +++ b/packages/neuron-wallet/src/utils/deep-camelize-keys.ts @@ -1,18 +1,4 @@ -export function jsonToHump(param: any) { - Object.keys(param).map(key => { - let item = param[key] - if (item instanceof Object || item instanceof Array) { - jsonToHump(item) - } - if (stringToHump(key) !== key) { - param[stringToHump(key)] = param[key] - delete param[key] - } - }) - return param -} - -export function stringToHump(key: string) { +function snakeToCamel(key: string) { let keyArr = key.split('_') for (let i = 0; i < keyArr.length; i++) { if (i !== 0) { @@ -22,4 +8,20 @@ export function stringToHump(key: string) { return keyArr.join('') } -export default { jsonToHump, stringToHump } +type Json = Record + +export function deepCamelizeKeys(param: Json): Json { + Object.keys(param).map(key => { + let item = param[key] + if (item instanceof Object) { + deepCamelizeKeys(item as Json) + } + if (snakeToCamel(key) !== key) { + param[snakeToCamel(key)] = param[key] + delete param[key] + } + }) + return param +} + +export default { deepCamelizeKeys } diff --git a/packages/neuron-wallet/src/utils/multisig.ts b/packages/neuron-wallet/src/utils/multisig.ts index 8eb11ebb70..39042f9a8e 100644 --- a/packages/neuron-wallet/src/utils/multisig.ts +++ b/packages/neuron-wallet/src/utils/multisig.ts @@ -6,7 +6,7 @@ import { OfflineSignJSON } from 'src/models/offline-sign' import Transaction from '../models/chain/transaction' import SystemScriptInfo from '../models/system-script-info' import Input from '../models/chain/input' -import { jsonToHump } from './json-to-hump' +import { deepCamelizeKeys } from './deep-camelize-keys' export const getMultisigStatus = (multisigConfig: MultisigConfigModel, signatures: Signatures) => { const multisigLockHash = scriptToHash( @@ -34,7 +34,8 @@ export const getMultisigStatus = (multisigConfig: MultisigConfigModel, signature export const parseMultisigTxJsonFromCkbCli = (tx: OfflineSignJSON): Transaction => { const { multisig_configs, transaction } = tx - const txObj = Transaction.fromObject(jsonToHump(transaction)) + // @ts-expect-error + const txObj = Transaction.fromObject(deepCamelizeKeys(transaction)) if (multisig_configs && Object.keys(multisig_configs).length) { const args = Object.keys(multisig_configs)[0] const lock = SystemScriptInfo.generateMultiSignScript(args) diff --git a/packages/neuron-wallet/tests/utils/json-to-hump/fixtures.json b/packages/neuron-wallet/tests/utils/deep-camelize-keys/fixtures.json similarity index 100% rename from packages/neuron-wallet/tests/utils/json-to-hump/fixtures.json rename to packages/neuron-wallet/tests/utils/deep-camelize-keys/fixtures.json diff --git a/packages/neuron-wallet/tests/utils/json-to-hump/index.test.ts b/packages/neuron-wallet/tests/utils/deep-camelize-keys/index.test.ts similarity index 56% rename from packages/neuron-wallet/tests/utils/json-to-hump/index.test.ts rename to packages/neuron-wallet/tests/utils/deep-camelize-keys/index.test.ts index 5e1c6956df..2545c058e7 100644 --- a/packages/neuron-wallet/tests/utils/json-to-hump/index.test.ts +++ b/packages/neuron-wallet/tests/utils/deep-camelize-keys/index.test.ts @@ -1,9 +1,9 @@ -import { jsonToHump } from '../../../src/utils/json-to-hump' +import { deepCamelizeKeys } from '../../../src/utils/deep-camelize-keys' import fixtures from './fixtures.json' describe('test json to hump', () => { it('json to hump', () => { - const result = jsonToHump(fixtures.value) + const result = deepCamelizeKeys(fixtures.value) expect(fixtures.expected).toEqual(result) }) }) From 4aad5dac8b0faae15fd29cb6b5d7e56add95199d Mon Sep 17 00:00:00 2001 From: devchenyan Date: Sat, 1 Jun 2024 13:46:34 +0800 Subject: [PATCH 4/8] fix: comments --- packages/neuron-wallet/src/controllers/multisig.ts | 2 +- packages/neuron-wallet/src/models/chain/cell-dep.ts | 5 +++-- packages/neuron-wallet/src/utils/deep-camelize-keys.ts | 4 ++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/neuron-wallet/src/controllers/multisig.ts b/packages/neuron-wallet/src/controllers/multisig.ts index d9211d06fd..152c39e357 100644 --- a/packages/neuron-wallet/src/controllers/multisig.ts +++ b/packages/neuron-wallet/src/controllers/multisig.ts @@ -216,7 +216,7 @@ export default class MultisigController { const tx = result.json const lockHash = scriptToHash(addressToScript(fullPayload)) - if (tx.transaction && tx?.multisig_configs) { + if (tx.transaction) { tx.transaction = parseMultisigTxJsonFromCkbCli(tx) } diff --git a/packages/neuron-wallet/src/models/chain/cell-dep.ts b/packages/neuron-wallet/src/models/chain/cell-dep.ts index 4e128b469c..f281246e20 100644 --- a/packages/neuron-wallet/src/models/chain/cell-dep.ts +++ b/packages/neuron-wallet/src/models/chain/cell-dep.ts @@ -1,4 +1,5 @@ import OutPoint from './out-point' +import { snakeToCamel } from '../../utils/deep-camelize-keys' export enum DepType { Code = 'code', @@ -14,8 +15,8 @@ export default class CellDep { this.depType = depType } - public static fromObject({ outPoint, depType }: { outPoint: OutPoint; depType: DepType | 'dep_group' }): CellDep { - const _depType = depType == 'dep_group' ? DepType.DepGroup : depType + public static fromObject({ outPoint, depType }: { outPoint: OutPoint; depType: DepType }): CellDep { + const _depType = snakeToCamel(depType) as DepType return new CellDep(OutPoint.fromObject(outPoint), _depType) } diff --git a/packages/neuron-wallet/src/utils/deep-camelize-keys.ts b/packages/neuron-wallet/src/utils/deep-camelize-keys.ts index 9e0f5e21bd..2a464dd68f 100644 --- a/packages/neuron-wallet/src/utils/deep-camelize-keys.ts +++ b/packages/neuron-wallet/src/utils/deep-camelize-keys.ts @@ -1,4 +1,4 @@ -function snakeToCamel(key: string) { +export function snakeToCamel(key: string) { let keyArr = key.split('_') for (let i = 0; i < keyArr.length; i++) { if (i !== 0) { @@ -24,4 +24,4 @@ export function deepCamelizeKeys(param: Json): Json { return param } -export default { deepCamelizeKeys } +export default { deepCamelizeKeys, snakeToCamel } From d4a5e5189897f03d53addda24584e93fce396394 Mon Sep 17 00:00:00 2001 From: devchenyan Date: Sat, 1 Jun 2024 15:46:12 +0800 Subject: [PATCH 5/8] fix --- packages/neuron-wallet/src/models/chain/transaction.ts | 2 +- packages/neuron-wallet/src/utils/deep-camelize-keys.ts | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/neuron-wallet/src/models/chain/transaction.ts b/packages/neuron-wallet/src/models/chain/transaction.ts index a8942bb43c..613143dbb4 100644 --- a/packages/neuron-wallet/src/models/chain/transaction.ts +++ b/packages/neuron-wallet/src/models/chain/transaction.ts @@ -123,7 +123,7 @@ export default class Transaction { this.description = description this.nervosDao = nervosDao this.assetAccountType = assetAccountType - this.version = BigInt(version).toString() + this.version = BigInt(version || '0x0').toString() this.value = value ? BigInt(value).toString() : value this.fee = fee ? BigInt(fee).toString() : fee this.interest = interest ? BigInt(interest).toString() : interest diff --git a/packages/neuron-wallet/src/utils/deep-camelize-keys.ts b/packages/neuron-wallet/src/utils/deep-camelize-keys.ts index 2a464dd68f..007e4ddc1d 100644 --- a/packages/neuron-wallet/src/utils/deep-camelize-keys.ts +++ b/packages/neuron-wallet/src/utils/deep-camelize-keys.ts @@ -1,4 +1,6 @@ export function snakeToCamel(key: string) { + if (!key) return '' + let keyArr = key.split('_') for (let i = 0; i < keyArr.length; i++) { if (i !== 0) { From d0e63634d4e8c5075d63da1d75e804a7a4318794 Mon Sep 17 00:00:00 2001 From: devchenyan Date: Wed, 5 Jun 2024 10:51:36 +0800 Subject: [PATCH 6/8] fix --- .../src/models/chain/cell-dep.ts | 2 +- .../src/utils/deep-camelize-keys.ts | 38 +++++++------------ 2 files changed, 15 insertions(+), 25 deletions(-) diff --git a/packages/neuron-wallet/src/models/chain/cell-dep.ts b/packages/neuron-wallet/src/models/chain/cell-dep.ts index f281246e20..03773180de 100644 --- a/packages/neuron-wallet/src/models/chain/cell-dep.ts +++ b/packages/neuron-wallet/src/models/chain/cell-dep.ts @@ -15,7 +15,7 @@ export default class CellDep { this.depType = depType } - public static fromObject({ outPoint, depType }: { outPoint: OutPoint; depType: DepType }): CellDep { + public static fromObject({ outPoint, depType }: { outPoint: OutPoint; depType: DepType | 'dep_group' }): CellDep { const _depType = snakeToCamel(depType) as DepType return new CellDep(OutPoint.fromObject(outPoint), _depType) } diff --git a/packages/neuron-wallet/src/utils/deep-camelize-keys.ts b/packages/neuron-wallet/src/utils/deep-camelize-keys.ts index 007e4ddc1d..678bfa730e 100644 --- a/packages/neuron-wallet/src/utils/deep-camelize-keys.ts +++ b/packages/neuron-wallet/src/utils/deep-camelize-keys.ts @@ -1,29 +1,19 @@ -export function snakeToCamel(key: string) { - if (!key) return '' - - let keyArr = key.split('_') - for (let i = 0; i < keyArr.length; i++) { - if (i !== 0) { - keyArr[i] = keyArr[i][0].toUpperCase() + keyArr[i].substr(1) - } - } - return keyArr.join('') +export const snakeToCamel = (str: string): string => { + return str.replace(/([-_][a-z])/gi, c => c.toUpperCase().replace(/[-_]/g, '')) } -type Json = Record - -export function deepCamelizeKeys(param: Json): Json { - Object.keys(param).map(key => { - let item = param[key] - if (item instanceof Object) { - deepCamelizeKeys(item as Json) - } - if (snakeToCamel(key) !== key) { - param[snakeToCamel(key)] = param[key] - delete param[key] - } - }) - return param +export 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).map(([key, value]: [string, unknown]) => [ + key.replace(/([-_][a-z])/gi, c => c.toUpperCase().replace(/[-_]/g, '')), + deepCamelizeKeys(value), + ]) + ) } export default { deepCamelizeKeys, snakeToCamel } From a638795942e1ed399ee3a1b9eb522ae8cd5d7a92 Mon Sep 17 00:00:00 2001 From: devchenyan Date: Wed, 5 Jun 2024 23:05:57 +0800 Subject: [PATCH 7/8] fix --- packages/neuron-wallet/src/models/chain/cell-dep.ts | 3 +-- packages/neuron-wallet/src/utils/deep-camelize-keys.ts | 1 + packages/neuron-wallet/src/utils/multisig.ts | 3 +-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/neuron-wallet/src/models/chain/cell-dep.ts b/packages/neuron-wallet/src/models/chain/cell-dep.ts index 03773180de..cb1979a065 100644 --- a/packages/neuron-wallet/src/models/chain/cell-dep.ts +++ b/packages/neuron-wallet/src/models/chain/cell-dep.ts @@ -16,8 +16,7 @@ export default class CellDep { } public static fromObject({ outPoint, depType }: { outPoint: OutPoint; depType: DepType | 'dep_group' }): CellDep { - const _depType = snakeToCamel(depType) as DepType - return new CellDep(OutPoint.fromObject(outPoint), _depType) + return new CellDep(OutPoint.fromObject(outPoint), snakeToCamel(depType) as DepType) } public toSDK(): CKBComponents.CellDep { diff --git a/packages/neuron-wallet/src/utils/deep-camelize-keys.ts b/packages/neuron-wallet/src/utils/deep-camelize-keys.ts index 678bfa730e..94343db948 100644 --- a/packages/neuron-wallet/src/utils/deep-camelize-keys.ts +++ b/packages/neuron-wallet/src/utils/deep-camelize-keys.ts @@ -1,4 +1,5 @@ export const snakeToCamel = (str: string): string => { + if (!str) return '' return str.replace(/([-_][a-z])/gi, c => c.toUpperCase().replace(/[-_]/g, '')) } diff --git a/packages/neuron-wallet/src/utils/multisig.ts b/packages/neuron-wallet/src/utils/multisig.ts index 39042f9a8e..5caef9529a 100644 --- a/packages/neuron-wallet/src/utils/multisig.ts +++ b/packages/neuron-wallet/src/utils/multisig.ts @@ -34,8 +34,7 @@ export const getMultisigStatus = (multisigConfig: MultisigConfigModel, signature export const parseMultisigTxJsonFromCkbCli = (tx: OfflineSignJSON): Transaction => { const { multisig_configs, transaction } = tx - // @ts-expect-error - const txObj = Transaction.fromObject(deepCamelizeKeys(transaction)) + const txObj = Transaction.fromObject(deepCamelizeKeys(transaction) as any) if (multisig_configs && Object.keys(multisig_configs).length) { const args = Object.keys(multisig_configs)[0] const lock = SystemScriptInfo.generateMultiSignScript(args) From a6201d46312cd267acff524629c683922a9d93b0 Mon Sep 17 00:00:00 2001 From: devchenyan Date: Fri, 7 Jun 2024 11:39:51 +0800 Subject: [PATCH 8/8] fix --- packages/neuron-ui/src/utils/calculateFee.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/neuron-ui/src/utils/calculateFee.ts b/packages/neuron-ui/src/utils/calculateFee.ts index 2c7103f747..358709730c 100644 --- a/packages/neuron-ui/src/utils/calculateFee.ts +++ b/packages/neuron-ui/src/utils/calculateFee.ts @@ -3,11 +3,11 @@ export const calculateFee = (tx: any) => { return '0' } const inputCapacities = tx.inputs.reduce( - (result: bigint, input: { capacity: string }) => result + BigInt(input.capacity), + (result: bigint, input: { capacity: string }) => result + BigInt(input.capacity || '0'), BigInt(0) ) const outputCapacities = tx.outputs.reduce( - (result: bigint, output: { capacity: string }) => result + BigInt(output.capacity), + (result: bigint, output: { capacity: string }) => result + BigInt(output.capacity || '0'), BigInt(0) )