diff --git a/CHANGELOG.md b/CHANGELOG.md index e0c905e0..48ee58bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,29 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +# [0.36.0](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.35.0...v0.36.0) (2020-09-21) + + +### Code Refactoring + +* **utils:** remove redundant default options in the address module ([d74caac](https://github.com/nervosnetwork/ckb-sdk-js/commit/d74caac7121c8d1fc81ca1e8479200a9897c1726)) + + +### Features + +* **core:** parse scripts from addresses on generating transactions ([#488](https://github.com/nervosnetwork/ckb-sdk-js/issues/488)) ([9536f08](https://github.com/nervosnetwork/ckb-sdk-js/commit/9536f0811c0531599e0ee56e01802878f9b1f165)) +* **utils:** add system scripts ([567f15a](https://github.com/nervosnetwork/ckb-sdk-js/commit/567f15aea993ce5797bcecaadeeae27c3baf4aed)) +* **utils:** add the utils#addressToScript ([a78340a](https://github.com/nervosnetwork/ckb-sdk-js/commit/a78340a496f37f0239f5115ef886e2dbd1a9974e)) + + +### BREAKING CHANGES + +* **utils:** Remove the utils#defaultAddressOptions + + + + + # [0.35.0](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.34.0...v0.35.0) (2020-08-24) diff --git a/README.md b/README.md index 1ff2564c..8dcd4f37 100644 --- a/README.md +++ b/README.md @@ -147,7 +147,7 @@ After that you can use the `ckb` object to generate addresses, send requests, et ## Basic RPC -Please see [Basic RPC](https://github.com/nervosnetwork/ckb-sdk-js/blob/develop/packages/ckb-sdk-rpc/src/Base.ts#L156) +Please see [Basic RPC](https://github.com/nervosnetwork/ckb-sdk-js/blob/develop/packages/ckb-sdk-rpc/src/Base/index.ts#L23) ## Batch Request diff --git a/lerna.json b/lerna.json index 7383e225..65d24557 100644 --- a/lerna.json +++ b/lerna.json @@ -4,5 +4,5 @@ ], "npmClient": "yarn", "useWorkspaces": true, - "version": "0.35.0" + "version": "0.36.0" } diff --git a/packages/ckb-sdk-core/CHANGELOG.md b/packages/ckb-sdk-core/CHANGELOG.md index 8e145163..25d728c0 100644 --- a/packages/ckb-sdk-core/CHANGELOG.md +++ b/packages/ckb-sdk-core/CHANGELOG.md @@ -3,6 +3,17 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +# [0.36.0](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.35.0...v0.36.0) (2020-09-21) + + +### Features + +* **core:** parse scripts from addresses on generating transactions ([#488](https://github.com/nervosnetwork/ckb-sdk-js/issues/488)) ([9536f08](https://github.com/nervosnetwork/ckb-sdk-js/commit/9536f0811c0531599e0ee56e01802878f9b1f165)) + + + + + # [0.35.0](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.34.0...v0.35.0) (2020-08-24) **Note:** Version bump only for package @nervosnetwork/ckb-sdk-core diff --git a/packages/ckb-sdk-core/__tests__/fixtures.json b/packages/ckb-sdk-core/__tests__/fixtures.json index 72ffb3a3..8c2c85b8 100644 --- a/packages/ckb-sdk-core/__tests__/fixtures.json +++ b/packages/ckb-sdk-core/__tests__/fixtures.json @@ -6,7 +6,8 @@ "outPoint": { "txHash": "0x84dcb061adebff4ef93d57c975ba9058a9be939d79ea12ee68003f6492448890", "index": "0x0" - } + }, + "depType": "depGroup" }, "daoDep": { "hashType": "type", @@ -15,7 +16,8 @@ "outPoint": { "txHash": "0x15fb8111fc78fa36da6af96c45ac4714cc9a33974fdae13cc524b29e1a488c7f", "index": "0x2" - } + }, + "depType": "depGroup" } }, "signTransaction": { @@ -360,10 +362,10 @@ } }, "generateRawTransaction": { - "testnet": { + "simple": { "params": { "fromAddress": "ckt1qyqw975zuu9svtyxgjuq44lv7mspte0n2tmqa703cd", - "toAddress": "ckt1qyqdm0tlp845spzscxc76tyxjcjgm6gudqpq4h77wx", + "toAddress": "ckt1q2n9dutjk669cfznq7httfar0gtk7qp0du3wjfvzck9l0w3k9eqhvdkr98kkxrtvuag8z2j8w4pkw2k6k4l5czshhac", "capacity": 783602013670, "safeMode": true, "isMainnet": false, @@ -525,7 +527,269 @@ "outPoint": { "txHash": "0x6347ee7ce5fc6828133590558b867097b895149a66e51cf353c361f7128e2091", "index": "0x0" + }, + "depType": "depGroup" + } + }, + "expected": { + "cellDeps": [ + { + "depType": "depGroup", + "outPoint": { + "index": "0x0", + "txHash": "0x6347ee7ce5fc6828133590558b867097b895149a66e51cf353c361f7128e2091" + } + } + ], + "headerDeps": [], + "inputs": [ + { + "previousOutput": { + "index": "0x1", + "txHash": "0x9160bef8b6a9e388a99184bfd8bea0e7795b487c77fe784120fb2bc3fb25d1b2" + }, + "since": "0x0" + }, + { + "previousOutput": { + "index": "0x2", + "txHash": "0x9160bef8b6a9e388a99184bfd8bea0e7795b487c77fe784120fb2bc3fb25d1b2" + }, + "since": "0x0" + }, + { + "previousOutput": { + "index": "0x3", + "txHash": "0x9160bef8b6a9e388a99184bfd8bea0e7795b487c77fe784120fb2bc3fb25d1b2" + }, + "since": "0x0" + }, + { + "previousOutput": { + "index": "0x4", + "txHash": "0x9160bef8b6a9e388a99184bfd8bea0e7795b487c77fe784120fb2bc3fb25d1b2" + }, + "since": "0x0" + }, + { + "previousOutput": { + "index": "0x5", + "txHash": "0x9160bef8b6a9e388a99184bfd8bea0e7795b487c77fe784120fb2bc3fb25d1b2" + }, + "since": "0x0" + }, + { + "previousOutput": { + "index": "0x6", + "txHash": "0x9160bef8b6a9e388a99184bfd8bea0e7795b487c77fe784120fb2bc3fb25d1b2" + }, + "since": "0x0" } + ], + "outputs": [ + { + "capacity": "0xb67251d5e6", + "lock": { + "args": "0x36c329ed630d6ce750712a477543672adab57f4c", + "codeHash": "0xa656f172b6b45c245307aeb5a7a37a176f002f6f22e92582c58bf7ba362e4176", + "hashType": "data" + } + }, + { + "capacity": "0x88cb4e12e", + "lock": { + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", + "codeHash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", + "hashType": "type" + } + } + ], + "outputsData": ["0x", "0x"], + "version": "0x0", + "witnesses": [] + } + }, + "complex": { + "params": { + "fromAddresses": ["ckt1qyqw975zuu9svtyxgjuq44lv7mspte0n2tmqa703cd"], + "receivePairs": [ + { + "address": "ckt1q2n9dutjk669cfznq7httfar0gtk7qp0du3wjfvzck9l0w3k9eqhvdkr98kkxrtvuag8z2j8w4pkw2k6k4l5czshhac", + "capacity": 783602013670, + "type": { + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", + "codeHash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", + "hashType": "type" + } + } + ], + "safeMode": true, + "isMainnet": false, + "cells": [ + [ + "0x641238cd95b9f5e6224da1963a0bcaa2e972dc87e56eb1ded9a35dcebbc37ff4", + [ + { + "blockHash": "0x355cd43b6b517be40ce69278b7c6d2017546e2d46557d59e31adff20646c845e", + "lock": { + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type", + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6" + }, + "outPoint": { + "txHash": "0x9160bef8b6a9e388a99184bfd8bea0e7795b487c77fe784120fb2bc3fb25d1b2", + "index": "0x0" + }, + "capacity": "0x1fd52bc92e", + "dataHash": "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "status": "live" + }, + { + "blockHash": "0x355cd43b6b517be40ce69278b7c6d2017546e2d46557d59e31adff20646c845e", + "lock": { + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type", + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6" + }, + "outPoint": { + "txHash": "0x9160bef8b6a9e388a99184bfd8bea0e7795b487c77fe784120fb2bc3fb25d1b2", + "index": "0x1" + }, + "capacity": "0x1fd52bc92e", + "dataHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "status": "live" + }, + { + "blockHash": "0x355cd43b6b517be40ce69278b7c6d2017546e2d46557d59e31adff20646c845e", + "lock": { + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type", + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6" + }, + "outPoint": { + "txHash": "0x9160bef8b6a9e388a99184bfd8bea0e7795b487c77fe784120fb2bc3fb25d1b2", + "index": "0x2" + }, + "capacity": "0x1fd52bc92e", + "dataHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "status": "live" + }, + { + "blockHash": "0x355cd43b6b517be40ce69278b7c6d2017546e2d46557d59e31adff20646c845e", + "lock": { + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type", + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6" + }, + "outPoint": { + "txHash": "0x9160bef8b6a9e388a99184bfd8bea0e7795b487c77fe784120fb2bc3fb25d1b2", + "index": "0x3" + }, + "capacity": "0x1fd52bc92e", + "dataHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "status": "live" + }, + { + "blockHash": "0x355cd43b6b517be40ce69278b7c6d2017546e2d46557d59e31adff20646c845e", + "lock": { + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type", + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6" + }, + "outPoint": { + "txHash": "0x9160bef8b6a9e388a99184bfd8bea0e7795b487c77fe784120fb2bc3fb25d1b2", + "index": "0x4" + }, + "capacity": "0x1fd52bc92e", + "dataHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "status": "live" + }, + { + "blockHash": "0x355cd43b6b517be40ce69278b7c6d2017546e2d46557d59e31adff20646c845e", + "lock": { + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type", + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6" + }, + "outPoint": { + "txHash": "0x9160bef8b6a9e388a99184bfd8bea0e7795b487c77fe784120fb2bc3fb25d1b2", + "index": "0x5" + }, + "capacity": "0x1fd52bc92e", + "dataHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "status": "live" + }, + { + "blockHash": "0x355cd43b6b517be40ce69278b7c6d2017546e2d46557d59e31adff20646c845e", + "lock": { + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type", + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6" + }, + "outPoint": { + "txHash": "0x9160bef8b6a9e388a99184bfd8bea0e7795b487c77fe784120fb2bc3fb25d1b2", + "index": "0x6" + }, + "capacity": "0x1fd52bc92e", + "dataHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "status": "live" + }, + { + "blockHash": "0x355cd43b6b517be40ce69278b7c6d2017546e2d46557d59e31adff20646c845e", + "lock": { + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type", + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6" + }, + "outPoint": { + "txHash": "0x9160bef8b6a9e388a99184bfd8bea0e7795b487c77fe784120fb2bc3fb25d1b2", + "index": "0x7" + }, + "capacity": "0x1fd52bc92e", + "dataHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "status": "live" + }, + { + "blockHash": "0x355cd43b6b517be40ce69278b7c6d2017546e2d46557d59e31adff20646c845e", + "lock": { + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type", + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6" + }, + "outPoint": { + "txHash": "0x9160bef8b6a9e388a99184bfd8bea0e7795b487c77fe784120fb2bc3fb25d1b2", + "index": "0x8" + }, + "capacity": "0x1fd52bc92e", + "dataHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "status": "live" + }, + { + "blockHash": "0x355cd43b6b517be40ce69278b7c6d2017546e2d46557d59e31adff20646c845e", + "lock": { + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type", + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6" + }, + "outPoint": { + "txHash": "0x9160bef8b6a9e388a99184bfd8bea0e7795b487c77fe784120fb2bc3fb25d1b2", + "index": "0x9" + }, + "capacity": "0x1fd52bc92e", + "dataHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "status": "live" + } + ] + ] + ], + "deps": { + "hashType": "type", + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "outPoint": { + "txHash": "0x6347ee7ce5fc6828133590558b867097b895149a66e51cf353c361f7128e2091", + "index": "0x0" + }, + "depType": "depGroup" } }, "expected": { @@ -587,8 +851,13 @@ { "capacity": "0xb67251d5e6", "lock": { - "args": "0xddbd7f09eb480450c1b1ed2c8696248de91c6802", - "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "args": "0x36c329ed630d6ce750712a477543672adab57f4c", + "codeHash": "0xa656f172b6b45c245307aeb5a7a37a176f002f6f22e92582c58bf7ba362e4176", + "hashType": "data" + }, + "type": { + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", + "codeHash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", "hashType": "type" } }, @@ -596,7 +865,7 @@ "capacity": "0x88cb4e12e", "lock": { "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", - "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "codeHash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", "hashType": "type" } } diff --git a/packages/ckb-sdk-core/__tests__/generateRawTransaction/fixtures.json b/packages/ckb-sdk-core/__tests__/generateRawTransaction/fixtures.json index 4c9bfcf8..5c1b826b 100644 --- a/packages/ckb-sdk-core/__tests__/generateRawTransaction/fixtures.json +++ b/packages/ckb-sdk-core/__tests__/generateRawTransaction/fixtures.json @@ -2,8 +2,16 @@ "generateRawTransaction": { "safeMode = true": { "params": { - "fromPublicKeyHash": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", - "toPublicKeyHash": "0xddbd7f09eb480450c1b1ed2c8696248de91c6802", + "inputScript": { + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type" + }, + "outputScript": { + "args": "0xddbd7f09eb480450c1b1ed2c8696248de91c6802", + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type" + }, "capacity": 783602000000, "fee": 13670, "safeMode": true, @@ -165,7 +173,8 @@ "outPoint": { "txHash": "0x6347ee7ce5fc6828133590558b867097b895149a66e51cf353c361f7128e2091", "index": "0x0" - } + }, + "depType": "depGroup" } }, "expected": { @@ -236,7 +245,7 @@ "capacity": "0x88cb4e12e", "lock": { "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", - "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "codeHash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", "hashType": "type" } } @@ -248,8 +257,16 @@ }, "safeMode = true by default": { "params": { - "fromPublicKeyHash": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", - "toPublicKeyHash": "0xddbd7f09eb480450c1b1ed2c8696248de91c6802", + "inputScript": { + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type" + }, + "outputScript": { + "args": "0xddbd7f09eb480450c1b1ed2c8696248de91c6802", + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type" + }, "capacity": 783602000000, "fee": 13670, "cells": [ @@ -410,7 +427,8 @@ "outPoint": { "txHash": "0x6347ee7ce5fc6828133590558b867097b895149a66e51cf353c361f7128e2091", "index": "0x0" - } + }, + "depType": "depGroup" } }, "expected": { @@ -481,7 +499,7 @@ "capacity": "0x88cb4e12e", "lock": { "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", - "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "codeHash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", "hashType": "type" } } @@ -493,8 +511,16 @@ }, "safeMode = false": { "params": { - "fromPublicKeyHash": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", - "toPublicKeyHash": "0xddbd7f09eb480450c1b1ed2c8696248de91c6802", + "inputScript": { + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type" + }, + "outputScript": { + "args": "0xddbd7f09eb480450c1b1ed2c8696248de91c6802", + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type" + }, "capacity": 783602000000, "fee": 13670, "safeMode": false, @@ -656,7 +682,8 @@ "outPoint": { "txHash": "0x6347ee7ce5fc6828133590558b867097b895149a66e51cf353c361f7128e2091", "index": "0x0" - } + }, + "depType": "depGroup" } }, "expected": { @@ -727,7 +754,7 @@ "capacity": "0x88cb4e12e", "lock": { "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", - "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "codeHash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", "hashType": "type" } } @@ -739,8 +766,16 @@ }, "more than one input for change": { "params": { - "fromPublicKeyHash": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", - "toPublicKeyHash": "0xddbd7f09eb480450c1b1ed2c8696248de91c6802", + "inputScript": { + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type" + }, + "outputScript": { + "args": "0xddbd7f09eb480450c1b1ed2c8696248de91c6802", + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type" + }, "capacity": 819602000000, "fee": 13670, "safeMode": true, @@ -902,7 +937,8 @@ "outPoint": { "txHash": "0x6347ee7ce5fc6828133590558b867097b895149a66e51cf353c361f7128e2091", "index": "0x0" - } + }, + "depType": "depGroup" } }, "expected": { @@ -980,7 +1016,7 @@ "capacity": "0x20001c425c", "lock": { "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", - "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "codeHash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", "hashType": "type" } } @@ -990,16 +1026,33 @@ "witnesses": [] } }, - "multi-inputs": { + "multi inputs": { "params": { - "fromPublicKeyHashes": [ - "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", - "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f5" + "inputScripts": [ + { + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type" + }, + { + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f5", + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type" + } ], "changePublicKeyHash": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f0", - "receivePairs": [ + "outputs": [ { - "publicKeyHash": "0xddbd7f09eb480450c1b1ed2c8696248de91c6802", + "lock": { + "args": "0xddbd7f09eb480450c1b1ed2c8696248de91c6802", + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type" + }, + "type": { + "args": "0xddbd7f09eb480450c1b1ed2c8696248de91c6802", + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type" + }, "capacity": 12000000000 } ], @@ -1068,7 +1121,8 @@ "outPoint": { "txHash": "0x6347ee7ce5fc6828133590558b867097b895149a66e51cf353c361f7128e2091", "index": "0x0" - } + }, + "depType": "depGroup" } }, "expected": { @@ -1112,15 +1166,293 @@ "args": "0xddbd7f09eb480450c1b1ed2c8696248de91c6802", "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", "hashType": "type" + }, + "type": { + "args": "0xddbd7f09eb480450c1b1ed2c8696248de91c6802", + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type" } }, { "capacity": "0x17780d860", "lock": { "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f0", + "codeHash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", + "hashType": "type" + } + } + ], + "outputsData": ["0x", "0x"], + "version": "0x0", + "witnesses": [] + } + }, + "multi cell deps": { + "params": { + "inputScript": { + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type" + }, + "outputScript": { + "args": "0xddbd7f09eb480450c1b1ed2c8696248de91c6802", + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type" + }, + "capacity": 783602000000, + "fee": 13670, + "safeMode": true, + "cells": [ + { + "blockHash": "0x355cd43b6b517be40ce69278b7c6d2017546e2d46557d59e31adff20646c845e", + "lock": { + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type", + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6" + }, + "outPoint": { + "txHash": "0x9160bef8b6a9e388a99184bfd8bea0e7795b487c77fe784120fb2bc3fb25d1b2", + "index": "0x0" + }, + "capacity": "0x1fd52bc92e", + "dataHash": "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", + "status": "live" + }, + { + "blockHash": "0x355cd43b6b517be40ce69278b7c6d2017546e2d46557d59e31adff20646c845e", + "lock": { + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type", + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6" + }, + "outPoint": { + "txHash": "0x9160bef8b6a9e388a99184bfd8bea0e7795b487c77fe784120fb2bc3fb25d1b2", + "index": "0x1" + }, + "capacity": "0x1fd52bc92e", + "dataHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "status": "live" + }, + { + "blockHash": "0x355cd43b6b517be40ce69278b7c6d2017546e2d46557d59e31adff20646c845e", + "lock": { + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type", + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6" + }, + "outPoint": { + "txHash": "0x9160bef8b6a9e388a99184bfd8bea0e7795b487c77fe784120fb2bc3fb25d1b2", + "index": "0x2" + }, + "capacity": "0x1fd52bc92e", + "dataHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "status": "live" + }, + { + "blockHash": "0x355cd43b6b517be40ce69278b7c6d2017546e2d46557d59e31adff20646c845e", + "lock": { + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type", + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6" + }, + "outPoint": { + "txHash": "0x9160bef8b6a9e388a99184bfd8bea0e7795b487c77fe784120fb2bc3fb25d1b2", + "index": "0x3" + }, + "capacity": "0x1fd52bc92e", + "dataHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "status": "live" + }, + { + "blockHash": "0x355cd43b6b517be40ce69278b7c6d2017546e2d46557d59e31adff20646c845e", + "lock": { + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type", + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6" + }, + "outPoint": { + "txHash": "0x9160bef8b6a9e388a99184bfd8bea0e7795b487c77fe784120fb2bc3fb25d1b2", + "index": "0x4" + }, + "capacity": "0x1fd52bc92e", + "dataHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "status": "live" + }, + { + "blockHash": "0x355cd43b6b517be40ce69278b7c6d2017546e2d46557d59e31adff20646c845e", + "lock": { + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type", + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6" + }, + "outPoint": { + "txHash": "0x9160bef8b6a9e388a99184bfd8bea0e7795b487c77fe784120fb2bc3fb25d1b2", + "index": "0x5" + }, + "capacity": "0x1fd52bc92e", + "dataHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "status": "live" + }, + { + "blockHash": "0x355cd43b6b517be40ce69278b7c6d2017546e2d46557d59e31adff20646c845e", + "lock": { + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type", + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6" + }, + "outPoint": { + "txHash": "0x9160bef8b6a9e388a99184bfd8bea0e7795b487c77fe784120fb2bc3fb25d1b2", + "index": "0x6" + }, + "capacity": "0x1fd52bc92e", + "dataHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "status": "live" + }, + { + "blockHash": "0x355cd43b6b517be40ce69278b7c6d2017546e2d46557d59e31adff20646c845e", + "lock": { + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type", + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6" + }, + "outPoint": { + "txHash": "0x9160bef8b6a9e388a99184bfd8bea0e7795b487c77fe784120fb2bc3fb25d1b2", + "index": "0x7" + }, + "capacity": "0x1fd52bc92e", + "dataHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "status": "live" + }, + { + "blockHash": "0x355cd43b6b517be40ce69278b7c6d2017546e2d46557d59e31adff20646c845e", + "lock": { + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type", + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6" + }, + "outPoint": { + "txHash": "0x9160bef8b6a9e388a99184bfd8bea0e7795b487c77fe784120fb2bc3fb25d1b2", + "index": "0x8" + }, + "capacity": "0x1fd52bc92e", + "dataHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "status": "live" + }, + { + "blockHash": "0x355cd43b6b517be40ce69278b7c6d2017546e2d46557d59e31adff20646c845e", + "lock": { + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type", + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6" + }, + "outPoint": { + "txHash": "0x9160bef8b6a9e388a99184bfd8bea0e7795b487c77fe784120fb2bc3fb25d1b2", + "index": "0x9" + }, + "capacity": "0x1fd52bc92e", + "dataHash": "0x0000000000000000000000000000000000000000000000000000000000000000", + "status": "live" + } + ], + "deps": [ + { + "hashType": "type", + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "outPoint": { + "txHash": "0x6347ee7ce5fc6828133590558b867097b895149a66e51cf353c361f7128e2091", + "index": "0x0" + }, + "depType": "depGroup" + }, + { + "hashType": "type", + "codeHash": "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", + "outPoint": { + "txHash": "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", + "index": "0x1" + }, + "depType": "code" + } + ] + }, + "expected": { + "cellDeps": [ + { + "depType": "depGroup", + "outPoint": { + "index": "0x0", + "txHash": "0x6347ee7ce5fc6828133590558b867097b895149a66e51cf353c361f7128e2091" + } + }, + { + "depType": "code", + "outPoint": { + "txHash": "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", + "index": "0x1" + } + } + ], + "headerDeps": [], + "inputs": [ + { + "previousOutput": { + "index": "0x1", + "txHash": "0x9160bef8b6a9e388a99184bfd8bea0e7795b487c77fe784120fb2bc3fb25d1b2" + }, + "since": "0x0" + }, + { + "previousOutput": { + "index": "0x2", + "txHash": "0x9160bef8b6a9e388a99184bfd8bea0e7795b487c77fe784120fb2bc3fb25d1b2" + }, + "since": "0x0" + }, + { + "previousOutput": { + "index": "0x3", + "txHash": "0x9160bef8b6a9e388a99184bfd8bea0e7795b487c77fe784120fb2bc3fb25d1b2" + }, + "since": "0x0" + }, + { + "previousOutput": { + "index": "0x4", + "txHash": "0x9160bef8b6a9e388a99184bfd8bea0e7795b487c77fe784120fb2bc3fb25d1b2" + }, + "since": "0x0" + }, + { + "previousOutput": { + "index": "0x5", + "txHash": "0x9160bef8b6a9e388a99184bfd8bea0e7795b487c77fe784120fb2bc3fb25d1b2" + }, + "since": "0x0" + }, + { + "previousOutput": { + "index": "0x6", + "txHash": "0x9160bef8b6a9e388a99184bfd8bea0e7795b487c77fe784120fb2bc3fb25d1b2" + }, + "since": "0x0" + } + ], + "outputs": [ + { + "capacity": "0xb67251a080", + "lock": { + "args": "0xddbd7f09eb480450c1b1ed2c8696248de91c6802", "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", "hashType": "type" } + }, + { + "capacity": "0x88cb4e12e", + "lock": { + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", + "codeHash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", + "hashType": "type" + } } ], "outputsData": ["0x", "0x"], @@ -1130,8 +1462,16 @@ }, "should throw an error when dep is missing": { "params": { - "fromPublicKeyHash": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", - "toPublicKeyHash": "0xddbd7f09eb480450c1b1ed2c8696248de91c6802", + "inputScript": { + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", + "codeHash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", + "hashType": "type" + }, + "outputScript": { + "args": "0xddbd7f09eb480450c1b1ed2c8696248de91c6802", + "codeHash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", + "hashType": "type" + }, "capacity": 783602000000, "fee": 13670, "safeMode": true, @@ -1191,8 +1531,16 @@ "simpleParams": { "params": [ { - "fromPublicKeyHash": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", - "toPublicKeyHash": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", + "inputScript": { + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type" + }, + "outputScript": { + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type" + }, "capacity": "0x111", "cells": [ { @@ -1201,17 +1549,23 @@ "outPoint": { "txHash": "0x", "index": "0x0" } } ] - }, - { - "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", - "hashType": "type" } ], "expected": { - "fromPkhes": ["0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6"], - "toPkhAndCapacityPairs": [ + "inputScripts": [ { - "publicKeyHash": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type" + } + ], + "outputs": [ + { + "lock": { + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type" + }, "capacity": "0x111" } ], @@ -1226,13 +1580,23 @@ "complexParams": { "params": [ { - "receivePairs": [ + "inputScripts": [ { - "publicKeyHash": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type" + } + ], + "outputs": [ + { + "lock": { + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type" + }, "capacity": "0x111" } ], - "fromPublicKeyHashes": ["0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6"], "cells": [ [ "0x0fec94c611533c9588c8ddfed557b9024f4431a65ace4b1e7106388ddd5dd87b", @@ -1244,17 +1608,23 @@ [{ "capacity": "0x222", "dataHash": "0x", "outPoint": { "txHash": "0x", "index": "0x0" } }] ] ] - }, - { - "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", - "hashType": "type" } ], "expected": { - "fromPkhes": ["0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6"], - "toPkhAndCapacityPairs": [ + "inputScripts": [ + { + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type" + } + ], + "outputs": [ { - "publicKeyHash": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", + "lock": { + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type" + }, "capacity": "0x111" } ], @@ -1275,17 +1645,22 @@ "should return outputs": { "params": [ { - "toPkhAndCapacityPairs": [ + "outputs": [ { - "publicKeyHash": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", + "lock": { + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type" + }, + "type": { + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type" + }, "capacity": 6200000000 } ], - "minCapacity": 6100000000, - "scriptBase": { - "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", - "hashType": "type" - } + "minCapacity": 6100000000 } ], "expected": [ @@ -1295,6 +1670,11 @@ "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", "hashType": "type" + }, + "type": { + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type" } } ] @@ -1302,17 +1682,17 @@ "should throw an error when capacity is too small": { "params": [ { - "toPkhAndCapacityPairs": [ + "outputs": [ { - "publicKeyHash": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", + "lock": { + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", + "codeHash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", + "hashType": "type" + }, "capacity": 6000000000 } ], - "minCapacity": 6100000000, - "scriptBase": { - "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", - "hashType": "type" - } + "minCapacity": 6100000000 } ], "exception": "Capacity should be at least 6100000000 shannon" @@ -1322,16 +1702,18 @@ "should pass when capacity is enough": { "params": [ { - "fromPkhes": ["0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6"], - "scriptBase": { - "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", - "hashType": "type" - }, + "inputScripts": [ + { + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", + "codeHash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", + "hashType": "type" + } + ], "safeMode": false, "costCapacity": 12200000000, "unspentCellsMap": [ [ - "0x0fec94c611533c9588c8ddfed557b9024f4431a65ace4b1e7106388ddd5dd87b", + "0x641238cd95b9f5e6224da1963a0bcaa2e972dc87e56eb1ded9a35dcebbc37ff4", [ { "capacity": "0x16b969d00", @@ -1376,16 +1758,18 @@ "non-plain cells should be skipped when safeMode = true": { "params": [ { - "fromPkhes": ["0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6"], - "scriptBase": { - "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", - "hashType": "type" - }, + "inputScripts": [ + { + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", + "codeHash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", + "hashType": "type" + } + ], "safeMode": true, "costCapacity": 12200000000, "unspentCellsMap": [ [ - "0x0fec94c611533c9588c8ddfed557b9024f4431a65ace4b1e7106388ddd5dd87b", + "0x641238cd95b9f5e6224da1963a0bcaa2e972dc87e56eb1ded9a35dcebbc37ff4", [ { "capacity": "0x16b969d00", @@ -1430,16 +1814,18 @@ "should throw an error when capacity is not enough": { "params": [ { - "fromPkhes": ["0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6"], - "scriptBase": { - "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", - "hashType": "type" - }, + "inputScripts": [ + { + "args": "0xe2fa82e70b062c8644b80ad7ecf6e015e5f352f6", + "codeHash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", + "hashType": "type" + } + ], "safeMode": false, "costCapacity": 12200000000, "unspentCellsMap": [ [ - "0x0fec94c611533c9588c8ddfed557b9024f4431a65ace4b1e7106388ddd5dd87b", + "0x641238cd95b9f5e6224da1963a0bcaa2e972dc87e56eb1ded9a35dcebbc37ff4", [{ "capacity": "0x16b969d00", "dataHash": "0x", "outPoint": { "txHash": "0x", "index": "0x0" } }] ] ] diff --git a/packages/ckb-sdk-core/__tests__/generateRawTransaction/index.test.js b/packages/ckb-sdk-core/__tests__/generateRawTransaction/index.test.js index fbd8ee74..8ac54387 100644 --- a/packages/ckb-sdk-core/__tests__/generateRawTransaction/index.test.js +++ b/packages/ckb-sdk-core/__tests__/generateRawTransaction/index.test.js @@ -19,12 +19,12 @@ describe('Test generate raw transaction', () => { expect.assertions(1) try { let fmtParams = params - if ('fromPublicKeyHash' in params) { + if ('inputScript' in params) { fmtParams = { ...params, capacity: BigInt(params.capacity), fee: BigInt(params.fee || 0) } } else { fmtParams = { ...params, - receivePairs: params.receivePairs.map(pair => ({ ...pair, capacity: BigInt(pair.capacity) })), + outputs: params.outputs.map(output => ({ ...output, capacity: BigInt(output.capacity) })), cells: new Map(params.cells), fee: BigInt(params.fee || 0), } diff --git a/packages/ckb-sdk-core/__tests__/index.test.js b/packages/ckb-sdk-core/__tests__/index.test.js index f03616b4..83f9cbb1 100644 --- a/packages/ckb-sdk-core/__tests__/index.test.js +++ b/packages/ckb-sdk-core/__tests__/index.test.js @@ -122,7 +122,7 @@ describe('ckb', () => { }) }) - describe('generate raw transactin', () => { + describe('generate raw transaction', () => { const fixtureTable = Object.entries( fixtures.generateRawTransaction, ).map(([title, { params, expected, exception }]) => [title, params, expected, exception]) @@ -130,11 +130,18 @@ describe('ckb', () => { test.each(fixtureTable)('%s', (_title, params, expected, exception) => { expect.assertions(1) try { - const rawTransaction = ckb.generateRawTransaction({ - ...params, - capacity: BigInt(params.capacity), - fee: BigInt(params.fee || 0), - }) + let fmtParams = params + if ('fromAddress' in params) { + fmtParams = { ...params, capacity: BigInt(params.capacity), fee: BigInt(params.fee || 0) } + } else { + fmtParams = { + ...params, + receivePairs: params.receivePairs.map(pair => ({ ...pair, capacity: BigInt(pair.capacity) })), + cells: new Map(params.cells), + fee: BigInt(params.fee || 0), + } + } + const rawTransaction = ckb.generateRawTransaction(fmtParams) expect(rawTransaction).toEqual(expected) } catch (err) { expect(err).toEqual(new Error(exception)) diff --git a/packages/ckb-sdk-core/package.json b/packages/ckb-sdk-core/package.json index 58d7be19..e9990dfe 100644 --- a/packages/ckb-sdk-core/package.json +++ b/packages/ckb-sdk-core/package.json @@ -1,6 +1,6 @@ { "name": "@nervosnetwork/ckb-sdk-core", - "version": "0.35.0", + "version": "0.36.0", "description": "JavaScript SDK for Nervos Network CKB Project", "author": "Nervos ", "homepage": "https://github.com/nervosnetwork/ckb-sdk-js#readme", @@ -31,9 +31,10 @@ "url": "https://github.com/nervosnetwork/ckb-sdk-js/issues" }, "dependencies": { - "@nervosnetwork/ckb-sdk-rpc": "0.35.0", - "@nervosnetwork/ckb-sdk-utils": "0.35.0", - "@nervosnetwork/ckb-types": "0.35.0" + "@nervosnetwork/ckb-sdk-rpc": "0.36.0", + "@nervosnetwork/ckb-sdk-utils": "0.36.0", + "@nervosnetwork/ckb-types": "0.36.0", + "tslib": "2.0.1" }, - "gitHead": "409da3a2e4a48a927a8ec884ed494b6bafbbfa5b" + "gitHead": "d0adcbb7e6743fdcc049984534afab13f5ede178" } diff --git a/packages/ckb-sdk-core/src/generateRawTransaction.ts b/packages/ckb-sdk-core/src/generateRawTransaction.ts index 1412807f..6fe231c9 100644 --- a/packages/ckb-sdk-core/src/generateRawTransaction.ts +++ b/packages/ckb-sdk-core/src/generateRawTransaction.ts @@ -1,11 +1,9 @@ -import { scriptToHash, JSBI } from '@nervosnetwork/ckb-sdk-utils' +import { scriptToHash, JSBI, systemScripts } from '@nervosnetwork/ckb-sdk-utils' import { assertToBeHexStringOrBigint } from '@nervosnetwork/ckb-sdk-utils/lib/validators' const EMPTY_DATA_HASH = '0x0000000000000000000000000000000000000000000000000000000000000000' const MIN_CELL_CAPACITY = `0x${(61_00_000_000).toString(16)}` -type ScriptBase = Omit - export const getBigInts = ({ fee, capacityThreshold, changeThreshold }: { [key: string]: string | bigint }) => { assertToBeHexStringOrBigint(fee!) assertToBeHexStringOrBigint(capacityThreshold!) @@ -20,63 +18,56 @@ export const getBigInts = ({ fee, capacityThreshold, changeThreshold }: { [key: export const getKeyAndCellsPairs = ( params: - | Pick - | Pick, - scriptBase: ScriptBase, + | Pick + | Pick, ) => { - const fromPkhes = 'fromPublicKeyHash' in params ? [params.fromPublicKeyHash] : params.fromPublicKeyHashes - const toPkhAndCapacityPairs = - 'toPublicKeyHash' in params - ? [{ publicKeyHash: params.toPublicKeyHash, capacity: params.capacity }] - : params.receivePairs + const inputScripts = 'inputScript' in params ? [params.inputScript] : params.inputScripts + const outputs: RawTransactionParams.Output[] = + 'outputScript' in params ? [{ lock: params.outputScript, capacity: params.capacity }] : params.outputs let unspentCellsMap = new Map() - if ('fromPublicKeyHash' in params) { - const lockHash = scriptToHash({ ...scriptBase, args: params.fromPublicKeyHash }) + + if ('inputScript' in params) { + const lockHash = scriptToHash(params.inputScript) unspentCellsMap.set(lockHash, params.cells || []) } else { unspentCellsMap = params.cells || new Map() } - return { fromPkhes, toPkhAndCapacityPairs, unspentCellsMap } + return { inputScripts, outputs, unspentCellsMap } } export const getTargetOutputs = ({ - toPkhAndCapacityPairs, + outputs, minCapacity, - scriptBase, }: { - toPkhAndCapacityPairs: ReturnType['toPkhAndCapacityPairs'] + outputs: ReturnType['outputs'] minCapacity: JSBI - scriptBase: ScriptBase }) => { - return toPkhAndCapacityPairs.map(pkhAndCapacity => { - const capacity = JSBI.BigInt(`${pkhAndCapacity.capacity}`) + return outputs.map(output => { + const capacity = JSBI.BigInt(`${output.capacity}`) if (JSBI.lessThan(capacity, minCapacity)) { throw new Error(`Capacity should be at least ${minCapacity} shannon`) } - return { capacity, lock: { ...scriptBase, args: pkhAndCapacity.publicKeyHash } } + return { ...output, capacity } }) } export const getInputs = ({ - fromPkhes, - scriptBase, + inputScripts, safeMode, costCapacity, unspentCellsMap, }: { - fromPkhes: ReturnType['fromPkhes'] + inputScripts: CKBComponents.Script[] unspentCellsMap: ReturnType['unspentCellsMap'] - scriptBase: ScriptBase safeMode: boolean costCapacity: JSBI }) => { const inputs: CKBComponents.CellInput[] = [] let sum = JSBI.BigInt(0) - for (let i = 0; i < fromPkhes.length; i++) { - const pkh = fromPkhes[i] - const lockhash = scriptToHash({ ...scriptBase, args: pkh }) + for (let i = 0; i < inputScripts.length; i++) { + const lockhash = scriptToHash(inputScripts[i]) const unspentCells = unspentCellsMap.get(lockhash) || [] for (let j = 0; j < unspentCells.length; j++) { @@ -115,14 +106,21 @@ const generateRawTransaction = ({ throw new Error('The dep is not loaded') } - const scriptBase: Omit = { codeHash: deps.codeHash, hashType: deps.hashType } const { targetFee, minCapacity, minChange, zeroBigInt } = getBigInts({ fee, capacityThreshold, changeThreshold }) - const { fromPkhes, toPkhAndCapacityPairs, unspentCellsMap } = getKeyAndCellsPairs(params, scriptBase) - const targetOutputs = getTargetOutputs({ toPkhAndCapacityPairs, minCapacity, scriptBase }) + const { inputScripts, outputs: toOutputs, unspentCellsMap } = getKeyAndCellsPairs(params) + const targetOutputs = getTargetOutputs({ outputs: toOutputs, minCapacity }) const targetCapacity = targetOutputs.reduce((acc, o) => JSBI.add(acc, o.capacity), zeroBigInt) const costCapacity = JSBI.add(JSBI.add(targetCapacity, targetFee), minChange) - const changeOutput = { capacity: zeroBigInt, lock: { ...scriptBase, args: changePublicKeyHash || fromPkhes[0] } } - const { inputs, sum: inputSum } = getInputs({ fromPkhes, scriptBase, safeMode, costCapacity, unspentCellsMap }) + const changeOutput = { + capacity: zeroBigInt, + lock: { + codeHash: systemScripts.SECP256K1_BLAKE160.codeHash, + hashType: systemScripts.SECP256K1_BLAKE160.hashType, + args: changePublicKeyHash || inputScripts[0].args, + }, + } + + const { inputs, sum: inputSum } = getInputs({ inputScripts, safeMode, costCapacity, unspentCellsMap }) if (JSBI.greaterThan(inputSum, JSBI.add(targetCapacity, targetFee))) { changeOutput.capacity = JSBI.subtract(JSBI.subtract(inputSum, targetCapacity), targetFee) @@ -133,11 +131,13 @@ const generateRawTransaction = ({ if (JSBI.greaterThan(changeOutput.capacity, zeroBigInt)) { outputs.push({ ...changeOutput, capacity: `0x${changeOutput.capacity.toString(16)}` }) } + + const cellDeps = Array.isArray(deps) ? deps : [deps] const outputsData = outputs.map(() => '0x') const tx = { version: '0x0', - cellDeps: [{ outPoint: deps.outPoint, depType: 'depGroup' as CKBComponents.DepType }], + cellDeps: cellDeps.map(dep => ({ outPoint: dep.outPoint, depType: dep.depType })), headerDeps: [], inputs, outputs, diff --git a/packages/ckb-sdk-core/src/index.ts b/packages/ckb-sdk-core/src/index.ts index 1da5718a..9a1ca6d1 100644 --- a/packages/ckb-sdk-core/src/index.ts +++ b/packages/ckb-sdk-core/src/index.ts @@ -26,7 +26,7 @@ interface RawTransactionParams extends RawTransactionParams.Base { interface ComplexRawTransactoinParams extends RawTransactionParams.Base { fromAddresses: Address[] - receivePairs: { address: Address; capacity: Capacity }[] + receivePairs: { address: Address; capacity: Capacity; type?: CKBComponents.Script | null }[] cells: Map } @@ -149,22 +149,15 @@ class CKB { ...params }: RawTransactionParams | ComplexRawTransactoinParams) => { if (this.#isSimpleTransaction(params)) { - const [fromPublicKeyHash, toPublicKeyHash] = [params.fromAddress, params.toAddress].map( - this.#extractPayloadFromAddress, - ) + const [inputScript, outputScript] = [params.fromAddress, params.toAddress].map(this.utils.addressToScript) let availableCells = params.cells || [] - if (!availableCells.length && deps) { - const lockHash = this.utils.scriptToHash({ - codeHash: deps.codeHash, - hashType: deps.hashType, - args: toPublicKeyHash, - }) - availableCells = this.cells.get(lockHash) ?? availableCells + if (!availableCells.length) { + availableCells = this.cells.get(this.utils.scriptToHash(inputScript)) ?? availableCells } return generateRawTransaction({ - fromPublicKeyHash, - toPublicKeyHash, + inputScript, + outputScript, capacity: params.capacity, fee, safeMode, @@ -176,14 +169,16 @@ class CKB { } if (this.#isComplexTransaction(params)) { - const fromPublicKeyHashes = params.fromAddresses.map(this.#extractPayloadFromAddress) - const receivePairs = params.receivePairs.map(pair => ({ - publicKeyHash: this.#extractPayloadFromAddress(pair.address), + const inputScripts = params.fromAddresses.map(this.utils.addressToScript) + const outputs = params.receivePairs.map(pair => ({ + lock: this.utils.addressToScript(pair.address), capacity: pair.capacity, + type: pair.type, })) + return generateRawTransaction({ - fromPublicKeyHashes, - receivePairs, + inputScripts, + outputs, cells: params.cells || this.cells, fee, safeMode, @@ -369,12 +364,6 @@ class CKB { } } - #extractPayloadFromAddress = (address: string) => { - const HRP_SIZE = 6 - const addressPayload = this.utils.parseAddress(address, 'hex') - return `0x${addressPayload.slice(HRP_SIZE)}` - } - #secp256k1DepsShouldBeReady = () => { if (!this.config.secp256k1Dep) { throw new ParameterRequiredException('Secp256k1 dep') @@ -415,6 +404,7 @@ class CKB { txHash: secp256k1DepTxHash, index: '0x0', }, + depType: 'depGroup', } } @@ -437,6 +427,7 @@ class CKB { txHash: daoDepTxHash, index: '0x2', }, + depType: 'depGroup', } } } diff --git a/packages/ckb-sdk-core/types/global.d.ts b/packages/ckb-sdk-core/types/global.d.ts index 600d6950..b5205e63 100644 --- a/packages/ckb-sdk-core/types/global.d.ts +++ b/packages/ckb-sdk-core/types/global.d.ts @@ -3,6 +3,7 @@ interface DepCellInfo { codeHash: CKBComponents.Hash256 typeHash?: CKBComponents.Hash256 outPoint: CKBComponents.OutPoint + depType: CKBComponents.DepType } interface CachedCell extends CKBComponents.CellIncludingOutPoint { @@ -41,22 +42,26 @@ declare namespace RawTransactionParams { interface Base { fee?: Capacity safeMode: boolean - deps: DepCellInfo + deps: DepCellInfo | DepCellInfo[] capacityThreshold?: Capacity changeThreshold?: Capacity changePublicKeyHash?: PublicKeyHash } interface Simple extends Base { - fromPublicKeyHash: PublicKeyHash - toPublicKeyHash: PublicKeyHash + inputScript: CKBComponents.Script + outputScript: CKBComponents.Script capacity: Capacity cells?: Cell[] } + interface Output extends CKBComponents.CellOutput { + capacity: string | bigint + } + interface Complex extends Base { - fromPublicKeyHashes: PublicKeyHash[] - receivePairs: { publicKeyHash: PublicKeyHash; capacity: Capacity }[] + inputScripts: CKBComponents.Script[] + outputs: Output[] cells?: Map } } diff --git a/packages/ckb-sdk-rpc/CHANGELOG.md b/packages/ckb-sdk-rpc/CHANGELOG.md index c101925c..fa234a7f 100644 --- a/packages/ckb-sdk-rpc/CHANGELOG.md +++ b/packages/ckb-sdk-rpc/CHANGELOG.md @@ -3,6 +3,14 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +# [0.36.0](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.35.0...v0.36.0) (2020-09-21) + +**Note:** Version bump only for package @nervosnetwork/ckb-sdk-rpc + + + + + # [0.35.0](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.34.0...v0.35.0) (2020-08-24) diff --git a/packages/ckb-sdk-rpc/package.json b/packages/ckb-sdk-rpc/package.json index 8bfa496f..1587abf6 100644 --- a/packages/ckb-sdk-rpc/package.json +++ b/packages/ckb-sdk-rpc/package.json @@ -1,6 +1,6 @@ { "name": "@nervosnetwork/ckb-sdk-rpc", - "version": "0.35.0", + "version": "0.36.0", "description": "RPC module of @nervosnetwork/ckb-sdk-core", "author": "Nervos ", "homepage": "https://github.com/nervosnetwork/ckb-sdk-js/packages/ckb-rpc#readme", @@ -33,11 +33,12 @@ "url": "https://github.com/nervosnetwork/ckb-sdk-js/issues" }, "dependencies": { - "@nervosnetwork/ckb-sdk-utils": "0.35.0", - "axios": "0.19.2" + "@nervosnetwork/ckb-sdk-utils": "0.36.0", + "axios": "0.19.2", + "tslib": "2.0.1" }, "devDependencies": { - "@nervosnetwork/ckb-types": "0.35.0" + "@nervosnetwork/ckb-types": "0.36.0" }, - "gitHead": "409da3a2e4a48a927a8ec884ed494b6bafbbfa5b" + "gitHead": "d0adcbb7e6743fdcc049984534afab13f5ede178" } diff --git a/packages/ckb-sdk-rpc/src/Base/index.ts b/packages/ckb-sdk-rpc/src/Base/index.ts index 82a030cd..6397a78a 100644 --- a/packages/ckb-sdk-rpc/src/Base/index.ts +++ b/packages/ckb-sdk-rpc/src/Base/index.ts @@ -93,6 +93,7 @@ export interface Base { /** * @method getCellsByLockHash * @memberof DefaultRPC + * @deprecated since v0.36.0 * @description rpc to get the collection of cells who have the corresponding lockhash, * the meaning of lockhash could be found in ckb-types * @param {string} hash - hash of cell's lock script @@ -189,6 +190,7 @@ export interface Base { /** * @method indexLockHash * @memberof DefaultRPC + * @deprecated since v0.36.0 * @description create index for live cells and transactions by the hash of lock script * @param {string} lockHash, the hash of lock script * @param {string} [indexFrom], the starting block number(exclusive), an optional parameter, @@ -202,6 +204,7 @@ export interface Base { /** * @method getLockHashIndexStates * @memberof DefaultRPC + * @deprecated since v0.36.0 * @description get lock hash index states * @retrun {Promise} */ @@ -210,6 +213,7 @@ export interface Base { /** * @method getLiveCellsByLockHash * @memberof DefaultRPC + * @deprecated since v0.36.0 * @description return the live cells collection by the hash of lock script * @param {string} lockHash, the hash of lock script * @param {string} pageNumber @@ -228,6 +232,7 @@ export interface Base { /** * @method getTransactionsByLockHash * @memberof DefaultRPC + * @deprecated since v0.36.0 * @description retrun the transactions collection by the hash of lock script. * return empty array when the `lock_hash` not indexed yet. * @param {string} lockHash, the hash of lock script @@ -243,11 +248,15 @@ export interface Base { reverseOrder?: boolean, ) => Promise + /** + * @deprecated since v0.36.0 + */ getCapacityByLockHash: (lockHash: CKBComponents.Hash) => Promise /** * @method deindexLockHash * @memberof DefaultRPC + * @deprecated since v0.36.0 * @description remove index for live cells and transaction by the hash of lock script, * returns empty array when the `lock_hash` not indexed yet. * @param {string} lockHash diff --git a/packages/ckb-sdk-utils/CHANGELOG.md b/packages/ckb-sdk-utils/CHANGELOG.md index 1e1c9c2c..c0b33447 100644 --- a/packages/ckb-sdk-utils/CHANGELOG.md +++ b/packages/ckb-sdk-utils/CHANGELOG.md @@ -3,6 +3,28 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +# [0.36.0](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.35.0...v0.36.0) (2020-09-21) + + +### Code Refactoring + +* **utils:** remove redundant default options in the address module ([d74caac](https://github.com/nervosnetwork/ckb-sdk-js/commit/d74caac7121c8d1fc81ca1e8479200a9897c1726)) + + +### Features + +* **utils:** add system scripts ([567f15a](https://github.com/nervosnetwork/ckb-sdk-js/commit/567f15aea993ce5797bcecaadeeae27c3baf4aed)) +* **utils:** add the utils#addressToScript ([a78340a](https://github.com/nervosnetwork/ckb-sdk-js/commit/a78340a496f37f0239f5115ef886e2dbd1a9974e)) + + +### BREAKING CHANGES + +* **utils:** Remove the utils#defaultAddressOptions + + + + + # [0.35.0](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.34.0...v0.35.0) (2020-08-24) diff --git a/packages/ckb-sdk-utils/__tests__/address/fixtures.json b/packages/ckb-sdk-utils/__tests__/address/fixtures.json index c1641555..c210d7ee 100644 --- a/packages/ckb-sdk-utils/__tests__/address/fixtures.json +++ b/packages/ckb-sdk-utils/__tests__/address/fixtures.json @@ -47,11 +47,11 @@ "params": ["0x36c329ed630d6ce750712a477543672adab57f4c", { "prefix": "ckb" }], "expected": "ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd" }, - "default prefix = ckb": { + "default prefix = ckb, type = 0x01 and codeHashOrCodeHashIndex = 0x00": { "params": ["0x36c329ed630d6ce750712a477543672adab57f4c", {}], "expected": "ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd" }, - "default option = { prefix: ckb, type: 0x01, codeHashIndex: 0x00 }": { + "default options = { prefix: ckb, type: 0x01, codeHashOrCodeHashIndex: 0x00 }": { "params": ["0x36c329ed630d6ce750712a477543672adab57f4c"], "expected": "ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd" }, @@ -70,11 +70,11 @@ ], "expected": "ckt1qyqrdsefa43s6m882pcj53m4gdnj4k440axqswmu83" }, - "default prefix = ckb": { + "default prefix = ckb, hashType = 0x01, and codeHashOrCodeHashIndex = 0x00": { "params": ["0x024a501efd328e062c8675f2365970728c859c592beeefd6be8ead3d901330bc01", {}], "expected": "ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd" }, - "default option = { prefix: ckt }": { + "default options = { prefix: ckt, hashType: 0x01, codeHashOrCodeHashIndex: 0x00 }": { "params": ["0x024a501efd328e062c8675f2365970728c859c592beeefd6be8ead3d901330bc01"], "expected": "ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd" } @@ -136,5 +136,47 @@ "params": ["ckb1qsqcjt4ypkpt20r83lugxyj9pwa30cty6737p2gfgx493qul2cgvrxhw"], "exception": "ckb1qsqcjt4ypkpt20r83lugxyj9pwa30cty6737p2gfgx493qul2cgvrxhw is not a valid full version address" } + }, + "addressToScript": { + "secp256k1/blake160 address": { + "params": ["ckb1qyqt8xaupvm8837nv3gtc9x0ekkj64vud3jqfwyw5v"], + "expected": { + "codeHash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", + "hashType": "type", + "args": "0xb39bbc0b3673c7d36450bc14cfcdad2d559c6c64" + } + }, + "secp256k1/multisig address": { + "params": ["ckb1qyq5lv479ewscx3ms620sv34pgeuz6zagaaqklhtgg"], + "expected": { + "codeHash": "0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8", + "hashType": "type", + "args": "0x4fb2be2e5d0c1a3b8694f832350a33c1685d477a" + } + }, + "anyone can pay address": { + "params": ["ckt1qjr2r35c0f9vhcdgslx2fjwa9tylevr5qka7mfgmscd33wlhfykyhqf39tsxa6c9qjmn0e4ulffe00344y5due3gayw"], + "expected": { + "codeHash": "0x86a1c6987a4acbe1a887cca4c9dd2ac9fcb07405bbeda51b861b18bbf7492c4b", + "hashType": "type", + "args": "0x81312ae06eeb0504b737e6bcfa5397be35a928de" + } + }, + "full version address of hashType = 'data'": { + "params": ["ckt1q2n9dutjk669cfznq7httfar0gtk7qp0du3wjfvzck9l0w3k9eqhvdkr98kkxrtvuag8z2j8w4pkw2k6k4l5czshhac"], + "expected": { + "codeHash": "0xa656f172b6b45c245307aeb5a7a37a176f002f6f22e92582c58bf7ba362e4176", + "hashType": "data", + "args": "0x36c329ed630d6ce750712a477543672adab57f4c" + } + }, + "full version address of hashType = 'type'": { + "params": ["ckt1qsvf96jqmq4483ncl7yrzfzshwchu9jd0glq4yy5r2jcsw04d7xlydkr98kkxrtvuag8z2j8w4pkw2k6k4l5c02auef"], + "expected": { + "codeHash": "0x1892ea40d82b53c678ff88312450bbb17e164d7a3e0a90941aa58839f56f8df2", + "hashType": "type", + "args": "0x36c329ed630d6ce750712a477543672adab57f4c" + } + } } } diff --git a/packages/ckb-sdk-utils/__tests__/address/index.test.js b/packages/ckb-sdk-utils/__tests__/address/index.test.js index b4d80799..f1adcbd2 100644 --- a/packages/ckb-sdk-utils/__tests__/address/index.test.js +++ b/packages/ckb-sdk-utils/__tests__/address/index.test.js @@ -1,7 +1,14 @@ const ckbUtils = require('../..') const fixtures = require('./fixtures.json') -const { toAddressPayload, bech32Address, pubkeyToAddress, parseAddress, fullPayloadToAddress } = ckbUtils +const { + toAddressPayload, + bech32Address, + pubkeyToAddress, + parseAddress, + fullPayloadToAddress, + addressToScript, +} = ckbUtils describe('Test address module', () => { describe('toAddressPayload', () => { @@ -93,4 +100,24 @@ describe('Test address module', () => { } }) }) + + describe('addressToScript', () => { + const fixtureTable = Object.entries(fixtures.addressToScript).map(([title, { params, expected, exception }]) => [ + title, + params, + expected, + exception, + ]) + + test.each(fixtureTable)(`%s`, (_title, params, expected, exception) => { + expect.assertions(1) + + try { + const actual = addressToScript(...params) + expect(actual).toEqual(expected) + } catch (err) { + expect(err).toEqual(new Error(exception)) + } + }) + }) }) diff --git a/packages/ckb-sdk-utils/__tests__/systemScripts/fixtures.json b/packages/ckb-sdk-utils/__tests__/systemScripts/fixtures.json new file mode 100644 index 00000000..9552637b --- /dev/null +++ b/packages/ckb-sdk-utils/__tests__/systemScripts/fixtures.json @@ -0,0 +1,68 @@ +{ + "SECP256K1_BLAKE160": { + "mainnet": { + "codeHash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", + "hashType": "type", + "txHash": "0x71a7ba8fc96349fea0ed3a5c47992e3b4084b031a42264a018e0072e8172e46c", + "index": "0x0", + "depType": "depGroup" + }, + "testnet": { + "codeHash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", + "hashType": "type", + "txHash": "0xf8de3bb47d055cdf460d93a2a6e1b05f7432f9777c8c474abf4eec1d4aee5d37", + "index": "0x0", + "depType": "depGroup" + } + }, + "SECP256K1_MULTISIG": { + "mainnet": { + "codeHash": "0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8", + "hashType": "type", + "txHash": "0x71a7ba8fc96349fea0ed3a5c47992e3b4084b031a42264a018e0072e8172e46c", + "index": "0x1", + "depType": "depGroup" + }, + "testnet": { + "codeHash": "0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8", + "hashType": "type", + "txHash": "0xf8de3bb47d055cdf460d93a2a6e1b05f7432f9777c8c474abf4eec1d4aee5d37", + "index": "0x1", + "depType": "depGroup" + } + }, + "ANYONE_CAN_PAY": { + "testnet": { + "codeHash": "0x86a1c6987a4acbe1a887cca4c9dd2ac9fcb07405bbeda51b861b18bbf7492c4b", + "hashType": "type", + "txHash": "0x4f32b3e39bd1b6350d326fdfafdfe05e5221865c3098ae323096f0bfc69e0a8c", + "index": "0x0", + "depType": "depGroup" + } + }, + "NERVOS_DAO": { + "mainnet": { + "codeHash": "0x82d76d1b75fe2fd9a27dfbaa65a039221a380d76c926f378d3f81cf3e7e13f2e", + "hashType": "type", + "txHash": "0xe2fb199810d49a4d8beec56718ba2593b665db9d52299a0f9e6e75416d73ff5c", + "index": "0x2", + "depType": "code" + }, + "testnet": { + "codeHash": "0x82d76d1b75fe2fd9a27dfbaa65a039221a380d76c926f378d3f81cf3e7e13f2e", + "hashType": "type", + "txHash": "0x8f8c79eb6671709633fe6a46de93c0fedc9c1b8a6527a18d3983879542635c9f", + "index": "0x2", + "depType": "code" + } + }, + "SIMPLE_UDT": { + "testnet": { + "codeHash": "0x48dbf59b4c7ee1547238021b4869bceedf4eea6b43772e5d66ef8865b6ae7212", + "hashType": "data", + "txHash": "0xc1b2ae129fad7465aaa9acc9785f842ba3e6e8b8051d899defa89f5508a77958", + "index": "0x0", + "depType": "code" + } + } +} diff --git a/packages/ckb-sdk-utils/__tests__/systemScripts/index.test.js b/packages/ckb-sdk-utils/__tests__/systemScripts/index.test.js new file mode 100644 index 00000000..988bea2c --- /dev/null +++ b/packages/ckb-sdk-utils/__tests__/systemScripts/index.test.js @@ -0,0 +1,60 @@ +const { systemScripts } = require('../..') +const fixtures = require('./fixtures.json') + +const getInfo = ({ codeHash, hashType, depType, mainnetOutPoint, testnetOutPoint }, isMain) => { + const outPoint = isMain ? mainnetOutPoint : testnetOutPoint + + return { codeHash, hashType, depType, ...outPoint } +} + +describe('Test System Scripts', () => { + describe('Test secp256k1/blake160', () => { + it('should has mainnet script', () => { + expect(getInfo(systemScripts.SECP256K1_BLAKE160, true)).toEqual(fixtures.SECP256K1_BLAKE160.mainnet) + }) + + it('should has testnet script', () => { + expect(getInfo(systemScripts.SECP256K1_BLAKE160, false)).toEqual(fixtures.SECP256K1_BLAKE160.testnet) + }) + }) + + describe('Test secp256k1/multisig', () => { + it('should has mainnet script', () => { + expect(getInfo(systemScripts.SECP256K1_MULTISIG, true)).toEqual(fixtures.SECP256K1_MULTISIG.mainnet) + }) + + it('should has testnet script', () => { + expect(getInfo(systemScripts.SECP256K1_MULTISIG, false)).toEqual(fixtures.SECP256K1_MULTISIG.testnet) + }) + }) + + describe('Test anyone can pay', () => { + it("shouldn't has mainnet script", () => { + expect(systemScripts.ANYONE_CAN_PAY.mainnet).toBeUndefined() + }) + + it('should has testnet script', () => { + expect(getInfo(systemScripts.ANYONE_CAN_PAY, false)).toEqual(fixtures.ANYONE_CAN_PAY.testnet) + }) + }) + + describe('Test nervos dao', () => { + it('should has mainnet script', () => { + expect(getInfo(systemScripts.NERVOS_DAO, true)).toEqual(fixtures.NERVOS_DAO.mainnet) + }) + + it('should has testnet script', () => { + expect(getInfo(systemScripts.NERVOS_DAO, false)).toEqual(fixtures.NERVOS_DAO.testnet) + }) + }) + + describe('Test simple udt', () => { + it("shouldn't has mainnet script", () => { + expect(systemScripts.SIMPLE_UDT.mainnet).toBeUndefined() + }) + + it('should has testnet script', () => { + expect(getInfo(systemScripts.SIMPLE_UDT, false)).toEqual(fixtures.SIMPLE_UDT.testnet) + }) + }) +}) diff --git a/packages/ckb-sdk-utils/package.json b/packages/ckb-sdk-utils/package.json index cfd7ec76..a585712e 100644 --- a/packages/ckb-sdk-utils/package.json +++ b/packages/ckb-sdk-utils/package.json @@ -1,6 +1,6 @@ { "name": "@nervosnetwork/ckb-sdk-utils", - "version": "0.35.0", + "version": "0.36.0", "description": "Utils module of @nervosnetwork/ckb-sdk-core", "author": "Nervos ", "homepage": "https://github.com/nervosnetwork/ckb-sdk-js#readme", @@ -31,14 +31,15 @@ "url": "https://github.com/nervosnetwork/ckb-sdk-js/issues" }, "dependencies": { - "@nervosnetwork/ckb-types": "0.35.0", + "@nervosnetwork/ckb-types": "0.36.0", "blake2b-wasm": "2.1.0", "elliptic": "6.5.3", - "jsbi": "3.1.3" + "jsbi": "3.1.3", + "tslib": "2.0.1" }, "devDependencies": { "@types/bitcoinjs-lib": "5.0.0", "@types/elliptic": "6.4.12" }, - "gitHead": "409da3a2e4a48a927a8ec884ed494b6bafbbfa5b" + "gitHead": "d0adcbb7e6743fdcc049984534afab13f5ede178" } diff --git a/packages/ckb-sdk-utils/src/address/index.ts b/packages/ckb-sdk-utils/src/address/index.ts index d859255e..f0ee062a 100644 --- a/packages/ckb-sdk-utils/src/address/index.ts +++ b/packages/ckb-sdk-utils/src/address/index.ts @@ -1,4 +1,5 @@ import { bech32, blake160 } from '..' +import { SECP256K1_BLAKE160, SECP256K1_MULTISIG, ANYONE_CAN_PAY } from '../systemScripts' import { hexToBytes, bytesToHex } from '../convertors' import { HexStringWithout0xException, AddressException, AddressPayloadException } from '../exceptions' @@ -17,15 +18,9 @@ export enum AddressType { export type CodeHashIndex = '0x00' | '0x01' | '0x02' export interface AddressOptions { - prefix: AddressPrefix - type: AddressType - codeHashOrCodeHashIndex: CodeHashIndex | CKBComponents.Hash256 -} - -export const defaultAddressOptions = { - prefix: AddressPrefix.Mainnet, - type: AddressType.HashIdx, - codeHashOrCodeHashIndex: '0x00', + prefix?: AddressPrefix + type?: AddressType + codeHashOrCodeHashIndex?: CodeHashIndex | CKBComponents.Hash256 } /** @@ -54,59 +49,40 @@ export const toAddressPayload = ( * @name bech32Address * @description generate the address by bech32 algorithm * @param args, used as the identifier of an address, usually the public key hash is used. - * @param {string} prefix, the prefix precedes the address. - * @param {string} type, used to indicate which format is adopted to compose the address. - * @param {string} codeHashOrCodeHashIndex, the referenced code hash or code hash index the address binds to. + * @param {[string]} prefix, the prefix precedes the address, default to be ckb. + * @param {[string]} type, used to indicate which format is adopted to compose the address, default to be 0x01. + * @param {[string]} codeHashOrCodeHashIndex, the referenced code hash or code hash index the address binds to, + * default to be 0x00. */ export const bech32Address = ( args: Uint8Array | string, - { - prefix = AddressPrefix.Mainnet, - type = AddressType.HashIdx, - codeHashOrCodeHashIndex = '0x00', - }: AddressOptions = defaultAddressOptions, + { prefix = AddressPrefix.Mainnet, type = AddressType.HashIdx, codeHashOrCodeHashIndex = '0x00' }: AddressOptions = {}, ) => bech32.encode(prefix, bech32.toWords(toAddressPayload(args, type, codeHashOrCodeHashIndex))) /** * @name fullPayloadToAddress * @description generate the address with payload in full version format. * @param {string} args, used as the identifier of an address. - * @param {string} prefix, the prefix precedes the address. - * @param {string} type, used to indicate which format the address conforms to, + * @param {[string]} prefix, the prefix precedes the address, default to be ckb. + * @param {[string]} type, used to indicate which format the address conforms to, default to be 0x02, * with hash type of Data or with hash type of Type. * @param {string} codeHash, the code hash used in the full version payload. */ export const fullPayloadToAddress = ({ args, - prefix = AddressPrefix.Mainnet, + prefix, type = AddressType.DataCodeHash, codeHash, }: { args: string - prefix: AddressPrefix - type: AddressType.DataCodeHash | AddressType.TypeCodeHash + prefix?: AddressPrefix + type?: AddressType.DataCodeHash | AddressType.TypeCodeHash codeHash: CKBComponents.Hash256 -}) => - bech32Address(args, { - prefix, - type, - codeHashOrCodeHashIndex: codeHash, - }) +}) => bech32Address(args, { prefix, type, codeHashOrCodeHashIndex: codeHash }) -export const pubkeyToAddress = ( - pubkey: Uint8Array | string, - { - prefix = AddressPrefix.Mainnet, - type = AddressType.HashIdx, - codeHashOrCodeHashIndex = '0x00' as CodeHashIndex, - }: AddressOptions = defaultAddressOptions, -) => { +export const pubkeyToAddress = (pubkey: Uint8Array | string, options: AddressOptions = {}) => { const publicKeyHash = blake160(pubkey) - return bech32Address(publicKeyHash, { - prefix, - type, - codeHashOrCodeHashIndex, - }) + return bech32Address(publicKeyHash, options) } const isValidShortVersionPayload = (payload: Uint8Array) => { @@ -177,3 +153,28 @@ export const parseAddress: ParseAddress = (address: string, encode: 'binary' | ' } return encode === 'binary' ? payload : bytesToHex(payload) } + +export const addressToScript = (address: string): CKBComponents.Script => { + const payload = parseAddress(address) + const type = payload[0] + + if (type === +AddressType.HashIdx) { + const codeHashIndices = [SECP256K1_BLAKE160, SECP256K1_MULTISIG, ANYONE_CAN_PAY] + const index = payload[1] + const args = payload.slice(2) + const script = codeHashIndices[index] + return { + codeHash: script.codeHash, + hashType: script.hashType, + args: bytesToHex(args), + } + } + + const codeHashAndArgs = bytesToHex(payload.slice(1)) + const hashType = type === +AddressType.DataCodeHash ? 'data' : 'type' + return { + codeHash: codeHashAndArgs.substr(0, 66), + hashType, + args: `0x${codeHashAndArgs.substr(66)}`, + } +} diff --git a/packages/ckb-sdk-utils/src/index.ts b/packages/ckb-sdk-utils/src/index.ts index f1731864..1e382f7a 100644 --- a/packages/ckb-sdk-utils/src/index.ts +++ b/packages/ckb-sdk-utils/src/index.ts @@ -14,6 +14,7 @@ export * from './serialization' export * from './convertors' export * from './epochs' export * from './sizes' +export * as systemScripts from './systemScripts' export { serializeScript, serializeRawTransaction, serializeTransaction, serializeWitnessArgs, JSBI, PERSONAL } export const { blake2b, bech32, blake160 } = crypto diff --git a/packages/ckb-sdk-utils/src/systemScripts.ts b/packages/ckb-sdk-utils/src/systemScripts.ts new file mode 100644 index 00000000..dbd81185 --- /dev/null +++ b/packages/ckb-sdk-utils/src/systemScripts.ts @@ -0,0 +1,106 @@ +/** + * @summary System scripts are the smart contracts built and deployed by the CKB core team. + * System scripts complement the function of CKB in a flexible way. + * System scripts can provide + * - core functions (e.g. secp256k1/blake160 and Nervos DAO), + * - shared standard implementations (e.g. Simple UDT), + * - or other auxiliary infrastructure components. + * @see https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0024-ckb-system-script-list/0024-ckb-system-script-list.md + */ +interface SystemScript extends Omit { + depType: CKBComponents.DepType +} + +type OutPoints = Record<'mainnetOutPoint' | 'testnetOutPoint', CKBComponents.OutPoint> + +/** + * @memberof System Scripts + * @typedef {Lock Script} + * @name SECP256K1_BLAKE160 + * @description SECP256K1_BLAKE160 is the default lock script to verify CKB transaction signature + */ +export const SECP256K1_BLAKE160: SystemScript & OutPoints = { + codeHash: '0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8', + hashType: 'type', + depType: 'depGroup', + mainnetOutPoint: { + txHash: '0x71a7ba8fc96349fea0ed3a5c47992e3b4084b031a42264a018e0072e8172e46c', + index: '0x0', + }, + testnetOutPoint: { + txHash: '0xf8de3bb47d055cdf460d93a2a6e1b05f7432f9777c8c474abf4eec1d4aee5d37', + index: '0x0', + }, +} + +/** + * @memberof System Scripts + * @typedef {Lock Script} + * @name SECP256K1_MULTISIG + * @description SECP256K1_MULTISIG is a script which allows a group of users to sign a single transaction + */ +export const SECP256K1_MULTISIG: SystemScript & OutPoints = { + codeHash: '0x5c5069eb0857efc65e1bca0c07df34c31663b3622fd3876c876320fc9634e2a8', + hashType: 'type', + depType: 'depGroup', + mainnetOutPoint: { + txHash: '0x71a7ba8fc96349fea0ed3a5c47992e3b4084b031a42264a018e0072e8172e46c', + index: '0x1', + }, + testnetOutPoint: { + txHash: '0xf8de3bb47d055cdf460d93a2a6e1b05f7432f9777c8c474abf4eec1d4aee5d37', + index: '0x1', + }, +} + +/** + * @memberof System Scripts + * @typedef {Lock Script} + * @name ANYONE_CAN_PAY + * @description ANYONE_CAN_PAY allows a recipient to provide cell capacity in asset transfer + */ +export const ANYONE_CAN_PAY: SystemScript & Pick = { + codeHash: '0x86a1c6987a4acbe1a887cca4c9dd2ac9fcb07405bbeda51b861b18bbf7492c4b', + hashType: 'type', + depType: 'depGroup', + testnetOutPoint: { + txHash: '0x4f32b3e39bd1b6350d326fdfafdfe05e5221865c3098ae323096f0bfc69e0a8c', + index: '0x0', + }, +} + +/** + * @memberof System Scripts + * @typedef {Type Script} + * @name NERVOS_DAO + * @description NERVOS_DAO is the script implements Nervos DAO + */ +export const NERVOS_DAO: SystemScript & OutPoints = { + codeHash: '0x82d76d1b75fe2fd9a27dfbaa65a039221a380d76c926f378d3f81cf3e7e13f2e', + hashType: 'type', + depType: 'code', + mainnetOutPoint: { + txHash: '0xe2fb199810d49a4d8beec56718ba2593b665db9d52299a0f9e6e75416d73ff5c', + index: '0x2', + }, + testnetOutPoint: { + txHash: '0x8f8c79eb6671709633fe6a46de93c0fedc9c1b8a6527a18d3983879542635c9f', + index: '0x2', + }, +} + +/** + * @memberof System Scripts + * @typedef {Type Script} + * @name SIMPLE_UDT + * @description SIMPLE_UDT implements the minimum standard for user defined tokens on Nervos CKB + */ +export const SIMPLE_UDT: SystemScript & Pick = { + codeHash: '0x48dbf59b4c7ee1547238021b4869bceedf4eea6b43772e5d66ef8865b6ae7212', + hashType: 'data', + depType: 'code', + testnetOutPoint: { + txHash: '0xc1b2ae129fad7465aaa9acc9785f842ba3e6e8b8051d899defa89f5508a77958', + index: '0x0', + }, +} diff --git a/packages/ckb-types/CHANGELOG.md b/packages/ckb-types/CHANGELOG.md index 8d79688e..9db762da 100644 --- a/packages/ckb-types/CHANGELOG.md +++ b/packages/ckb-types/CHANGELOG.md @@ -3,6 +3,14 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +# [0.36.0](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.35.0...v0.36.0) (2020-09-21) + +**Note:** Version bump only for package @nervosnetwork/ckb-types + + + + + # [0.35.0](https://github.com/nervosnetwork/ckb-sdk-js/compare/v0.34.0...v0.35.0) (2020-08-24) ### Features diff --git a/packages/ckb-types/package.json b/packages/ckb-types/package.json index d1d8d5c4..96ce6b14 100644 --- a/packages/ckb-types/package.json +++ b/packages/ckb-types/package.json @@ -1,6 +1,6 @@ { "name": "@nervosnetwork/ckb-types", - "version": "0.35.0", + "version": "0.36.0", "description": "Type module of @nervosnetwork/ckb-sdk-core", "author": "Nervos ", "homepage": "https://github.com/nervosnetwork/ckb-sdk-js#readme", @@ -23,5 +23,5 @@ "scripts": { "doc": "../../node_modules/.bin/typedoc --out docs ./index.d.ts --mode modules --includeDeclarations --excludeExternals --ignoreCompilerErrors --theme default --readme README.md" }, - "gitHead": "409da3a2e4a48a927a8ec884ed494b6bafbbfa5b" + "gitHead": "d0adcbb7e6743fdcc049984534afab13f5ede178" } diff --git a/tsconfig.json b/tsconfig.json index c4860156..9886a786 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -5,7 +5,8 @@ "lib": ["es6"], "esModuleInterop": true, "moduleResolution": "node", - "sourceMap": true + "sourceMap": true, + "importHelpers": true }, "exclude": ["node_modules", "**/*.spec.ts", "*/**/debug.ts"] } diff --git a/yarn.lock b/yarn.lock index 16714ec7..ea5ec094 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8613,6 +8613,11 @@ tsconfig-paths@^3.9.0: minimist "^1.2.0" strip-bom "^3.0.0" +tslib@2.0.1: + version "2.0.1" + resolved "https://registry.npmjs.org/tslib/-/tslib-2.0.1.tgz#410eb0d113e5b6356490eec749603725b021b43e" + integrity sha512-SgIkNheinmEBgx1IUNirK0TUD4X9yjjBRTqqjggWCU3pUEqIk3/Uwl3yRixYKT6WjQuGiwDv4NomL3wqRCj+CQ== + tslib@^1.8.1, tslib@^1.9.0: version "1.10.0" resolved "https://registry.npmjs.org/tslib/-/tslib-1.10.0.tgz#c3c19f95973fb0a62973fb09d90d961ee43e5c8a"