@ckb-lumos/helpers
Advanced tools
Comparing version 0.0.0-canary-6427eb4-20230810033735 to 0.0.0-canary-66228d6-20240122031015
@@ -9,5 +9,6 @@ "use strict"; | ||
exports.parseFullFormatAddress = parseFullFormatAddress; | ||
var _base = require("@ckb-lumos/base"); | ||
var _codec = require("@ckb-lumos/codec"); | ||
var _configManager = require("@ckb-lumos/config-manager"); | ||
var _bech = require("bech32"); | ||
var _utils = require("./utils"); | ||
// https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0021-ckb-address-format/0021-ckb-address-format.md | ||
@@ -69,11 +70,5 @@ // | format type | description | | ||
} | ||
const codeHash = (0, _utils.byteArrayToHex)(body.slice(0, 32)); | ||
const hashType = (() => { | ||
const serializedHashType = body[32]; | ||
if (serializedHashType === 0) return "data"; | ||
if (serializedHashType === 1) return "type"; | ||
if (serializedHashType === 2) return "data1"; | ||
throw new Error(`Invalid hashType ${serializedHashType}`); | ||
})(); | ||
const args = (0, _utils.byteArrayToHex)(body.slice(33)); | ||
const codeHash = _codec.bytes.hexify(body.slice(0, 32)); | ||
const hashType = _base.blockchain.HashType.unpack(body.slice(32, 33)); | ||
const args = _codec.bytes.hexify(body.slice(33)); | ||
return { | ||
@@ -117,3 +112,3 @@ codeHash, | ||
hashType: scriptTemplate.HASH_TYPE, | ||
args: (0, _utils.byteArrayToHex)(argsBytes) | ||
args: _codec.bytes.hexify(argsBytes) | ||
}; | ||
@@ -128,5 +123,5 @@ } | ||
return { | ||
codeHash: (0, _utils.byteArrayToHex)(body.slice(0, 32)), | ||
codeHash: _codec.bytes.hexify(body.slice(0, 32)), | ||
hashType: "data", | ||
args: (0, _utils.byteArrayToHex)(body.slice(32)) | ||
args: _codec.bytes.hexify(body.slice(32)) | ||
}; | ||
@@ -141,5 +136,5 @@ } | ||
return { | ||
codeHash: (0, _utils.byteArrayToHex)(body.slice(0, 32)), | ||
codeHash: _codec.bytes.hexify(body.slice(0, 32)), | ||
hashType: "type", | ||
args: (0, _utils.byteArrayToHex)(body.slice(32)) | ||
args: _codec.bytes.hexify(body.slice(32)) | ||
}; | ||
@@ -146,0 +141,0 @@ } |
@@ -33,2 +33,3 @@ "use strict"; | ||
var _toolkit = require("@ckb-lumos/toolkit"); | ||
var _blockchain = require("@ckb-lumos/base/lib/blockchain"); | ||
const { | ||
@@ -221,15 +222,7 @@ bytify, | ||
config = config || (0, _configManager.getConfig)(); | ||
const data = []; | ||
const hashType = (() => { | ||
if (script.hashType === "data") return 0; | ||
if (script.hashType === "type") return 1; | ||
if (script.hashType === "data1") return 2; | ||
// https://github.com/nervosnetwork/rfcs/blob/9aef152a5123c8972de1aefc11794cf84d1762ed/rfcs/0021-ckb-address-format/0021-ckb-address-format.md#full-payload-format | ||
// Full payload format directly encodes all data fields of lock script. The encode rule of full payload format is Bech32m. | ||
// payload = 0x00 | code_hash | hash_type | args | ||
/* c8 ignore next */ | ||
throw new Error(`Invalid hashType ${script.hashType}`); | ||
})(); | ||
data.push(0x00); | ||
data.push(...(0, _utils.hexToByteArray)(script.codeHash)); | ||
data.push(hashType); | ||
data.push(...(0, _utils.hexToByteArray)(script.args)); | ||
const data = _codec.bytes.concat([0x00], script.codeHash, _blockchain.HashType.pack(script.hashType), script.args); | ||
return _bech.bech32m.encode(config.PREFIX, _bech.bech32m.toWords(data), BECH32_LIMIT); | ||
@@ -236,0 +229,0 @@ } |
import { HexString } from "@ckb-lumos/base"; | ||
export declare function hexToByteArray(h: HexString): number[]; | ||
export declare function byteArrayToHex(a: number[]): HexString; | ||
//# sourceMappingURL=utils.d.ts.map |
@@ -6,3 +6,2 @@ "use strict"; | ||
}); | ||
exports.byteArrayToHex = byteArrayToHex; | ||
exports.hexToByteArray = hexToByteArray; | ||
@@ -23,5 +22,2 @@ function hexToByteArray(h) { | ||
} | ||
function byteArrayToHex(a) { | ||
return "0x" + a.map(i => ("00" + i.toString(16)).slice(-2)).join(""); | ||
} | ||
//# sourceMappingURL=utils.js.map |
{ | ||
"name": "@ckb-lumos/helpers", | ||
"version": "0.0.0-canary-6427eb4-20230810033735", | ||
"version": "0.0.0-canary-66228d6-20240122031015", | ||
"description": "Helper functions for working with CKB", | ||
@@ -29,7 +29,7 @@ "author": "Xuejie Xiao <xxuejie@gmail.com>", | ||
"dependencies": { | ||
"@ckb-lumos/base": "0.0.0-canary-6427eb4-20230810033735", | ||
"@ckb-lumos/bi": "0.0.0-canary-6427eb4-20230810033735", | ||
"@ckb-lumos/codec": "0.0.0-canary-6427eb4-20230810033735", | ||
"@ckb-lumos/config-manager": "0.0.0-canary-6427eb4-20230810033735", | ||
"@ckb-lumos/toolkit": "0.0.0-canary-6427eb4-20230810033735", | ||
"@ckb-lumos/base": "0.0.0-canary-66228d6-20240122031015", | ||
"@ckb-lumos/bi": "0.0.0-canary-66228d6-20240122031015", | ||
"@ckb-lumos/codec": "0.0.0-canary-66228d6-20240122031015", | ||
"@ckb-lumos/config-manager": "0.0.0-canary-66228d6-20240122031015", | ||
"@ckb-lumos/toolkit": "0.0.0-canary-66228d6-20240122031015", | ||
"bech32": "^2.0.0", | ||
@@ -36,0 +36,0 @@ "immutable": "^4.3.0" |
@@ -9,7 +9,7 @@ // https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0021-ckb-address-format/0021-ckb-address-format.md | ||
import { Address, Script } from "@ckb-lumos/base"; | ||
import { Address, Script, blockchain } from "@ckb-lumos/base"; | ||
import { bytes } from "@ckb-lumos/codec"; | ||
import { getConfig } from "@ckb-lumos/config-manager"; | ||
import { bech32, bech32m } from "bech32"; | ||
import { Options } from "./"; | ||
import { byteArrayToHex } from "./utils"; | ||
@@ -66,14 +66,6 @@ const BECH32_LIMIT = 1023; | ||
const codeHash = byteArrayToHex(body.slice(0, 32)); | ||
const hashType = (() => { | ||
const serializedHashType = body[32]; | ||
const codeHash = bytes.hexify(body.slice(0, 32)); | ||
const hashType = blockchain.HashType.unpack(body.slice(32, 33)); | ||
const args = bytes.hexify(body.slice(33)); | ||
if (serializedHashType === 0) return "data"; | ||
if (serializedHashType === 1) return "type"; | ||
if (serializedHashType === 2) return "data1"; | ||
throw new Error(`Invalid hashType ${serializedHashType}`); | ||
})(); | ||
const args = byteArrayToHex(body.slice(33)); | ||
return { codeHash, hashType, args }; | ||
@@ -117,3 +109,3 @@ } | ||
hashType: scriptTemplate.HASH_TYPE, | ||
args: byteArrayToHex(argsBytes), | ||
args: bytes.hexify(argsBytes), | ||
}; | ||
@@ -127,5 +119,5 @@ } | ||
return { | ||
codeHash: byteArrayToHex(body.slice(0, 32)), | ||
codeHash: bytes.hexify(body.slice(0, 32)), | ||
hashType: "data", | ||
args: byteArrayToHex(body.slice(32)), | ||
args: bytes.hexify(body.slice(32)), | ||
}; | ||
@@ -139,5 +131,5 @@ } | ||
return { | ||
codeHash: byteArrayToHex(body.slice(0, 32)), | ||
codeHash: bytes.hexify(body.slice(0, 32)), | ||
hashType: "type", | ||
args: byteArrayToHex(body.slice(32)), | ||
args: bytes.hexify(body.slice(32)), | ||
}; | ||
@@ -144,0 +136,0 @@ } |
@@ -26,2 +26,3 @@ import { bytes } from "@ckb-lumos/codec"; | ||
import { validators } from "@ckb-lumos/toolkit"; | ||
import { HashType } from "@ckb-lumos/base/lib/blockchain"; | ||
@@ -241,19 +242,13 @@ const { bytify, hexify } = bytes; | ||
config = config || getConfig(); | ||
// https://github.com/nervosnetwork/rfcs/blob/9aef152a5123c8972de1aefc11794cf84d1762ed/rfcs/0021-ckb-address-format/0021-ckb-address-format.md#full-payload-format | ||
// Full payload format directly encodes all data fields of lock script. The encode rule of full payload format is Bech32m. | ||
// payload = 0x00 | code_hash | hash_type | args | ||
const data: number[] = []; | ||
const data = bytes.concat( | ||
[0x00], | ||
script.codeHash, | ||
HashType.pack(script.hashType), | ||
script.args | ||
); | ||
const hashType = (() => { | ||
if (script.hashType === "data") return 0; | ||
if (script.hashType === "type") return 1; | ||
if (script.hashType === "data1") return 2; | ||
/* c8 ignore next */ | ||
throw new Error(`Invalid hashType ${script.hashType}`); | ||
})(); | ||
data.push(0x00); | ||
data.push(...hexToByteArray(script.codeHash)); | ||
data.push(hashType); | ||
data.push(...hexToByteArray(script.args)); | ||
return bech32m.encode(config.PREFIX, bech32m.toWords(data), BECH32_LIMIT); | ||
@@ -260,0 +255,0 @@ } |
@@ -17,5 +17,1 @@ import { HexString } from "@ckb-lumos/base"; | ||
} | ||
export function byteArrayToHex(a: number[]): HexString { | ||
return "0x" + a.map((i) => ("00" + i.toString(16)).slice(-2)).join(""); | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
90026
1279
+ Added@ckb-lumos/base@0.0.0-canary-66228d6-20240122031015(transitive)
+ Added@ckb-lumos/bi@0.0.0-canary-66228d6-20240122031015(transitive)
+ Added@ckb-lumos/codec@0.0.0-canary-66228d6-20240122031015(transitive)
+ Added@ckb-lumos/config-manager@0.0.0-canary-66228d6-20240122031015(transitive)
+ Added@ckb-lumos/rpc@0.0.0-canary-66228d6-20240122031015(transitive)
+ Added@ckb-lumos/toolkit@0.0.0-canary-66228d6-20240122031015(transitive)
+ Addedabort-controller@3.0.0(transitive)
+ Addedevent-target-shim@5.0.1(transitive)
- Removed@ckb-lumos/base@0.0.0-canary-6427eb4-20230810033735(transitive)
- Removed@ckb-lumos/bi@0.0.0-canary-6427eb4-20230810033735(transitive)
- Removed@ckb-lumos/codec@0.0.0-canary-6427eb4-20230810033735(transitive)
- Removed@ckb-lumos/config-manager@0.0.0-canary-6427eb4-20230810033735(transitive)
- Removed@ckb-lumos/toolkit@0.0.0-canary-6427eb4-20230810033735(transitive)
Updated@ckb-lumos/config-manager@0.0.0-canary-66228d6-20240122031015
Updated@ckb-lumos/toolkit@0.0.0-canary-66228d6-20240122031015