From 607558b2a5e1ab5d8a709ad8d00110988e303a87 Mon Sep 17 00:00:00 2001 From: classicalliu Date: Sat, 7 Sep 2019 16:59:55 +0800 Subject: [PATCH] feat: bump sdk to v0.20.0 --- packages/neuron-ui/package.json | 2 +- packages/neuron-wallet/package.json | 6 +- .../neuron-wallet/src/models/lock-utils.ts | 9 +- .../services/sync/check-and-save/output.ts | 7 +- .../src/services/tx/transaction-persistor.ts | 12 ++- .../neuron-wallet/src/services/wallets.ts | 5 +- .../neuron-wallet/src/types/convert-to.ts | 2 +- .../tests/models/lock-utils.test.ts | 82 +++++++++---------- yarn.lock | 66 +++++++-------- 9 files changed, 95 insertions(+), 96 deletions(-) diff --git a/packages/neuron-ui/package.json b/packages/neuron-ui/package.json index 606762d8a8..a30dec3864 100644 --- a/packages/neuron-ui/package.json +++ b/packages/neuron-ui/package.json @@ -43,7 +43,7 @@ "last 2 chrome versions" ], "dependencies": { - "@nervosnetwork/ckb-sdk-core": "0.19.1", + "@nervosnetwork/ckb-sdk-core": "0.20.0", "@uifabric/experiments": "7.10.0", "@uifabric/styling": "7.4.0", "canvg": "2.0.0", diff --git a/packages/neuron-wallet/package.json b/packages/neuron-wallet/package.json index bec308a2c1..8215a513e3 100644 --- a/packages/neuron-wallet/package.json +++ b/packages/neuron-wallet/package.json @@ -34,8 +34,8 @@ ] }, "dependencies": { - "@nervosnetwork/ckb-sdk-core": "0.19.1", - "@nervosnetwork/ckb-sdk-utils": "0.19.1", + "@nervosnetwork/ckb-sdk-core": "0.20.0", + "@nervosnetwork/ckb-sdk-utils": "0.20.0", "bn.js": "4.11.8", "chalk": "2.4.2", "electron-log": "3.0.7", @@ -51,7 +51,7 @@ "uuid": "3.3.2" }, "devDependencies": { - "@nervosnetwork/ckb-types": "0.19.1", + "@nervosnetwork/ckb-types": "0.20.0", "@types/electron-devtools-installer": "2.2.0", "@types/elliptic": "6.4.8", "@types/sqlite3": "3.1.5", diff --git a/packages/neuron-wallet/src/models/lock-utils.ts b/packages/neuron-wallet/src/models/lock-utils.ts index 780eaf0dca..211e2a3ce1 100644 --- a/packages/neuron-wallet/src/models/lock-utils.ts +++ b/packages/neuron-wallet/src/models/lock-utils.ts @@ -1,3 +1,4 @@ +import { scriptToHash } from '@nervosnetwork/ckb-sdk-utils' import NodeService from 'services/node' import { OutPoint, Script, ScriptHashType } from 'types/cell-types' import env from 'env' @@ -65,9 +66,9 @@ export default class LockUtils { SystemScriptSubject.next({ codeHash: info.codeHash }) } - static computeScriptHash = async (script: Script): Promise => { + static computeScriptHash = (script: Script): string => { const ckbScript: CKBComponents.Script = ConvertTo.toSdkScript(script) - const hash: string = await (core.rpc as any).computeScriptHash(ckbScript) + const hash: string = scriptToHash(ckbScript) if (!hash.startsWith('0x')) { return `0x${hash}` } @@ -75,7 +76,7 @@ export default class LockUtils { } // use SDK lockScriptToHash - static lockScriptToHash = async (lock: Script) => { + static lockScriptToHash = (lock: Script) => { return LockUtils.computeScriptHash(lock) } @@ -92,7 +93,7 @@ export default class LockUtils { static async addressToLockHash(address: string, hashType: ScriptHashType = ScriptHashType.Type): Promise { const lock: Script = await this.addressToLockScript(address, hashType) - const lockHash: string = await this.lockScriptToHash(lock) + const lockHash: string = this.lockScriptToHash(lock) return lockHash } diff --git a/packages/neuron-wallet/src/services/sync/check-and-save/output.ts b/packages/neuron-wallet/src/services/sync/check-and-save/output.ts index 79524238b1..04c49684ce 100644 --- a/packages/neuron-wallet/src/services/sync/check-and-save/output.ts +++ b/packages/neuron-wallet/src/services/sync/check-and-save/output.ts @@ -6,20 +6,19 @@ export default class CheckOutput { constructor(output: Cell) { this.output = output - // this.calcLockHash() + this.calcLockHash() } - public calcLockHash = async (): Promise => { + public calcLockHash = (): Cell => { if (this.output.lockHash) { return this.output } - this.output.lockHash = await LockUtils.lockScriptToHash(this.output.lock) + this.output.lockHash = LockUtils.lockScriptToHash(this.output.lock) return this.output } public checkLockHash = async (lockHashList: string[]): Promise => { - await this.calcLockHash() return lockHashList.includes(this.output.lockHash!) } } diff --git a/packages/neuron-wallet/src/services/tx/transaction-persistor.ts b/packages/neuron-wallet/src/services/tx/transaction-persistor.ts index 100adb4bf5..9a7b1d2ce5 100644 --- a/packages/neuron-wallet/src/services/tx/transaction-persistor.ts +++ b/packages/neuron-wallet/src/services/tx/transaction-persistor.ts @@ -185,13 +185,11 @@ export class TransactionPersistor { saveType: TxSaveType ): Promise => { const tx: Transaction = transaction - tx.outputs = await Promise.all( - tx.outputs!.map(async o => { - const output = o - output.lockHash = await LockUtils.lockScriptToHash(output.lock!) - return output - }) - ) + tx.outputs = tx.outputs!.map(o => { + const output = o + output.lockHash = LockUtils.lockScriptToHash(output.lock!) + return output + }) tx.inputs = await Promise.all( tx.inputs!.map(async i => { diff --git a/packages/neuron-wallet/src/services/wallets.ts b/packages/neuron-wallet/src/services/wallets.ts index 3c3f1ea5a1..1b13d62a39 100644 --- a/packages/neuron-wallet/src/services/wallets.ts +++ b/packages/neuron-wallet/src/services/wallets.ts @@ -345,7 +345,10 @@ export default class WalletService { fee ) - const txHash: string = await (core.rpc as any).computeTransactionHash(ConvertTo.toSdkTxWithoutHash(tx)) + let txHash: string = await core.utils.rawTransactionToHash(ConvertTo.toSdkTxWithoutHash(tx)) + if (!txHash.startsWith('0x')) { + txHash = `0x${txHash}` + } const { inputs } = tx diff --git a/packages/neuron-wallet/src/types/convert-to.ts b/packages/neuron-wallet/src/types/convert-to.ts index 81d81704c0..0821adc17d 100644 --- a/packages/neuron-wallet/src/types/convert-to.ts +++ b/packages/neuron-wallet/src/types/convert-to.ts @@ -21,7 +21,7 @@ export default class ConvertTo { } } - public static toSdkTxWithoutHash = (tx: TransactionWithoutHash): any => { + public static toSdkTxWithoutHash = (tx: TransactionWithoutHash): CKBComponents.RawTransaction => { const transaction = { ...tx, inputs: tx.inputs!.map(input => ConvertTo.toSdkInput(input)), diff --git a/packages/neuron-wallet/tests/models/lock-utils.test.ts b/packages/neuron-wallet/tests/models/lock-utils.test.ts index 2407618ae1..bd2fa60ea7 100644 --- a/packages/neuron-wallet/tests/models/lock-utils.test.ts +++ b/packages/neuron-wallet/tests/models/lock-utils.test.ts @@ -3,42 +3,41 @@ import LockUtils from '../../src/models/lock-utils' const systemScript = { outPoint: { - txHash: '0xc640423e9c8f53855a471c66e3d915fee4f653ac7f7e82033139d25df2ad9aad', + txHash: '0x74e34a76d68f5ed864c0ad139a82461ee809e981939cd9cfcd92ac0fdbb1114b', index: '0', }, - codeHash: '0x68d5438ac952d2f584abf879527946a537e82c7f3c1cbf6d8ebf9767437d8e88', + codeHash: '0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2', hashType: ScriptHashType.Type, } describe('LockUtils Test', () => { const bob = { lockScript: { - codeHash: '0x68d5438ac952d2f584abf879527946a537e82c7f3c1cbf6d8ebf9767437d8e88', + codeHash: '0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2', args: ['0x36c329ed630d6ce750712a477543672adab57f4c'], hashType: ScriptHashType.Type, }, - lockHash: '0x024b0fd0c4912e98aab6808f6474cacb1969255d526b3cac5d3bdd15962a8818', + lockHash: '0xecaeea8c8581d08a3b52980272001dbf203bc6fa2afcabe7cc90cc2afff488ba', address: 'ckt1qyqrdsefa43s6m882pcj53m4gdnj4k440axqswmu83', blake160: '0x36c329ed630d6ce750712a477543672adab57f4c', } - // const alice = { - // lockScript: { - // codeHash: '0x68d5438ac952d2f584abf879527946a537e82c7f3c1cbf6d8ebf9767437d8e88', - // args: ['0xe2193df51d78411601796b35b17b4f8f2cd85bd0'], - // hashType: ScriptHashType.Type, - // }, - // lockHash: '0xf7173d209ce5773a6395735288a53b7182da4a8b0aa4718123208acf37a95196', - // address: 'ckt1qyqwyxfa75whssgkq9ukkdd30d8c7txct0gqfvmy2v', - // blake160: '0xe2193df51d78411601796b35b17b4f8f2cd85bd0', - // } + const alice = { + lockScript: { + codeHash: '0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2', + args: ['0xe2193df51d78411601796b35b17b4f8f2cd85bd0'], + hashType: ScriptHashType.Type, + }, + lockHash: '0x489306d801d54bee2d8562ae20fdc53635b568f8107bddff15bb357f520cc02c', + address: 'ckt1qyqwyxfa75whssgkq9ukkdd30d8c7txct0gqfvmy2v', + blake160: '0xe2193df51d78411601796b35b17b4f8f2cd85bd0', + } - // TODO: it need network and node - // it('lockScriptToHash', async () => { - // const lockHash: string = await LockUtils.lockScriptToHash(bob.lockScript) + it('lockScriptToHash', async () => { + const lockHash: string = await LockUtils.lockScriptToHash(bob.lockScript) - // expect(lockHash).toEqual(bob.lockHash) - // }) + expect(lockHash).toEqual(bob.lockHash) + }) // FIXME: test failed, should fix addressToLockScript it('addressToLockScript', async () => { @@ -51,38 +50,37 @@ describe('LockUtils Test', () => { expect(lockScript).toEqual(bob.lockScript) }) - // TODO: it need network and node - // it('addressToLockHash', async () => { - // const mockContractInfo = jest.fn() - // mockContractInfo.mockReturnValue(systemScript) - // LockUtils.systemScript = mockContractInfo.bind(LockUtils) + it('addressToLockHash', async () => { + const mockContractInfo = jest.fn() + mockContractInfo.mockReturnValue(systemScript) + LockUtils.systemScript = mockContractInfo.bind(LockUtils) - // const lockHash: string = await LockUtils.addressToLockHash(bob.address) + const lockHash: string = await LockUtils.addressToLockHash(bob.address) - // expect(lockHash).toEqual(bob.lockHash) - // }) + expect(lockHash).toEqual(bob.lockHash) + }) - // it('addressToAllLockHashes', async () => { - // const mockContractInfo = jest.fn() - // mockContractInfo.mockReturnValue(systemScript) - // LockUtils.systemScript = mockContractInfo.bind(LockUtils) + it('addressToAllLockHashes', async () => { + const mockContractInfo = jest.fn() + mockContractInfo.mockReturnValue(systemScript) + LockUtils.systemScript = mockContractInfo.bind(LockUtils) - // const lockHashes: string[] = await LockUtils.addressToAllLockHashes(bob.address) + const lockHashes: string[] = await LockUtils.addressToAllLockHashes(bob.address) - // expect(lockHashes).toEqual([bob.lockHash]) - // }) + expect(lockHashes).toEqual([bob.lockHash]) + }) - // it('addressesToAllLockHashes', async () => { - // const mockContractInfo = jest.fn() - // mockContractInfo.mockReturnValue(systemScript) - // LockUtils.systemScript = mockContractInfo.bind(LockUtils) + it('addressesToAllLockHashes', async () => { + const mockContractInfo = jest.fn() + mockContractInfo.mockReturnValue(systemScript) + LockUtils.systemScript = mockContractInfo.bind(LockUtils) - // const lockHashes: string[] = await LockUtils.addressesToAllLockHashes([bob.address, alice.address]) + const lockHashes: string[] = await LockUtils.addressesToAllLockHashes([bob.address, alice.address]) - // const expectedResult = [bob.lockHash, alice.lockHash] + const expectedResult = [bob.lockHash, alice.lockHash] - // expect(lockHashes).toEqual(expectedResult) - // }) + expect(lockHashes).toEqual(expectedResult) + }) it('lockScriptToAddress', async () => { const address: string = LockUtils.lockScriptToAddress(bob.lockScript) diff --git a/yarn.lock b/yarn.lock index 6b9daef5bf..5e2f98b3f7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2604,45 +2604,45 @@ call-me-maybe "^1.0.1" glob-to-regexp "^0.3.0" -"@nervosnetwork/ckb-sdk-address@0.19.1": - version "0.19.1" - resolved "https://registry.yarnpkg.com/@nervosnetwork/ckb-sdk-address/-/ckb-sdk-address-0.19.1.tgz#ec1927310a88a87759010b9e0089e2fce87371f3" - integrity sha512-ZEVX49G1PoXH2+qd4KMnrbYTU4U7fF3uJww/09OIS5xP285Gqb19JBjNCY+tnRj/hSC6HLp9/UqZex1vUZWOWg== - dependencies: - "@nervosnetwork/ckb-sdk-utils" "0.19.1" - "@nervosnetwork/ckb-types" "0.19.1" - -"@nervosnetwork/ckb-sdk-core@0.19.1": - version "0.19.1" - resolved "https://registry.yarnpkg.com/@nervosnetwork/ckb-sdk-core/-/ckb-sdk-core-0.19.1.tgz#23042993d083cfc21629bebf5c8270c1878df7dd" - integrity sha512-nWj2BZ4letebndIC+Ohd+eqanYJFF/FLEy1h3nhpN+jPPwKwhHNnt3QARTOOqAOBxUFIz7t6WNXFI7icvmxdTA== - dependencies: - "@nervosnetwork/ckb-sdk-address" "0.19.1" - "@nervosnetwork/ckb-sdk-rpc" "0.19.1" - "@nervosnetwork/ckb-sdk-utils" "0.19.1" - "@nervosnetwork/ckb-types" "0.19.1" - -"@nervosnetwork/ckb-sdk-rpc@0.19.1": - version "0.19.1" - resolved "https://registry.yarnpkg.com/@nervosnetwork/ckb-sdk-rpc/-/ckb-sdk-rpc-0.19.1.tgz#1f6505e08d989da51a6025c7c6600aaffbce86a9" - integrity sha512-HdeoXUd4s44de9rRYKk/33pOzdfBWP83zUGbQWgFUeTPgNTKd/poCJB9PKChb1HgZENkz2aQgRyY5hObPE1pJA== - dependencies: - "@nervosnetwork/ckb-sdk-utils" "0.19.1" +"@nervosnetwork/ckb-sdk-address@0.20.0": + version "0.20.0" + resolved "https://registry.yarnpkg.com/@nervosnetwork/ckb-sdk-address/-/ckb-sdk-address-0.20.0.tgz#3de8824c860e22e185664f2f4c39001648431f56" + integrity sha512-3FEP1I/+fRVuvafD6h1adRlv/6lfbRVwpgA/XrW4jYQBqghlaBcHVZbYqVAn5HFKAcJWtk9r9Xb8avCzfjaobg== + dependencies: + "@nervosnetwork/ckb-sdk-utils" "0.20.0" + "@nervosnetwork/ckb-types" "0.20.0" + +"@nervosnetwork/ckb-sdk-core@0.20.0": + version "0.20.0" + resolved "https://registry.yarnpkg.com/@nervosnetwork/ckb-sdk-core/-/ckb-sdk-core-0.20.0.tgz#c05faef381ba10f6efa246943237fe3b8aef5e16" + integrity sha512-My0xdhRZcXVzrQRk8pi00guyaf9kGaVdKhKE+oTLthyPHxAqGtbyl7sc4Fx0RNuQhVtwe1jmh6LEtPgZbqbiLw== + dependencies: + "@nervosnetwork/ckb-sdk-address" "0.20.0" + "@nervosnetwork/ckb-sdk-rpc" "0.20.0" + "@nervosnetwork/ckb-sdk-utils" "0.20.0" + "@nervosnetwork/ckb-types" "0.20.0" + +"@nervosnetwork/ckb-sdk-rpc@0.20.0": + version "0.20.0" + resolved "https://registry.yarnpkg.com/@nervosnetwork/ckb-sdk-rpc/-/ckb-sdk-rpc-0.20.0.tgz#3fa2bb17f4d81e56569859c40f147eb6b53a11b4" + integrity sha512-0F6j/hop1JOTLGalX+0LsjXRKc4+4gMBV7cdSOZh9O56Qd4m1yZS+2t6+nrN9Xi9+pAC+8wkhKvGJl2B3mXBnQ== + dependencies: + "@nervosnetwork/ckb-sdk-utils" "0.20.0" axios "0.19.0" -"@nervosnetwork/ckb-sdk-utils@0.19.1": - version "0.19.1" - resolved "https://registry.yarnpkg.com/@nervosnetwork/ckb-sdk-utils/-/ckb-sdk-utils-0.19.1.tgz#86edd68b568596b50fd5a6801d9e52280c31376b" - integrity sha512-xRx8P9L3YBWdE4Itw/VTLENhx4xsagg5C79Jgy+E8NexuMDqEjiZD78Qz0/5LyZ3CaolsN4TwfNjRD3/5fb2kg== +"@nervosnetwork/ckb-sdk-utils@0.20.0": + version "0.20.0" + resolved "https://registry.yarnpkg.com/@nervosnetwork/ckb-sdk-utils/-/ckb-sdk-utils-0.20.0.tgz#0acbed3caef1f9c5180336efed13c6083246c7f7" + integrity sha512-CGXQFCSyZFTtF9yDeNnk6NR9Seq5Sk1haZgLwk0jRHzmZdD/v3PQNnonOBywaVEWNKcsVXrCBJEUAvOBKeaP3g== dependencies: - "@nervosnetwork/ckb-types" "0.19.1" + "@nervosnetwork/ckb-types" "0.20.0" blake2b-wasm "1.1.7" elliptic "6.4.1" -"@nervosnetwork/ckb-types@0.19.1": - version "0.19.1" - resolved "https://registry.yarnpkg.com/@nervosnetwork/ckb-types/-/ckb-types-0.19.1.tgz#4e819152b0985a6b8fdd221e6e283c898235dc20" - integrity sha512-Bdlbd2jdSGCZ27Lb0tKTWS8YT01+bLKvpk6Ypnth+nutAywGnh13EOyhXluK6WKXq6uNytBnnBac4O8mccDeVQ== +"@nervosnetwork/ckb-types@0.20.0": + version "0.20.0" + resolved "https://registry.yarnpkg.com/@nervosnetwork/ckb-types/-/ckb-types-0.20.0.tgz#d172904262011fdbc0b4970c0a34fbe2842da73e" + integrity sha512-ZqiLayu2wg1vJVt3gXXsRxwYQ/B9nur7WniCBsnQswDVs9hM5lZ628Ov50PIISOLSR//B7dxpnsr4rd/oz4yMA== "@nodelib/fs.stat@^1.1.2": version "1.1.3"