1- import { scriptToHash } from '@nervosnetwork/ckb-sdk-utils'
1+ import {
2+ scriptToHash ,
3+ AddressPrefix ,
4+ bech32Address ,
5+ AddressType ,
6+ parseAddress
7+ } from '@nervosnetwork/ckb-sdk-utils'
28import NodeService from 'services/node'
39import { OutPoint , Script , ScriptHashType } from 'types/cell-types'
410import env from 'env'
511import ConvertTo from 'types/convert-to'
612import { SystemScriptSubject } from 'models/subjects/system-script'
7-
8- const { core } = NodeService . getInstance ( )
13+ import Core from '@nervosnetwork/ckb-sdk-core'
914
1015export interface SystemScript {
1116 codeHash : string
@@ -28,11 +33,15 @@ export default class LockUtils {
2833 @subscribed
2934 static systemScriptInfo : SystemScript | undefined
3035
31- static async systemScript ( ) : Promise < SystemScript > {
32- if ( this . systemScriptInfo ) {
36+ static previousURL : string | undefined
37+
38+ static async systemScript ( nodeURL : string = NodeService . getInstance ( ) . core . rpc . node . url ) : Promise < SystemScript > {
39+ if ( this . systemScriptInfo && nodeURL === LockUtils . previousURL ) {
3340 return this . systemScriptInfo
3441 }
3542
43+ const core = new Core ( nodeURL )
44+
3645 const systemCell = await core . loadSecp256k1Dep ( )
3746 let { codeHash } = systemCell
3847 const { outPoint, hashType } = systemCell
@@ -57,6 +66,7 @@ export default class LockUtils {
5766 }
5867
5968 this . systemScriptInfo = systemScriptInfo
69+ LockUtils . previousURL = nodeURL
6070
6171 return systemScriptInfo
6272 }
@@ -80,8 +90,12 @@ export default class LockUtils {
8090 return LockUtils . computeScriptHash ( lock )
8191 }
8292
83- static async addressToLockScript ( address : string , hashType : ScriptHashType = ScriptHashType . Type ) : Promise < Script > {
84- const systemScript = await this . systemScript ( )
93+ static async addressToLockScript (
94+ address : string ,
95+ hashType : ScriptHashType = ScriptHashType . Type ,
96+ nodeURL : string = NodeService . getInstance ( ) . core . rpc . node . url
97+ ) : Promise < Script > {
98+ const systemScript = await this . systemScript ( nodeURL )
8599
86100 const lock : Script = {
87101 codeHash : systemScript . codeHash ,
@@ -91,22 +105,32 @@ export default class LockUtils {
91105 return lock
92106 }
93107
94- static async addressToLockHash ( address : string , hashType : ScriptHashType = ScriptHashType . Type ) : Promise < string > {
95- const lock : Script = await this . addressToLockScript ( address , hashType )
108+ static async addressToLockHash (
109+ address : string ,
110+ hashType : ScriptHashType = ScriptHashType . Type ,
111+ nodeURL : string = NodeService . getInstance ( ) . core . rpc . node . url
112+ ) : Promise < string > {
113+ const lock : Script = await this . addressToLockScript ( address , hashType , nodeURL )
96114 const lockHash : string = this . lockScriptToHash ( lock )
97115
98116 return lockHash
99117 }
100118
101- static async addressToAllLockHashes ( address : string ) : Promise < string [ ] > {
102- const dataLockHash = await LockUtils . addressToLockHash ( address )
119+ static async addressToAllLockHashes (
120+ address : string ,
121+ nodeURL : string = NodeService . getInstance ( ) . core . rpc . node . url
122+ ) : Promise < string [ ] > {
123+ const dataLockHash = await LockUtils . addressToLockHash ( address , ScriptHashType . Type , nodeURL )
103124 return [ dataLockHash ]
104125 }
105126
106- static async addressesToAllLockHashes ( addresses : string [ ] ) : Promise < string [ ] > {
127+ static async addressesToAllLockHashes (
128+ addresses : string [ ] ,
129+ nodeURL : string = NodeService . getInstance ( ) . core . rpc . node . url
130+ ) : Promise < string [ ] > {
107131 const lockHashes : string [ ] = ( await Promise . all (
108132 addresses . map ( async addr => {
109- return LockUtils . addressToAllLockHashes ( addr )
133+ return LockUtils . addressToAllLockHashes ( addr , nodeURL )
110134 } )
111135 ) ) . reduce ( ( acc , val ) => acc . concat ( val ) , [ ] )
112136 return lockHashes
@@ -118,16 +142,16 @@ export default class LockUtils {
118142 }
119143
120144 static blake160ToAddress ( blake160 : string ) : string {
121- const prefix = env . testnet ? core . utils . AddressPrefix . Testnet : core . utils . AddressPrefix . Mainnet
122- return core . utils . bech32Address ( blake160 , {
145+ const prefix = env . testnet ? AddressPrefix . Testnet : AddressPrefix . Mainnet
146+ return bech32Address ( blake160 , {
123147 prefix,
124- type : core . utils . AddressType . HashIdx ,
148+ type : AddressType . HashIdx ,
125149 codeHashIndex : '0x00' ,
126150 } )
127151 }
128152
129153 static addressToBlake160 ( address : string ) : string {
130- const result : string = core . utils . parseAddress ( address , 'hex' ) as string
154+ const result : string = parseAddress ( address , 'hex' ) as string
131155 const hrp : string = `0100`
132156 let blake160 : string = result . slice ( hrp . length + 2 , result . length )
133157 if ( ! blake160 . startsWith ( '0x' ) ) {
0 commit comments