Skip to content

Commit

Permalink
feat: compatible ckb-cli tx multisig file
Browse files Browse the repository at this point in the history
  • Loading branch information
devchenyan committed May 26, 2024
1 parent bed901b commit 3677308
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
10 changes: 8 additions & 2 deletions packages/neuron-wallet/src/controllers/multisig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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'),
Expand All @@ -226,7 +232,7 @@ export default class MultisigController {
}
return {
status: ResponseCode.Success,
result: result?.json,
result: tx,
}
}
}
1 change: 1 addition & 0 deletions packages/neuron-wallet/src/models/offline-sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export interface OfflineSignJSON {
description?: string
asset_account?: AssetAccount
multisig_configs?: MultisigConfigs
signatures?: Signatures
}

export default class OfflineSign implements OfflineSignProps {
Expand Down
25 changes: 25 additions & 0 deletions packages/neuron-wallet/src/utils/hump.ts
Original file line number Diff line number Diff line change
@@ -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 }
39 changes: 39 additions & 0 deletions packages/neuron-wallet/src/utils/multisig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
}

1 comment on commit 3677308

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Packaging for test is done in 9245269938

Please sign in to comment.