From 18c0fdaf8195874cb89de162d72ae2dd0276b60a Mon Sep 17 00:00:00 2001 From: IronLu233 Date: Thu, 27 Apr 2023 14:34:13 +0800 Subject: [PATCH 01/11] fix(ownership-provider): add method `isOwnedByWallet` --- package-lock.json | 12 ++++-- packages/ownership-providers/package.json | 6 ++- .../src/FullOwnershipProvider.ts | 43 ++++++++++++++++++- 3 files changed, 54 insertions(+), 7 deletions(-) diff --git a/package-lock.json b/package-lock.json index 088cdd17..fa5cc867 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9618,8 +9618,9 @@ }, "node_modules/@types/lodash.times": { "version": "4.3.7", + "resolved": "https://registry.npmjs.org/@types/lodash.times/-/lodash.times-4.3.7.tgz", + "integrity": "sha512-Jd7PPonqfO2zpNdwpF4dAH4DPSJ7hnsX35k8pFtkgNqSQWSWLINbi1HnTFAE/oOn6XXfXONahZWVVK3klTOIPg==", "dev": true, - "license": "MIT", "dependencies": { "@types/lodash": "*" } @@ -23507,7 +23508,8 @@ }, "node_modules/lodash.times": { "version": "4.3.2", - "license": "MIT" + "resolved": "https://registry.npmjs.org/lodash.times/-/lodash.times-4.3.2.tgz", + "integrity": "sha512-FfaJzl0SA35CRPDh5SWe2BTght6y5KSK7yJv166qIp/8q7qOwBDCvuDZE2RUSMRpBkLF6rZKbLEUoTmaP3qg6A==" }, "node_modules/lodash.union": { "version": "4.6.0", @@ -37792,7 +37794,8 @@ "dependencies": { "@nexus-wallet/protocol": "0.0.17", "@nexus-wallet/utils": "0.0.17", - "lodash.range": "3.2.0" + "lodash.range": "3.2.0", + "lodash.times": "4.3.2" }, "devDependencies": { "@ckb-lumos/base": "0.20.0-alpha.2", @@ -37801,7 +37804,8 @@ "@ckb-lumos/common-scripts": "0.20.0-alpha.2", "@ckb-lumos/config-manager": "0.20.0-alpha.2", "@ckb-lumos/helpers": "0.20.0-alpha.2", - "@types/lodash.range": "3.2.7" + "@types/lodash.range": "3.2.7", + "@types/lodash.times": "4.3.7" }, "peerDependencies": { "@ckb-lumos/base": "0.20.0-alpha.2", diff --git a/packages/ownership-providers/package.json b/packages/ownership-providers/package.json index 33df4978..f32eaa1d 100644 --- a/packages/ownership-providers/package.json +++ b/packages/ownership-providers/package.json @@ -33,7 +33,8 @@ "dependencies": { "@nexus-wallet/protocol": "0.0.17", "@nexus-wallet/utils": "0.0.17", - "lodash.range": "3.2.0" + "lodash.range": "3.2.0", + "lodash.times": "4.3.2" }, "devDependencies": { "@ckb-lumos/base": "0.20.0-alpha.2", @@ -42,7 +43,8 @@ "@ckb-lumos/common-scripts": "0.20.0-alpha.2", "@ckb-lumos/config-manager": "0.20.0-alpha.2", "@ckb-lumos/helpers": "0.20.0-alpha.2", - "@types/lodash.range": "3.2.7" + "@types/lodash.range": "3.2.7", + "@types/lodash.times": "4.3.7" }, "peerDependencies": { "@ckb-lumos/base": "0.20.0-alpha.2", diff --git a/packages/ownership-providers/src/FullOwnershipProvider.ts b/packages/ownership-providers/src/FullOwnershipProvider.ts index 2efd0397..6ac0a74d 100644 --- a/packages/ownership-providers/src/FullOwnershipProvider.ts +++ b/packages/ownership-providers/src/FullOwnershipProvider.ts @@ -11,8 +11,9 @@ import { Address, blockchain, Cell, CellDep, HexString, Script, Transaction } fr import { bytes, PackParam } from '@ckb-lumos/codec'; import { secp256k1Blake160 } from '@ckb-lumos/common-scripts'; import { Config as LumosConfig, getConfig as getLumosConfig, ScriptConfig } from '@ckb-lumos/config-manager'; +import times from 'lodash.times'; import { Uint8ArrayCodec } from '@ckb-lumos/codec/lib/base'; -import { OutputValidator } from '@nexus-wallet/protocol/lib/base'; +import { OutputValidator, Paginate } from '@nexus-wallet/protocol/lib/base'; function equalPack(codec: C, a: PackParam, b: PackParam): boolean { return bytes.equal(codec.pack(a), codec.pack(b)); @@ -389,6 +390,46 @@ export class FullOwnershipProvider { }); } + private async isOwnedByWallet(locks: Script[]): Promise { + function stringifyLock(lock: Script) { + return bytes.hexify(blockchain.Script.pack(lock)); + } + if (locks.length === 0) return []; + + const offChainLockSet = new Set( + ( + await Promise.all([ + this.getOffChainLocks({ change: 'external' }), + this.getOffChainLocks({ change: 'internal' }), + ]) + ) + .flat() + .map(stringifyLock), + ); + const result = times(locks.length, () => false); + + locks.forEach((lock, index) => { + result[index] = offChainLockSet.has(bytes.hexify(blockchain.Script.pack(lock))); + }); + + for (const change of ['external', 'external'] as const) { + let cursor: string | undefined = undefined; + + let page: Paginate