diff --git a/README.md b/README.md index b19b51a..eefe468 100644 --- a/README.md +++ b/README.md @@ -6,17 +6,41 @@ JavaScript SDK for [CoTA](https://talk.nervos.org/t/rfc-cota-a-compact-token-aggregator-standard-for-extremely-low-cost-nfts-and-fts/6338). +[CoTA Docs](https://developer.mibao.net/docs/develop/cota/overview) + ## Feature - Provide methods for [cota-aggregator](https://github.com/nervina-labs/cota-aggregator) and [cota-registry-aggregator](https://github.com/nervina-labs/cota-registry-aggregator) RPC APIs - Provide methods to generate CoTA operating transactions +## Prerequisites + +- [CKB Node](https://docs.nervos.org/docs/basics/guides/testnet) +- [CKB Indexer](https://github.com/nervosnetwork/ckb-indexer) +- [CoTA Registry Aggregator](https://github.com/nervina-labs/cota-registry-aggregator) +- [CoTA Aggregator](https://github.com/nervina-labs/cota-aggregator) + +### Public ckb node url and ckb indexer url as blow can be used to develop and test + +``` +mainnet +https://mainnet.ckbapp.dev ---> ckb mainnet rpc +https://mainnet.ckbapp.dev/rpc ---> ckb mainnet rpc +https://mainnet.ckbapp.dev/indexer ---> ckb mainnet indexer_rpc + +testnet: +https://testnet.ckbapp.dev ---> ckb testnet rpc +https://testnet.ckbapp.dev/rpc ---> ckb testnet rpc +https://testnet.ckbapp.dev/indexer ---> ckb testnet indexer_rpc +``` + ## Examples - [aggregator example](https://github.com/nervina-labs/cota-sdk-js/blob/develop/example/aggregator.ts): Fetch CoTA NFT data and [SMT](https://github.com/nervosnetwork/sparse-merkle-tree) data from Aggregator server -- [registry example](https://github.com/nervina-labs/cota-sdk-js/blob/develop/example/define.ts): Generate defining CoTA cells transaction +- [registry example](https://github.com/nervina-labs/cota-sdk-js/blob/develop/example/registry.ts): Generate registering CoTA cells transaction +- [define example](https://github.com/nervina-labs/cota-sdk-js/blob/develop/example/define.ts): Generate defining CoTA cells transaction - [mint example](https://github.com/nervina-labs/cota-sdk-js/blob/develop/example/mint.ts): Generate minting CoTA NFT transaction - [claim example](https://github.com/nervina-labs/cota-sdk-js/blob/develop/example/claim.ts): Generate claiming CoTA NFT transaction - [withdraw example](https://github.com/nervina-labs/cota-sdk-js/blob/develop/example/withdraw.ts): Generate withdrawing CoTA NFT transaction - [transfer example](https://github.com/nervina-labs/cota-sdk-js/blob/develop/example/transfer.ts): Generate transferring CoTA NFT transaction -- [update example](https://github.com/nervina-labs/cota-sdk-js/blob/develop/example/update.ts): Generate updating CoTA NFT information transaction \ No newline at end of file +- [update example](https://github.com/nervina-labs/cota-sdk-js/blob/develop/example/update.ts): Generate updating CoTA NFT information transaction diff --git a/example/aggregator.js b/example/aggregator/index.js similarity index 93% rename from example/aggregator.js rename to example/aggregator/index.js index ceac26a..a400a31 100644 --- a/example/aggregator.js +++ b/example/aggregator/index.js @@ -1,4 +1,4 @@ -const { Aggregator } = require('../lib/aggregator') +const { Aggregator } = require('@nervina-labs/cota-sdk') const run = async () => { const aggregator = new Aggregator({ registryUrl: 'http://localhost:3050', cotaUrl: 'http://localhost:3030' }) diff --git a/example/aggregator/package.json b/example/aggregator/package.json new file mode 100644 index 0000000..ca6c09e --- /dev/null +++ b/example/aggregator/package.json @@ -0,0 +1,10 @@ +{ + "name": "aggregator-example", + "version": "1.0.0", + "description": "The example of aggregator rpc", + "main": "index.js", + "license": "MIT", + "dependencies": { + "@nervina-labs/cota-sdk": "0.1.0" + } +} diff --git a/example/aggregator/yarn.lock b/example/aggregator/yarn.lock new file mode 100644 index 0000000..c9a1b55 --- /dev/null +++ b/example/aggregator/yarn.lock @@ -0,0 +1,168 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +"@nervina-labs/cota-sdk@0.1.0": + version "0.1.0" + resolved "https://registry.yarnpkg.com/@nervina-labs/cota-sdk/-/cota-sdk-0.1.0.tgz#012768d8f048108e93d04269a385e82697716a7b" + integrity sha512-fi7OlfSMbW8IKV1XtuPaQ3JAw8U9c7rbmL33KG+UTOZzM3ws5OEsfCRukSH0cffAtH+mueEtSC+MRgW5htnfvQ== + dependencies: + "@nervosnetwork/ckb-sdk-core" "^0.102.0" + "@nervosnetwork/ckb-sdk-utils" "^0.102.0" + "@nervosnetwork/ckb-types" "^0.102.0" + axios "^0.26.0" + camelcase-keys "^7.0.2" + +"@nervosnetwork/ckb-sdk-core@^0.102.0": + version "0.102.1" + resolved "https://registry.yarnpkg.com/@nervosnetwork/ckb-sdk-core/-/ckb-sdk-core-0.102.1.tgz#680c1ebe6425549132d96da288d7600b24dcd98a" + integrity sha512-q6T5ooNYzA9ZhlV4ad39Zn6ubVOa7gCFZ31O+LJAhSxRZDpKctTiwVbgvNFoa2vDvTZej6hT98fb2pACW4rYnQ== + dependencies: + "@nervosnetwork/ckb-sdk-rpc" "0.102.1" + "@nervosnetwork/ckb-sdk-utils" "0.102.1" + "@nervosnetwork/ckb-types" "0.102.1" + tslib "2.3.1" + +"@nervosnetwork/ckb-sdk-rpc@0.102.1": + version "0.102.1" + resolved "https://registry.yarnpkg.com/@nervosnetwork/ckb-sdk-rpc/-/ckb-sdk-rpc-0.102.1.tgz#6b9d98071428e81d1c28880991a4455458850478" + integrity sha512-xCIhiAunHt9peEY+doxNk28Nc74I8RAN0JHyyhxOYWTZqWg7EwvPeI9CNSi0Do4M/zgJ3Cln6yWjJQrSChMQuA== + dependencies: + "@nervosnetwork/ckb-sdk-utils" "0.102.1" + axios "0.21.4" + tslib "2.3.1" + +"@nervosnetwork/ckb-sdk-utils@0.102.1", "@nervosnetwork/ckb-sdk-utils@^0.102.0": + version "0.102.1" + resolved "https://registry.yarnpkg.com/@nervosnetwork/ckb-sdk-utils/-/ckb-sdk-utils-0.102.1.tgz#5e9bd9e2ebfbbd7b55f915e087e5cbd557bab34f" + integrity sha512-M+6P+8qhXiJoV5e77KtCLQQoKvuOAYdaCd6FEF5V6OhURbkRdhy6gxulY2YB7chlaw7QbvPhlVHlWB1DFOjkHw== + dependencies: + "@nervosnetwork/ckb-types" "0.102.1" + bech32 "2.0.0" + elliptic "6.5.4" + jsbi "3.1.3" + tslib "2.3.1" + +"@nervosnetwork/ckb-types@0.102.1", "@nervosnetwork/ckb-types@^0.102.0": + version "0.102.1" + resolved "https://registry.yarnpkg.com/@nervosnetwork/ckb-types/-/ckb-types-0.102.1.tgz#95278bfdbc489ad6225ed8125a235ee7ba378882" + integrity sha512-WFE/wPwZm+UYpgKmP2QX2zQyzCkAocfjQyP8DWJds3KhsI+7Z9EjFZ3lzDEWclU/KokC1fOd9+Bu5DMrfkcZng== + +axios@0.21.4: + version "0.21.4" + resolved "https://registry.yarnpkg.com/axios/-/axios-0.21.4.tgz#c67b90dc0568e5c1cf2b0b858c43ba28e2eda575" + integrity sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg== + dependencies: + follow-redirects "^1.14.0" + +axios@^0.26.0: + version "0.26.0" + resolved "https://registry.yarnpkg.com/axios/-/axios-0.26.0.tgz#9a318f1c69ec108f8cd5f3c3d390366635e13928" + integrity sha512-lKoGLMYtHvFrPVt3r+RBMp9nh34N0M8zEfCWqdWZx6phynIEhQqAdydpyBAAG211zlhX9Rgu08cOamy6XjE5Og== + dependencies: + follow-redirects "^1.14.8" + +bech32@2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/bech32/-/bech32-2.0.0.tgz#078d3686535075c8c79709f054b1b226a133b355" + integrity sha512-LcknSilhIGatDAsY1ak2I8VtGaHNhgMSYVxFrGLXv+xLHytaKZKcaUJJUE7qmBr7h33o5YQwP55pMI0xmkpJwg== + +bn.js@^4.11.9: + version "4.12.0" + resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-4.12.0.tgz#775b3f278efbb9718eec7361f483fb36fbbfea88" + integrity sha512-c98Bf3tPniI+scsdk237ku1Dc3ujXQTSgyiPUDEOe7tRkhrqridvh8klBv0HCEso1OLOYcHuCv/cS6DNxKH+ZA== + +brorand@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/brorand/-/brorand-1.1.0.tgz#12c25efe40a45e3c323eb8675a0a0ce57b22371f" + integrity sha1-EsJe/kCkXjwyPrhnWgoM5XsiNx8= + +camelcase-keys@^7.0.2: + version "7.0.2" + resolved "https://registry.yarnpkg.com/camelcase-keys/-/camelcase-keys-7.0.2.tgz#d048d8c69448745bb0de6fc4c1c52a30dfbe7252" + integrity sha512-Rjs1H+A9R+Ig+4E/9oyB66UC5Mj9Xq3N//vcLf2WzgdTi/3gUu3Z9KoqmlrEG4VuuLK8wJHofxzdQXz/knhiYg== + dependencies: + camelcase "^6.3.0" + map-obj "^4.1.0" + quick-lru "^5.1.1" + type-fest "^1.2.1" + +camelcase@^6.3.0: + version "6.3.0" + resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-6.3.0.tgz#5685b95eb209ac9c0c177467778c9c84df58ba9a" + integrity sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA== + +elliptic@6.5.4: + version "6.5.4" + resolved "https://registry.yarnpkg.com/elliptic/-/elliptic-6.5.4.tgz#da37cebd31e79a1367e941b592ed1fbebd58abbb" + integrity sha512-iLhC6ULemrljPZb+QutR5TQGB+pdW6KGD5RSegS+8sorOZT+rdQFbsQFJgvN3eRqNALqJer4oQ16YvJHlU8hzQ== + dependencies: + bn.js "^4.11.9" + brorand "^1.1.0" + hash.js "^1.0.0" + hmac-drbg "^1.0.1" + inherits "^2.0.4" + minimalistic-assert "^1.0.1" + minimalistic-crypto-utils "^1.0.1" + +follow-redirects@^1.14.0, follow-redirects@^1.14.8: + version "1.14.9" + resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.9.tgz#dd4ea157de7bfaf9ea9b3fbd85aa16951f78d8d7" + integrity sha512-MQDfihBQYMcyy5dhRDJUHcw7lb2Pv/TuE6xP1vyraLukNDHKbDxDNaOE3NbCAdKQApno+GPRyo1YAp89yCjK4w== + +hash.js@^1.0.0, hash.js@^1.0.3: + version "1.1.7" + resolved "https://registry.yarnpkg.com/hash.js/-/hash.js-1.1.7.tgz#0babca538e8d4ee4a0f8988d68866537a003cf42" + integrity sha512-taOaskGt4z4SOANNseOviYDvjEJinIkRgmp7LbKP2YTTmVxWBl87s/uzK9r+44BclBSp2X7K1hqeNfz9JbBeXA== + dependencies: + inherits "^2.0.3" + minimalistic-assert "^1.0.1" + +hmac-drbg@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/hmac-drbg/-/hmac-drbg-1.0.1.tgz#d2745701025a6c775a6c545793ed502fc0c649a1" + integrity sha1-0nRXAQJabHdabFRXk+1QL8DGSaE= + dependencies: + hash.js "^1.0.3" + minimalistic-assert "^1.0.0" + minimalistic-crypto-utils "^1.0.1" + +inherits@^2.0.3, inherits@^2.0.4: + version "2.0.4" + resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c" + integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ== + +jsbi@3.1.3: + version "3.1.3" + resolved "https://registry.yarnpkg.com/jsbi/-/jsbi-3.1.3.tgz#f024b340032f7c7caaa6ca4b32b55e8d33f6e897" + integrity sha512-nBJqA0C6Qns+ZxurbEoIR56wyjiUszpNy70FHvxO5ervMoCbZVE3z3kxr5nKGhlxr/9MhKTSUBs7cAwwuf3g9w== + +map-obj@^4.1.0: + version "4.3.0" + resolved "https://registry.yarnpkg.com/map-obj/-/map-obj-4.3.0.tgz#9304f906e93faae70880da102a9f1df0ea8bb05a" + integrity sha512-hdN1wVrZbb29eBGiGjJbeP8JbKjq1urkHJ/LIP/NY48MZ1QVXUsQBV1G1zvYFHn1XE06cwjBsOI2K3Ulnj1YXQ== + +minimalistic-assert@^1.0.0, minimalistic-assert@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/minimalistic-assert/-/minimalistic-assert-1.0.1.tgz#2e194de044626d4a10e7f7fbc00ce73e83e4d5c7" + integrity sha512-UtJcAD4yEaGtjPezWuO9wC4nwUnVH/8/Im3yEHQP4b67cXlD/Qr9hdITCU1xDbSEXg2XKNaP8jsReV7vQd00/A== + +minimalistic-crypto-utils@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/minimalistic-crypto-utils/-/minimalistic-crypto-utils-1.0.1.tgz#f6c00c1c0b082246e5c4d99dfb8c7c083b2b582a" + integrity sha1-9sAMHAsIIkblxNmd+4x8CDsrWCo= + +quick-lru@^5.1.1: + version "5.1.1" + resolved "https://registry.yarnpkg.com/quick-lru/-/quick-lru-5.1.1.tgz#366493e6b3e42a3a6885e2e99d18f80fb7a8c932" + integrity sha512-WuyALRjWPDGtt/wzJiadO5AXY+8hZ80hVpe6MyivgraREW751X3SbhRvG3eLKOYN+8VEvqLcf3wdnt44Z4S4SA== + +tslib@2.3.1: + version "2.3.1" + resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.3.1.tgz#e8a335add5ceae51aa261d32a490158ef042ef01" + integrity sha512-77EbyPPpMz+FRFRuAFlWMtmgUWGe9UOG2Z25NqCwiIjRhOf5iKGuzSe5P2w1laq+FkRy4p+PCuVkJSGkzTEKVw== + +type-fest@^1.2.1: + version "1.4.0" + resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-1.4.0.tgz#e9fb813fe3bf1744ec359d55d1affefa76f14be1" + integrity sha512-yGSza74xk0UG8k+pLh5oeoYirvIiWo5t0/o3zHHAO2tRDiZcxWP7fywNlXhqb6/r6sWvwi+RsyQMWhVLe4BVuA== diff --git a/example/define-flashsigner.ts b/example/define-flashsigner.ts index 97a5d57..feb1f40 100644 --- a/example/define-flashsigner.ts +++ b/example/define-flashsigner.ts @@ -14,24 +14,24 @@ const run = async () => { } const ckb = service.collector.getCkb() const defineLock = addressToScript(TEST_ADDRESS) - let {rawTx, cotaId} = await generateDefineCotaTx(service, defineLock, 100, "0x00") + let { rawTx, cotaId } = await generateDefineCotaTx(service, defineLock, 100, '0x00') console.log(`cotaId: ${cotaId}`) const flashsingerDep: CKBComponents.CellDep = { outPoint: { - txHash: "0xb66776ff3244033fcd15312ae8b17d384c11bebbb923fce3bd896d89f4744d48", - index: "0x0", + txHash: '0xb66776ff3244033fcd15312ae8b17d384c11bebbb923fce3bd896d89f4744d48', + index: '0x0', }, - depType: "depGroup" + depType: 'depGroup', } rawTx.cellDeps.push(flashsingerDep) - rawTx.witnesses = rawTx.witnesses.map(witness => witness !== '0x' ? serializeWitnessArgs(witness) : '0x') + rawTx.witnesses = rawTx.witnesses.map(witness => (witness !== '0x' ? serializeWitnessArgs(witness) : '0x')) let signedTx = rawTx rawTx = toSnakeCase(rawTx) console.log(JSON.stringify(rawTx)) // TODO: Add witnesses signed by flashsigner - signedTx.witnesses = ["flashsigner-signed-witness"] + signedTx.witnesses = ['flashsigner-signed-witness'] console.log(JSON.stringify(signedTx)) let txHash = await ckb.rpc.sendTransaction(signedTx, 'passthrough') diff --git a/example/define.ts b/example/define.ts index 7c4e884..16e3d29 100644 --- a/example/define.ts +++ b/example/define.ts @@ -20,7 +20,7 @@ const run = async () => { } const ckb = service.collector.getCkb() const defineLock = addressToScript(TEST_ADDRESS) - let {rawTx, cotaId} = await generateDefineCotaTx(service, defineLock, 100, "0x00") + let { rawTx, cotaId } = await generateDefineCotaTx(service, defineLock, 100, '0x00') console.log(`cotaId: ${cotaId}`) const secp256k1Dep = await secp256k1CellDep(ckb) rawTx.cellDeps.push(secp256k1Dep) diff --git a/example/define/index.js b/example/define/index.js new file mode 100644 index 0000000..e3a69f6 --- /dev/null +++ b/example/define/index.js @@ -0,0 +1,30 @@ +const { addressToScript } = require('@nervosnetwork/ckb-sdk-utils') +const { Collector, Aggregator, generateDefineCotaTx } = require('@nervina-labs/cota-sdk') + +const TEST_PRIVATE_KEY = '0xc5bd09c9b954559c70a77d68bde95369e2ce910556ddc20f739080cde3b62ef2' +const TEST_ADDRESS = 'ckt1qyq0scej4vn0uka238m63azcel7cmcme7f2sxj5ska' + +const secp256k1CellDep = async ckb => { + const secp256k1Dep = (await ckb.loadDeps()).secp256k1Dep + return { outPoint: secp256k1Dep.outPoint, depType: 'depGroup' } +} + +const run = async () => { + const service = { + collector: new Collector({ ckbNodeUrl: 'http://localhost:8114', ckbIndexerUrl: 'http://localhost:8116' }), + aggregator: new Aggregator({ registryUrl: 'http://localhost:3050', cotaUrl: 'http://localhost:3030' }), + } + const ckb = service.collector.getCkb() + const defineLock = addressToScript(TEST_ADDRESS) + let { rawTx, cotaId } = await generateDefineCotaTx(service, defineLock, 100, '0x00') + console.log(`cotaId: ${cotaId}`) + const secp256k1Dep = await secp256k1CellDep(ckb) + rawTx.cellDeps.push(secp256k1Dep) + + const signedTx = ckb.signTransaction(TEST_PRIVATE_KEY)(rawTx) + console.log(JSON.stringify(signedTx)) + let txHash = await ckb.rpc.sendTransaction(signedTx, 'passthrough') + console.info(`Define cota nft tx has been sent with tx hash ${txHash}`) +} + +run() diff --git a/example/define/index.ts b/example/define/index.ts new file mode 100644 index 0000000..e61f86b --- /dev/null +++ b/example/define/index.ts @@ -0,0 +1,31 @@ +import { addressToScript } from '@nervosnetwork/ckb-sdk-utils' +import { Service, Aggregator, Collector, generateDefineCotaTx } from '@nervina-labs/cota-sdk' +import CKB from '@nervosnetwork/ckb-sdk-core' + +const TEST_PRIVATE_KEY = '0xc5bd09c9b954559c70a77d68bde95369e2ce910556ddc20f739080cde3b62ef2' +const TEST_ADDRESS = 'ckt1qyq0scej4vn0uka238m63azcel7cmcme7f2sxj5ska' + +const secp256k1CellDep = async (ckb: CKB): Promise => { + const secp256k1Dep = (await ckb.loadDeps()).secp256k1Dep + return { outPoint: secp256k1Dep.outPoint, depType: 'depGroup' } +} + +const run = async () => { + const service: Service = { + collector: new Collector({ ckbNodeUrl: 'http://localhost:8114', ckbIndexerUrl: 'http://localhost:8116' }), + aggregator: new Aggregator({ registryUrl: 'http://localhost:3050', cotaUrl: 'http://localhost:3030' }), + } + const ckb = service.collector.getCkb() + const defineLock = addressToScript(TEST_ADDRESS) + let { rawTx, cotaId } = await generateDefineCotaTx(service, defineLock, 100, '0x00') + console.log(`cotaId: ${cotaId}`) + const secp256k1Dep = await secp256k1CellDep(ckb) + rawTx.cellDeps.push(secp256k1Dep) + + const signedTx = ckb.signTransaction(TEST_PRIVATE_KEY)(rawTx) + console.log(JSON.stringify(signedTx)) + let txHash = await ckb.rpc.sendTransaction(signedTx, 'passthrough') + console.info(`Define cota nft tx has been sent with tx hash ${txHash}`) +} + +run() diff --git a/example/define/package.json b/example/define/package.json new file mode 100644 index 0000000..7cf759b --- /dev/null +++ b/example/define/package.json @@ -0,0 +1,11 @@ +{ + "name": "define-example", + "version": "1.0.0", + "description": "The example of defining cota nft", + "main": "index.js", + "license": "MIT", + "dependencies": { + "@nervosnetwork/ckb-sdk-utils": "^0.102.0", + "@nervina-labs/cota-sdk": "0.1.0" + } +} diff --git a/example/define/yarn.lock b/example/define/yarn.lock new file mode 100644 index 0000000..c9a1b55 --- /dev/null +++ b/example/define/yarn.lock @@ -0,0 +1,168 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +"@nervina-labs/cota-sdk@0.1.0": + version "0.1.0" + resolved "https://registry.yarnpkg.com/@nervina-labs/cota-sdk/-/cota-sdk-0.1.0.tgz#012768d8f048108e93d04269a385e82697716a7b" + integrity sha512-fi7OlfSMbW8IKV1XtuPaQ3JAw8U9c7rbmL33KG+UTOZzM3ws5OEsfCRukSH0cffAtH+mueEtSC+MRgW5htnfvQ== + dependencies: + "@nervosnetwork/ckb-sdk-core" "^0.102.0" + "@nervosnetwork/ckb-sdk-utils" "^0.102.0" + "@nervosnetwork/ckb-types" "^0.102.0" + axios "^0.26.0" + camelcase-keys "^7.0.2" + +"@nervosnetwork/ckb-sdk-core@^0.102.0": + version "0.102.1" + resolved "https://registry.yarnpkg.com/@nervosnetwork/ckb-sdk-core/-/ckb-sdk-core-0.102.1.tgz#680c1ebe6425549132d96da288d7600b24dcd98a" + integrity sha512-q6T5ooNYzA9ZhlV4ad39Zn6ubVOa7gCFZ31O+LJAhSxRZDpKctTiwVbgvNFoa2vDvTZej6hT98fb2pACW4rYnQ== + dependencies: + "@nervosnetwork/ckb-sdk-rpc" "0.102.1" + "@nervosnetwork/ckb-sdk-utils" "0.102.1" + "@nervosnetwork/ckb-types" "0.102.1" + tslib "2.3.1" + +"@nervosnetwork/ckb-sdk-rpc@0.102.1": + version "0.102.1" + resolved "https://registry.yarnpkg.com/@nervosnetwork/ckb-sdk-rpc/-/ckb-sdk-rpc-0.102.1.tgz#6b9d98071428e81d1c28880991a4455458850478" + integrity sha512-xCIhiAunHt9peEY+doxNk28Nc74I8RAN0JHyyhxOYWTZqWg7EwvPeI9CNSi0Do4M/zgJ3Cln6yWjJQrSChMQuA== + dependencies: + "@nervosnetwork/ckb-sdk-utils" "0.102.1" + axios "0.21.4" + tslib "2.3.1" + +"@nervosnetwork/ckb-sdk-utils@0.102.1", "@nervosnetwork/ckb-sdk-utils@^0.102.0": + version "0.102.1" + resolved "https://registry.yarnpkg.com/@nervosnetwork/ckb-sdk-utils/-/ckb-sdk-utils-0.102.1.tgz#5e9bd9e2ebfbbd7b55f915e087e5cbd557bab34f" + integrity sha512-M+6P+8qhXiJoV5e77KtCLQQoKvuOAYdaCd6FEF5V6OhURbkRdhy6gxulY2YB7chlaw7QbvPhlVHlWB1DFOjkHw== + dependencies: + "@nervosnetwork/ckb-types" "0.102.1" + bech32 "2.0.0" + elliptic "6.5.4" + jsbi "3.1.3" + tslib "2.3.1" + +"@nervosnetwork/ckb-types@0.102.1", "@nervosnetwork/ckb-types@^0.102.0": + version "0.102.1" + resolved "https://registry.yarnpkg.com/@nervosnetwork/ckb-types/-/ckb-types-0.102.1.tgz#95278bfdbc489ad6225ed8125a235ee7ba378882" + integrity sha512-WFE/wPwZm+UYpgKmP2QX2zQyzCkAocfjQyP8DWJds3KhsI+7Z9EjFZ3lzDEWclU/KokC1fOd9+Bu5DMrfkcZng== + +axios@0.21.4: + version "0.21.4" + resolved "https://registry.yarnpkg.com/axios/-/axios-0.21.4.tgz#c67b90dc0568e5c1cf2b0b858c43ba28e2eda575" + integrity sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg== + dependencies: + follow-redirects "^1.14.0" + +axios@^0.26.0: + version "0.26.0" + resolved "https://registry.yarnpkg.com/axios/-/axios-0.26.0.tgz#9a318f1c69ec108f8cd5f3c3d390366635e13928" + integrity sha512-lKoGLMYtHvFrPVt3r+RBMp9nh34N0M8zEfCWqdWZx6phynIEhQqAdydpyBAAG211zlhX9Rgu08cOamy6XjE5Og== + dependencies: + follow-redirects "^1.14.8" + +bech32@2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/bech32/-/bech32-2.0.0.tgz#078d3686535075c8c79709f054b1b226a133b355" + integrity sha512-LcknSilhIGatDAsY1ak2I8VtGaHNhgMSYVxFrGLXv+xLHytaKZKcaUJJUE7qmBr7h33o5YQwP55pMI0xmkpJwg== + +bn.js@^4.11.9: + version "4.12.0" + resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-4.12.0.tgz#775b3f278efbb9718eec7361f483fb36fbbfea88" + integrity sha512-c98Bf3tPniI+scsdk237ku1Dc3ujXQTSgyiPUDEOe7tRkhrqridvh8klBv0HCEso1OLOYcHuCv/cS6DNxKH+ZA== + +brorand@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/brorand/-/brorand-1.1.0.tgz#12c25efe40a45e3c323eb8675a0a0ce57b22371f" + integrity sha1-EsJe/kCkXjwyPrhnWgoM5XsiNx8= + +camelcase-keys@^7.0.2: + version "7.0.2" + resolved "https://registry.yarnpkg.com/camelcase-keys/-/camelcase-keys-7.0.2.tgz#d048d8c69448745bb0de6fc4c1c52a30dfbe7252" + integrity sha512-Rjs1H+A9R+Ig+4E/9oyB66UC5Mj9Xq3N//vcLf2WzgdTi/3gUu3Z9KoqmlrEG4VuuLK8wJHofxzdQXz/knhiYg== + dependencies: + camelcase "^6.3.0" + map-obj "^4.1.0" + quick-lru "^5.1.1" + type-fest "^1.2.1" + +camelcase@^6.3.0: + version "6.3.0" + resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-6.3.0.tgz#5685b95eb209ac9c0c177467778c9c84df58ba9a" + integrity sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA== + +elliptic@6.5.4: + version "6.5.4" + resolved "https://registry.yarnpkg.com/elliptic/-/elliptic-6.5.4.tgz#da37cebd31e79a1367e941b592ed1fbebd58abbb" + integrity sha512-iLhC6ULemrljPZb+QutR5TQGB+pdW6KGD5RSegS+8sorOZT+rdQFbsQFJgvN3eRqNALqJer4oQ16YvJHlU8hzQ== + dependencies: + bn.js "^4.11.9" + brorand "^1.1.0" + hash.js "^1.0.0" + hmac-drbg "^1.0.1" + inherits "^2.0.4" + minimalistic-assert "^1.0.1" + minimalistic-crypto-utils "^1.0.1" + +follow-redirects@^1.14.0, follow-redirects@^1.14.8: + version "1.14.9" + resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.9.tgz#dd4ea157de7bfaf9ea9b3fbd85aa16951f78d8d7" + integrity sha512-MQDfihBQYMcyy5dhRDJUHcw7lb2Pv/TuE6xP1vyraLukNDHKbDxDNaOE3NbCAdKQApno+GPRyo1YAp89yCjK4w== + +hash.js@^1.0.0, hash.js@^1.0.3: + version "1.1.7" + resolved "https://registry.yarnpkg.com/hash.js/-/hash.js-1.1.7.tgz#0babca538e8d4ee4a0f8988d68866537a003cf42" + integrity sha512-taOaskGt4z4SOANNseOviYDvjEJinIkRgmp7LbKP2YTTmVxWBl87s/uzK9r+44BclBSp2X7K1hqeNfz9JbBeXA== + dependencies: + inherits "^2.0.3" + minimalistic-assert "^1.0.1" + +hmac-drbg@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/hmac-drbg/-/hmac-drbg-1.0.1.tgz#d2745701025a6c775a6c545793ed502fc0c649a1" + integrity sha1-0nRXAQJabHdabFRXk+1QL8DGSaE= + dependencies: + hash.js "^1.0.3" + minimalistic-assert "^1.0.0" + minimalistic-crypto-utils "^1.0.1" + +inherits@^2.0.3, inherits@^2.0.4: + version "2.0.4" + resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c" + integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ== + +jsbi@3.1.3: + version "3.1.3" + resolved "https://registry.yarnpkg.com/jsbi/-/jsbi-3.1.3.tgz#f024b340032f7c7caaa6ca4b32b55e8d33f6e897" + integrity sha512-nBJqA0C6Qns+ZxurbEoIR56wyjiUszpNy70FHvxO5ervMoCbZVE3z3kxr5nKGhlxr/9MhKTSUBs7cAwwuf3g9w== + +map-obj@^4.1.0: + version "4.3.0" + resolved "https://registry.yarnpkg.com/map-obj/-/map-obj-4.3.0.tgz#9304f906e93faae70880da102a9f1df0ea8bb05a" + integrity sha512-hdN1wVrZbb29eBGiGjJbeP8JbKjq1urkHJ/LIP/NY48MZ1QVXUsQBV1G1zvYFHn1XE06cwjBsOI2K3Ulnj1YXQ== + +minimalistic-assert@^1.0.0, minimalistic-assert@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/minimalistic-assert/-/minimalistic-assert-1.0.1.tgz#2e194de044626d4a10e7f7fbc00ce73e83e4d5c7" + integrity sha512-UtJcAD4yEaGtjPezWuO9wC4nwUnVH/8/Im3yEHQP4b67cXlD/Qr9hdITCU1xDbSEXg2XKNaP8jsReV7vQd00/A== + +minimalistic-crypto-utils@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/minimalistic-crypto-utils/-/minimalistic-crypto-utils-1.0.1.tgz#f6c00c1c0b082246e5c4d99dfb8c7c083b2b582a" + integrity sha1-9sAMHAsIIkblxNmd+4x8CDsrWCo= + +quick-lru@^5.1.1: + version "5.1.1" + resolved "https://registry.yarnpkg.com/quick-lru/-/quick-lru-5.1.1.tgz#366493e6b3e42a3a6885e2e99d18f80fb7a8c932" + integrity sha512-WuyALRjWPDGtt/wzJiadO5AXY+8hZ80hVpe6MyivgraREW751X3SbhRvG3eLKOYN+8VEvqLcf3wdnt44Z4S4SA== + +tslib@2.3.1: + version "2.3.1" + resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.3.1.tgz#e8a335add5ceae51aa261d32a490158ef042ef01" + integrity sha512-77EbyPPpMz+FRFRuAFlWMtmgUWGe9UOG2Z25NqCwiIjRhOf5iKGuzSe5P2w1laq+FkRy4p+PCuVkJSGkzTEKVw== + +type-fest@^1.2.1: + version "1.4.0" + resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-1.4.0.tgz#e9fb813fe3bf1744ec359d55d1affefa76f14be1" + integrity sha512-yGSza74xk0UG8k+pLh5oeoYirvIiWo5t0/o3zHHAO2tRDiZcxWP7fywNlXhqb6/r6sWvwi+RsyQMWhVLe4BVuA== diff --git a/example/mint.ts b/example/mint.ts index 763d8b0..0365f5b 100644 --- a/example/mint.ts +++ b/example/mint.ts @@ -23,8 +23,8 @@ const run = async () => { const mintLock = addressToScript(TEST_ADDRESS) const mintCotaInfo: MintCotaInfo = { - cotaId: "0x1deb31f603652bf59ff5027b522e1d81c288b72f", - withdrawals: [ + cotaId: '0x1deb31f603652bf59ff5027b522e1d81c288b72f', + withdrawals: [ { tokenIndex: '0x00000000', state: '0x00', @@ -37,7 +37,7 @@ const run = async () => { characteristic: '0xa505050505050505050505050505050505050505', toLockScript: serializeScript(addressToScript(RECEIVER_ADDRESS)), }, - ] + ], } let rawTx = await generateMintCotaTx(service, mintLock, mintCotaInfo) diff --git a/example/registry.js b/example/registry.js index 62ca2c7..e02d2cd 100644 --- a/example/registry.js +++ b/example/registry.js @@ -6,7 +6,7 @@ const { } = require('@nervosnetwork/ckb-sdk-utils') const { Collector } = require('../lib/collector') const { Aggregator } = require('../lib/aggregator') -const { TestnetDeployment } = require('../lib/constants') +const { getAlwaysSuccessLock } = require('../lib/constants') const { generateRegisterCotaTx } = require('../lib/service/registry') const signWitnesses = require('@nervosnetwork/ckb-sdk-core/lib/signWitnesses') @@ -30,7 +30,7 @@ const run = async () => { const secp256k1Dep = await secp256k1CellDep(ckb) rawTx.cellDeps.push(secp256k1Dep) - const registryLock = TestnetDeployment.AlwaysSuccessLockScript + const registryLock = getAlwaysSuccessLock(false) let keyMap = new Map() keyMap.set(scriptToHash(registryLock), '') diff --git a/example/registry.ts b/example/registry.ts index 5d80c1b..eddb621 100644 --- a/example/registry.ts +++ b/example/registry.ts @@ -1,7 +1,7 @@ import { addressToScript, rawTransactionToHash, scriptToHash, serializeWitnessArgs } from '@nervosnetwork/ckb-sdk-utils' import { Collector } from '../src/collector' import { Aggregator } from '../src/aggregator' -import { TestnetDeployment } from '../src/constants' +import { getAlwaysSuccessLock } from '../src/constants' import { generateRegisterCotaTx } from '../src/service/registry' import { Service } from '../src' import CKB from '@nervosnetwork/ckb-sdk-core' @@ -27,7 +27,7 @@ const run = async () => { const secp256k1Dep = await secp256k1CellDep(ckb) rawTx.cellDeps.push(secp256k1Dep) - const registryLock = TestnetDeployment.AlwaysSuccessLockScript + const registryLock = getAlwaysSuccessLock(false) let keyMap = new Map() keyMap.set(scriptToHash(registryLock), '') diff --git a/example/transfer.ts b/example/transfer.ts index de9dc72..cdb7343 100644 --- a/example/transfer.ts +++ b/example/transfer.ts @@ -28,7 +28,7 @@ const run = async () => { { cotaId: '0x1deb31f603652bf59ff5027b522e1d81c288b72f', tokenIndex: '0x00000001', - toLockScript: serializeScript(addressToScript(OTHER_ADDRESS)) + toLockScript: serializeScript(addressToScript(OTHER_ADDRESS)), }, ] let rawTx = await generateTransferCotaTx(service, cotaLock, withdrawLock, transfers) diff --git a/example/update.ts b/example/update.ts index ff1d7c2..cb75be0 100644 --- a/example/update.ts +++ b/example/update.ts @@ -26,8 +26,8 @@ const run = async () => { cotaId: '0x1deb31f603652bf59ff5027b522e1d81c288b72f', tokenIndex: '0x00000000', state: '0x00', - characteristic: '0x0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a' - } + characteristic: '0x0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a', + }, ] let rawTx = await generateUpdateCotaTx(service, cotaLock, cotaNfts) diff --git a/example/withdraw.ts b/example/withdraw.ts index 9ca3e3b..666a074 100644 --- a/example/withdraw.ts +++ b/example/withdraw.ts @@ -27,7 +27,7 @@ const run = async () => { { cotaId: '0x1deb31f603652bf59ff5027b522e1d81c288b72f', tokenIndex: '0x00000000', - toLockScript: serializeScript(toLock) + toLockScript: serializeScript(toLock), }, ] let rawTx = await generateWithdrawCotaTx(service, withdrawLock, withdrawals) diff --git a/package.json b/package.json index 576460d..3c91fbf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@nervina-labs/cota-sdk", - "version": "0.1.0", + "version": "0.1.1", "description": "The SDK of CoTA", "repository": "git@github.com:nervina-labs/cota-sdk-js.git", "author": "duanyytop ", @@ -24,20 +24,20 @@ "prepare": "husky install" }, "dependencies": { - "@nervosnetwork/ckb-sdk-core": "^0.102.0", - "@nervosnetwork/ckb-sdk-utils": "^0.102.0", - "@nervosnetwork/ckb-types": "^0.102.0", + "@nervosnetwork/ckb-sdk-core": "^0.102.1", + "@nervosnetwork/ckb-sdk-utils": "^0.102.1", + "@nervosnetwork/ckb-types": "^0.102.1", "axios": "^0.26.0", "camelcase-keys": "^7.0.2" }, "devDependencies": { "@types/crypto-js": "4.1.1", - "@types/node": "17.0.18", - "@typescript-eslint/parser": "5.12.0", + "@types/node": "17.0.19", + "@typescript-eslint/parser": "5.12.1", "babel-eslint": "10.1.0", "convert-keys": "1.3.4", "eslint": "8.9.0", - "eslint-config-prettier": "8.3.0", + "eslint-config-prettier": "8.4.0", "eslint-plugin-import": "2.25.4", "eslint-plugin-prettier": "4.0.0", "husky": "7.0.4", diff --git a/src/constants/index.ts b/src/constants/index.ts index ea68597..af93245 100644 --- a/src/constants/index.ts +++ b/src/constants/index.ts @@ -1,7 +1,7 @@ -export const FEE = BigInt(1600000) +export const FEE = BigInt(160000) export const MIN_CAPACITY = BigInt(61) * BigInt(100000000) -export const TestnetDeployment = { +const TestnetInfo = { RegistryTypeScript: { codeHash: '0x9302db6cc1344b81a5efee06962abcb40427ecfcbe69d471b01b2658ed948075', hashType: 'type', @@ -30,3 +30,47 @@ export const TestnetDeployment = { depType: 'code', } as CKBComponents.CellDep, } + +const MainnetInfo = { + RegistryTypeScript: { + codeHash: '0x90ca618be6c15f5857d3cbd09f9f24ca6770af047ba9ee70989ec3b229419ac7', + hashType: 'type', + args: '0x563631b49cee549f3585ab4dde5f9d590f507f1f', + } as CKBComponents.Script, + + CotaTypeScript: { + codeHash: '0x1122a4fb54697cf2e6e3a96c9d80fd398a936559b90954c6e88eb7ba0cf652df', + hashType: 'type', + args: '0x', + } as CKBComponents.Script, + + CotaTypeDep: { + outPoint: { txHash: '0xae2d5838730fc096e68fe839aea50d294493e10054513c10ca35e77e82e9243b', index: '0x0' }, + depType: 'depGroup', + } as CKBComponents.CellDep, + + AlwaysSuccessLockScript: { + codeHash: '0xd483925160e4232b2cb29f012e8380b7b612d71cf4e79991476b6bcf610735f6', + hashType: 'data', + args: '0x', + } as CKBComponents.Script, + + AlwaysSuccessLockDep: { + outPoint: { txHash: '0x81e22f4bb39080b112e5efb18e3fad65ebea735eac2f9c495b7f4d3b4faa377d', index: '0x0' }, + depType: 'code', + } as CKBComponents.CellDep, +} + +export const getCotaTypeScript = (isMainnet = false) => + isMainnet ? MainnetInfo.CotaTypeScript : TestnetInfo.CotaTypeScript + +export const getReistryTypeScript = (isMainnet = false) => + isMainnet ? MainnetInfo.RegistryTypeScript : TestnetInfo.RegistryTypeScript + +export const getCotaCellDep = (isMainnet = false) => (isMainnet ? MainnetInfo.CotaTypeDep : TestnetInfo.CotaTypeDep) + +export const getAlwaysSuccessLock = (isMainnet = false) => + isMainnet ? MainnetInfo.AlwaysSuccessLockScript : TestnetInfo.AlwaysSuccessLockScript + +export const getAlwaysSuccessCellDep = (isMainnet = false) => + isMainnet ? MainnetInfo.AlwaysSuccessLockDep : TestnetInfo.AlwaysSuccessLockDep diff --git a/src/service/cota/claim.ts b/src/service/cota/claim.ts index cdce281..381f02c 100644 --- a/src/service/cota/claim.ts +++ b/src/service/cota/claim.ts @@ -1,6 +1,6 @@ import { scriptToHash, serializeScript } from '@nervosnetwork/ckb-sdk-utils' import { ClaimReq, Service, Claim } from '../..' -import { FEE, TestnetDeployment } from '../../constants' +import { FEE, getCotaTypeScript, getCotaCellDep } from '../../constants' export const generateClaimCotaTx = async ( service: Service, @@ -8,8 +8,9 @@ export const generateClaimCotaTx = async ( withdrawalLock: CKBComponents.Script, claims: Claim[], fee = FEE, + isMainnet = false, ) => { - const cotaType = TestnetDeployment.CotaTypeScript + const cotaType = getCotaTypeScript(isMainnet) const cotaCells = await service.collector.getCells(cotaLock, cotaType) if (!cotaCells || cotaCells.length === 0) { throw new Error("Cota cell doesn't exist") @@ -43,7 +44,7 @@ export const generateClaimCotaTx = async ( const outputsData = [`0x00${smtRootHash}`] const withdrawalCellDep: CKBComponents.CellDep = { outPoint: withdrawalCotaCell.outPoint, depType: 'code' } - const cellDeps = [withdrawalCellDep, TestnetDeployment.CotaTypeDep] + const cellDeps = [withdrawalCellDep, getCotaCellDep(isMainnet)] const rawTx = { version: '0x0', diff --git a/src/service/cota/define.ts b/src/service/cota/define.ts index 5314014..a602c48 100644 --- a/src/service/cota/define.ts +++ b/src/service/cota/define.ts @@ -1,7 +1,7 @@ import { hexToBytes, PERSONAL, scriptToHash, serializeInput } from '@nervosnetwork/ckb-sdk-utils' import blake2b from '@nervosnetwork/ckb-sdk-utils/lib/crypto/blake2b' import { Byte, Service, DefineReq } from '../..' -import { FEE, TestnetDeployment } from '../../constants' +import { FEE, getCotaTypeScript, getCotaCellDep } from '../../constants' import { u8ToHex, u32ToBe, append0x } from '../../utils' const generateCotaId = (firstInput: CKBComponents.CellInput, definesIndex: number) => { @@ -18,8 +18,9 @@ export const generateDefineCotaTx = async ( total: number, confiure: Byte, fee = FEE, + isMainnet = false, ) => { - const cotaType = TestnetDeployment.CotaTypeScript + const cotaType = getCotaTypeScript(isMainnet) const cotaCells = await service.collector.getCells(cotaLock, cotaType) if (!cotaCells || cotaCells.length === 0) { throw new Error("Cota cell doesn't exist") @@ -50,7 +51,7 @@ export const generateDefineCotaTx = async ( const cotaCellData = `0x00${smtRootHash}` const outputsData = [cotaCellData] - const cellDeps = [TestnetDeployment.CotaTypeDep] + const cellDeps = [getCotaCellDep(isMainnet)] const rawTx = { version: '0x0', diff --git a/src/service/cota/mint.ts b/src/service/cota/mint.ts index 4ca2a71..e0b7a4f 100644 --- a/src/service/cota/mint.ts +++ b/src/service/cota/mint.ts @@ -1,6 +1,6 @@ import { scriptToHash, serializeOutPoint } from '@nervosnetwork/ckb-sdk-utils' -import { Byte, Byte20, Byte4, Bytes, MintReq, Service } from '../..' -import { FEE, TestnetDeployment } from '../../constants' +import { MintReq, Service } from '../..' +import { FEE, getCotaTypeScript, getCotaCellDep } from '../../constants' import { MintCotaInfo } from '../../types/service' import { append0x } from '../../utils' @@ -9,8 +9,9 @@ export const generateMintCotaTx = async ( cotaLock: CKBComponents.Script, mintCotaInfo: MintCotaInfo, fee = FEE, + isMainnet = false, ) => { - const cotaType = TestnetDeployment.CotaTypeScript + const cotaType = getCotaTypeScript(isMainnet) const cotaCells = await service.collector.getCells(cotaLock, cotaType) if (!cotaCells || cotaCells.length === 0) { throw new Error("Cota cell doesn't exist") @@ -37,7 +38,7 @@ export const generateMintCotaTx = async ( const cotaCellData = `0x00${smtRootHash}` const outputsData = [cotaCellData] - const cellDeps = [TestnetDeployment.CotaTypeDep] + const cellDeps = [getCotaCellDep(isMainnet)] const rawTx = { version: '0x0', diff --git a/src/service/cota/transfer.ts b/src/service/cota/transfer.ts index 54b8cf1..81bb569 100644 --- a/src/service/cota/transfer.ts +++ b/src/service/cota/transfer.ts @@ -1,6 +1,6 @@ import { scriptToHash, serializeOutPoint, serializeScript } from '@nervosnetwork/ckb-sdk-utils' import { Service, TransferReq, TransferWithdrawal } from '../..' -import { FEE, TestnetDeployment } from '../../constants' +import { FEE, getCotaTypeScript, getCotaCellDep } from '../../constants' import { append0x } from '../../utils/hex' export const generateTransferCotaTx = async ( @@ -9,8 +9,9 @@ export const generateTransferCotaTx = async ( withdrawalLock: CKBComponents.Script, transfers: TransferWithdrawal[], fee = FEE, + isMainnet = false, ) => { - const cotaType = TestnetDeployment.CotaTypeScript + const cotaType = getCotaTypeScript(isMainnet) const cotaCells = await service.collector.getCells(cotaLock, cotaType) if (!cotaCells || cotaCells.length === 0) { throw new Error("Cota cell doesn't exist") @@ -43,7 +44,7 @@ export const generateTransferCotaTx = async ( const outputsData = [`0x00${smtRootHash}`] const withdrawalCellDep: CKBComponents.CellDep = { outPoint: withdrawalCotaCell.outPoint, depType: 'code' } - const cellDeps = [withdrawalCellDep, TestnetDeployment.CotaTypeDep] + const cellDeps = [withdrawalCellDep, getCotaCellDep(isMainnet)] const rawTx = { version: '0x0', cellDeps, diff --git a/src/service/cota/update.ts b/src/service/cota/update.ts index a08c00a..2ac395c 100644 --- a/src/service/cota/update.ts +++ b/src/service/cota/update.ts @@ -1,14 +1,15 @@ import { scriptToHash } from '@nervosnetwork/ckb-sdk-utils' import { CotaNft, Service, UpdateReq } from '../..' -import { FEE, TestnetDeployment } from '../../constants' +import { FEE, getCotaTypeScript, getCotaCellDep } from '../../constants' export const generateUpdateCotaTx = async ( service: Service, cotaLock: CKBComponents.Script, cotaNfts: CotaNft[], fee = FEE, + isMainnet = false, ) => { - const cotaType = TestnetDeployment.CotaTypeScript + const cotaType = getCotaTypeScript(isMainnet) const cotaCells = await service.collector.getCells(cotaLock, cotaType) if (!cotaCells || cotaCells.length === 0) { throw new Error("Cota cell doesn't exist") @@ -28,7 +29,7 @@ export const generateUpdateCotaTx = async ( } const { smtRootHash, updateSmtEntry } = await service.aggregator.generateUpdateCotaSmt(updateReq) const outputsData = [`0x00${smtRootHash}`] - const cellDeps = [TestnetDeployment.CotaTypeDep] + const cellDeps = [getCotaCellDep(isMainnet)] const rawTx = { version: '0x0', cellDeps, diff --git a/src/service/cota/withdraw.ts b/src/service/cota/withdraw.ts index e34a529..e517bc3 100644 --- a/src/service/cota/withdraw.ts +++ b/src/service/cota/withdraw.ts @@ -1,6 +1,6 @@ import { scriptToHash, serializeOutPoint } from '@nervosnetwork/ckb-sdk-utils' import { Service, TransferWithdrawal, WithdrawalReq } from '../../types' -import { FEE, TestnetDeployment } from '../../constants' +import { FEE, getCotaTypeScript, getCotaCellDep } from '../../constants' import { append0x } from '../../utils' export const generateWithdrawCotaTx = async ( @@ -8,8 +8,9 @@ export const generateWithdrawCotaTx = async ( cotaLock: CKBComponents.Script, withdrawals: TransferWithdrawal[], fee = FEE, + isMainnet = false, ) => { - const cotaType = TestnetDeployment.CotaTypeScript + const cotaType = getCotaTypeScript(isMainnet) const cotaCells = await service.collector.getCells(cotaLock, cotaType) if (!cotaCells || cotaCells.length === 0) { throw new Error("Cota cell doesn't exist") @@ -31,7 +32,7 @@ export const generateWithdrawCotaTx = async ( } const { smtRootHash, withdrawalSmtEntry } = await service.aggregator.generateWithdrawalCotaSmt(withdrawalReq) const outputsData = [`0x00${smtRootHash}`] - const cellDeps = [TestnetDeployment.CotaTypeDep] + const cellDeps = [getCotaCellDep(isMainnet)] const rawTx = { version: '0x0', cellDeps, diff --git a/src/service/registry/index.ts b/src/service/registry/index.ts index 4ab5f93..d215a62 100644 --- a/src/service/registry/index.ts +++ b/src/service/registry/index.ts @@ -1,6 +1,13 @@ import { scriptToHash, serializeWitnessArgs } from '@nervosnetwork/ckb-sdk-utils' import { Service } from '../..' -import { FEE, TestnetDeployment } from '../../constants' +import { + FEE, + getAlwaysSuccessLock, + getCotaTypeScript, + getReistryTypeScript, + getAlwaysSuccessCellDep, + getCotaCellDep, +} from '../../constants' import { append0x, remove0x } from '../../utils/hex' const COTA_CELL_CAPACITY = BigInt(150) * BigInt(100000000) @@ -8,11 +15,12 @@ const COTA_CELL_CAPACITY = BigInt(150) * BigInt(100000000) const generateCotaOutputs = async ( inputCapacity: bigint, cotaLocks: CKBComponents.Script[], + isMainnet = false, ): Promise => { - const registryLock = TestnetDeployment.AlwaysSuccessLockScript + const registryLock = getAlwaysSuccessLock(isMainnet) let outputs: CKBComponents.CellOutput[] = cotaLocks.map(lock => { const args = append0x(remove0x(scriptToHash(lock)).slice(0, 40)) - const cotaType = { ...TestnetDeployment.CotaTypeScript, args } + const cotaType = { ...getCotaTypeScript(isMainnet), args } return { capacity: `0x${COTA_CELL_CAPACITY.toString(16)}`, lock, @@ -35,10 +43,11 @@ export const generateRegisterCotaTx = async ( cotaLocks: CKBComponents.Script[], lock: CKBComponents.Script, fee = FEE, + isMainnet = false, ): Promise => { const cotaCount = BigInt(cotaLocks.length) - const registryLock = TestnetDeployment.AlwaysSuccessLockScript - const registryType = TestnetDeployment.RegistryTypeScript + const registryLock = getAlwaysSuccessLock(isMainnet) + const registryType = getReistryTypeScript(isMainnet) const registryCells = await service.collector.getCells(registryLock, registryType) if (!registryCells || registryCells.length === 0) { throw new Error("Registry cell doesn't exist") @@ -70,7 +79,7 @@ export const generateRegisterCotaTx = async ( const outputsData = outputs.map((_, i) => (i === 0 ? registryCellData : i !== outputs.length - 1 ? '0x00' : '0x')) - const cellDeps = [TestnetDeployment.AlwaysSuccessLockDep, TestnetDeployment.CotaTypeDep] + const cellDeps = [getAlwaysSuccessCellDep(isMainnet), getCotaCellDep(isMainnet)] let rawTx = { version: '0x0', diff --git a/yarn.lock b/yarn.lock index 94e3807..f26c9e2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -148,7 +148,7 @@ resolved "https://registry.yarnpkg.com/@humanwhocodes/object-schema/-/object-schema-1.2.1.tgz#b520529ec21d8e5945a1851dfd1c32e94e39ff45" integrity sha512-ZnQMnLV4e7hDlUvw8H+U8ASL02SS2Gn6+9Ac3wGGLIe7+je2AeAOxPY+izIPJDfFDb7eDjev0Us8MO1iFRN8hA== -"@nervosnetwork/ckb-sdk-core@^0.102.0": +"@nervosnetwork/ckb-sdk-core@^0.102.1": version "0.102.1" resolved "https://registry.yarnpkg.com/@nervosnetwork/ckb-sdk-core/-/ckb-sdk-core-0.102.1.tgz#680c1ebe6425549132d96da288d7600b24dcd98a" integrity sha512-q6T5ooNYzA9ZhlV4ad39Zn6ubVOa7gCFZ31O+LJAhSxRZDpKctTiwVbgvNFoa2vDvTZej6hT98fb2pACW4rYnQ== @@ -167,7 +167,7 @@ axios "0.21.4" tslib "2.3.1" -"@nervosnetwork/ckb-sdk-utils@0.102.1", "@nervosnetwork/ckb-sdk-utils@^0.102.0": +"@nervosnetwork/ckb-sdk-utils@0.102.1", "@nervosnetwork/ckb-sdk-utils@^0.102.1": version "0.102.1" resolved "https://registry.yarnpkg.com/@nervosnetwork/ckb-sdk-utils/-/ckb-sdk-utils-0.102.1.tgz#5e9bd9e2ebfbbd7b55f915e087e5cbd557bab34f" integrity sha512-M+6P+8qhXiJoV5e77KtCLQQoKvuOAYdaCd6FEF5V6OhURbkRdhy6gxulY2YB7chlaw7QbvPhlVHlWB1DFOjkHw== @@ -178,7 +178,7 @@ jsbi "3.1.3" tslib "2.3.1" -"@nervosnetwork/ckb-types@0.102.1", "@nervosnetwork/ckb-types@^0.102.0": +"@nervosnetwork/ckb-types@0.102.1", "@nervosnetwork/ckb-types@^0.102.1": version "0.102.1" resolved "https://registry.yarnpkg.com/@nervosnetwork/ckb-types/-/ckb-types-0.102.1.tgz#95278bfdbc489ad6225ed8125a235ee7ba378882" integrity sha512-WFE/wPwZm+UYpgKmP2QX2zQyzCkAocfjQyP8DWJds3KhsI+7Z9EjFZ3lzDEWclU/KokC1fOd9+Bu5DMrfkcZng== @@ -234,53 +234,53 @@ resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee" integrity sha1-7ihweulOEdK4J7y+UnC86n8+ce4= -"@types/node@17.0.18": - version "17.0.18" - resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.18.tgz#3b4fed5cfb58010e3a2be4b6e74615e4847f1074" - integrity sha512-eKj4f/BsN/qcculZiRSujogjvp5O/k4lOW5m35NopjZM/QwLOR075a8pJW5hD+Rtdm2DaCVPENS6KtSQnUD6BA== +"@types/node@17.0.19": + version "17.0.19" + resolved "https://registry.yarnpkg.com/@types/node/-/node-17.0.19.tgz#726171367f404bfbe8512ba608a09ebad810c7e6" + integrity sha512-PfeQhvcMR4cPFVuYfBN4ifG7p9c+Dlh3yUZR6k+5yQK7wX3gDgVxBly4/WkBRs9x4dmcy1TVl08SY67wwtEvmA== -"@typescript-eslint/parser@5.12.0": - version "5.12.0" - resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-5.12.0.tgz#0ca669861813df99ce54916f66f524c625ed2434" - integrity sha512-MfSwg9JMBojMUoGjUmX+D2stoQj1CBYTCP0qnnVtu9A+YQXVKNtLjasYh+jozOcrb/wau8TCfWOkQTiOAruBog== +"@typescript-eslint/parser@5.12.1": + version "5.12.1" + resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-5.12.1.tgz#b090289b553b8aa0899740d799d0f96e6f49771b" + integrity sha512-6LuVUbe7oSdHxUWoX/m40Ni8gsZMKCi31rlawBHt7VtW15iHzjbpj2WLiToG2758KjtCCiLRKZqfrOdl3cNKuw== dependencies: - "@typescript-eslint/scope-manager" "5.12.0" - "@typescript-eslint/types" "5.12.0" - "@typescript-eslint/typescript-estree" "5.12.0" + "@typescript-eslint/scope-manager" "5.12.1" + "@typescript-eslint/types" "5.12.1" + "@typescript-eslint/typescript-estree" "5.12.1" debug "^4.3.2" -"@typescript-eslint/scope-manager@5.12.0": - version "5.12.0" - resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-5.12.0.tgz#59619e6e5e2b1ce6cb3948b56014d3a24da83f5e" - integrity sha512-GAMobtIJI8FGf1sLlUWNUm2IOkIjvn7laFWyRx7CLrv6nLBI7su+B7lbStqVlK5NdLvHRFiJo2HhiDF7Ki01WQ== +"@typescript-eslint/scope-manager@5.12.1": + version "5.12.1" + resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-5.12.1.tgz#58734fd45d2d1dec49641aacc075fba5f0968817" + integrity sha512-J0Wrh5xS6XNkd4TkOosxdpObzlYfXjAFIm9QxYLCPOcHVv1FyyFCPom66uIh8uBr0sZCrtS+n19tzufhwab8ZQ== dependencies: - "@typescript-eslint/types" "5.12.0" - "@typescript-eslint/visitor-keys" "5.12.0" + "@typescript-eslint/types" "5.12.1" + "@typescript-eslint/visitor-keys" "5.12.1" -"@typescript-eslint/types@5.12.0": - version "5.12.0" - resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-5.12.0.tgz#5b4030a28222ee01e851836562c07769eecda0b8" - integrity sha512-JowqbwPf93nvf8fZn5XrPGFBdIK8+yx5UEGs2QFAYFI8IWYfrzz+6zqlurGr2ctShMaJxqwsqmra3WXWjH1nRQ== +"@typescript-eslint/types@5.12.1": + version "5.12.1" + resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-5.12.1.tgz#46a36a28ff4d946821b58fe5a73c81dc2e12aa89" + integrity sha512-hfcbq4qVOHV1YRdhkDldhV9NpmmAu2vp6wuFODL71Y0Ixak+FLeEU4rnPxgmZMnGreGEghlEucs9UZn5KOfHJA== -"@typescript-eslint/typescript-estree@5.12.0": - version "5.12.0" - resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-5.12.0.tgz#cabf545fd592722f0e2b4104711e63bf89525cd2" - integrity sha512-Dd9gVeOqt38QHR0BEA8oRaT65WYqPYbIc5tRFQPkfLquVEFPD1HAtbZT98TLBkEcCkvwDYOAvuSvAD9DnQhMfQ== +"@typescript-eslint/typescript-estree@5.12.1": + version "5.12.1" + resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-5.12.1.tgz#6a9425b9c305bcbc38e2d1d9a24c08e15e02b722" + integrity sha512-ahOdkIY9Mgbza7L9sIi205Pe1inCkZWAHE1TV1bpxlU4RZNPtXaDZfiiFWcL9jdxvW1hDYZJXrFm+vlMkXRbBw== dependencies: - "@typescript-eslint/types" "5.12.0" - "@typescript-eslint/visitor-keys" "5.12.0" + "@typescript-eslint/types" "5.12.1" + "@typescript-eslint/visitor-keys" "5.12.1" debug "^4.3.2" globby "^11.0.4" is-glob "^4.0.3" semver "^7.3.5" tsutils "^3.21.0" -"@typescript-eslint/visitor-keys@5.12.0": - version "5.12.0" - resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-5.12.0.tgz#1ac9352ed140b07ba144ebf371b743fdf537ec16" - integrity sha512-cFwTlgnMV6TgezQynx2c/4/tx9Tufbuo9LPzmWqyRC3QC4qTGkAG1C6pBr0/4I10PAI/FlYunI3vJjIcu+ZHMg== +"@typescript-eslint/visitor-keys@5.12.1": + version "5.12.1" + resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-5.12.1.tgz#f722da106c8f9695ae5640574225e45af3e52ec3" + integrity sha512-l1KSLfupuwrXx6wc0AuOmC7Ko5g14ZOQ86wJJqRbdLbXLK02pK/DPiDDqCc7BqqiiA04/eAA6ayL0bgOrAkH7A== dependencies: - "@typescript-eslint/types" "5.12.0" + "@typescript-eslint/types" "5.12.1" eslint-visitor-keys "^3.0.0" acorn-jsx@^5.3.1: @@ -638,10 +638,10 @@ escape-string-regexp@^4.0.0: resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-4.0.0.tgz#14ba83a5d373e3d311e5afca29cf5bfad965bf34" integrity sha512-TtpcNJ3XAzx3Gq8sWRzJaVajRs0uVxA2YAkdb1jm2YkPz4G6egUFAyA3n5vtEIZefPk5Wa4UXbKuS5fKkJWdgA== -eslint-config-prettier@8.3.0: - version "8.3.0" - resolved "https://registry.yarnpkg.com/eslint-config-prettier/-/eslint-config-prettier-8.3.0.tgz#f7471b20b6fe8a9a9254cc684454202886a2dd7a" - integrity sha512-BgZuLUSeKzvlL/VUjx/Yb787VQ26RU3gGjA3iiFvdsp/2bMfVIWUVP7tjxtjS0e+HP409cPlPvNkQloz8C91ew== +eslint-config-prettier@8.4.0: + version "8.4.0" + resolved "https://registry.yarnpkg.com/eslint-config-prettier/-/eslint-config-prettier-8.4.0.tgz#8e6d17c7436649e98c4c2189868562921ef563de" + integrity sha512-CFotdUcMY18nGRo5KGsnNxpznzhkopOcOo0InID+sgQssPrzjvsyKZPvOgymTFeHrFuC3Tzdf2YndhXtULK9Iw== eslint-import-resolver-node@^0.3.6: version "0.3.6"