@ckb-lumos/helpers
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -1,8 +0,9 @@ | ||
const { core } = require("@ckb-lumos/types"); | ||
const { core } = require("@ckb-lumos/base"); | ||
const bech32 = require("bech32"); | ||
const { normalizers, validators, Reader } = require("ckb-js-toolkit"); | ||
const { List, Record, Map } = require("immutable"); | ||
const configs = require("./configs"); | ||
const { LINA } = configs; | ||
const { getConfig } = require("@ckb-lumos/config-manager"); | ||
const BECH32_LIMIT = 1023; | ||
function byteArrayToHex(a) { | ||
@@ -35,2 +36,3 @@ return "0x" + a.map((i) => ("00" + i.toString(16)).slice(-2)).join(""); | ||
bytes += new Reader(fullCell.cell_output.lock.args).length(); | ||
// hash_type field | ||
bytes += 1; | ||
@@ -48,7 +50,6 @@ if (fullCell.cell_output.type) { | ||
function locateCellDep(script, { config = LINA } = {}) { | ||
function locateCellDep(script, { config = undefined } = {}) { | ||
config = config || getConfig(); | ||
const scriptTemplate = Object.values(config.SCRIPTS).find( | ||
(s) => | ||
s.SCRIPT.code_hash === script.code_hash && | ||
s.SCRIPT.hash_type === script.hash_type | ||
(s) => s.CODE_HASH === script.code_hash && s.HASH_TYPE === script.hash_type | ||
); | ||
@@ -58,3 +59,6 @@ if (scriptTemplate) { | ||
dep_type: scriptTemplate.DEP_TYPE, | ||
out_point: Object.assign({}, scriptTemplate.OUT_POINT), | ||
out_point: { | ||
tx_hash: scriptTemplate.TX_HASH, | ||
index: scriptTemplate.INDEX, | ||
}, | ||
}; | ||
@@ -65,10 +69,9 @@ } | ||
function generateAddress(script, { config = LINA } = {}) { | ||
function generateAddress(script, { config = undefined } = {}) { | ||
config = config || getConfig(); | ||
const scriptTemplate = Object.values(config.SCRIPTS).find( | ||
(s) => | ||
s.SCRIPT.code_hash === script.code_hash && | ||
s.SCRIPT.hash_type === script.hash_type | ||
(s) => s.CODE_HASH === script.code_hash && s.HASH_TYPE === script.hash_type | ||
); | ||
const data = []; | ||
if (scriptTemplate) { | ||
if (scriptTemplate && scriptTemplate.SHORT_ID !== undefined) { | ||
data.push(1, scriptTemplate.SHORT_ID); | ||
@@ -79,10 +82,11 @@ data.push(...hexToByteArray(script.args)); | ||
data.push(...hexToByteArray(script.code_hash)); | ||
data.push(...hexToByteArray(script.hash_type)); | ||
data.push(...hexToByteArray(script.args)); | ||
} | ||
const words = bech32.toWords(data); | ||
return bech32.encode(config.PREFIX, words); | ||
return bech32.encode(config.PREFIX, words, BECH32_LIMIT); | ||
} | ||
function parseAddress(address, { config = LINA } = {}) { | ||
const { prefix, words } = bech32.decode(address); | ||
function parseAddress(address, { config = undefined } = {}) { | ||
config = config || getConfig(); | ||
const { prefix, words } = bech32.decode(address, BECH32_LIMIT); | ||
if (prefix !== config.PREFIX) { | ||
@@ -105,5 +109,7 @@ throw Error( | ||
} | ||
return Object.assign({}, scriptTemplate.SCRIPT, { | ||
return { | ||
code_hash: scriptTemplate.CODE_HASH, | ||
hash_type: scriptTemplate.HASH_TYPE, | ||
args: byteArrayToHex(data.slice(2)), | ||
}); | ||
}; | ||
case 2: | ||
@@ -217,3 +223,2 @@ if (data.length < 33) { | ||
module.exports = { | ||
configs, | ||
locateCellDep, | ||
@@ -220,0 +225,0 @@ minimalCellCapacity, |
{ | ||
"name": "@ckb-lumos/helpers", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Helper functions for working with CKB", | ||
@@ -24,5 +24,5 @@ "author": "Xuejie Xiao <xxuejie@gmail.com>", | ||
"scripts": { | ||
"fmt": "prettier --write \"lib/**/*.js\" index.d.ts package.json", | ||
"lint": "eslint -c ../../.eslintrc.js \"lib/**/*.js\"", | ||
"test": "echo \"No tests yet!\"" | ||
"fmt": "prettier --write \"{lib,tests}/**/*.js\" index.d.ts package.json", | ||
"lint": "eslint -c ../../.eslintrc.js \"{lib,tests}/**/*.js\"", | ||
"test": "ava tests/**/*.test.js" | ||
}, | ||
@@ -33,3 +33,3 @@ "bugs": { | ||
"dependencies": { | ||
"@ckb-lumos/types": "^0.3.0", | ||
"@ckb-lumos/base": "^0.4.0", | ||
"bech32": "^1.1.4", | ||
@@ -36,0 +36,0 @@ "ckb-js-toolkit": "^0.9.1", |
@@ -5,3 +5,3 @@ # `@ckb-lumos/helpers` | ||
The difference between this and `@ckb-lumos/types`, is that `@ckb-lumos/types` contains only core definitions, while this module contains utilities used in a framework sense. One example is: you can pretty much use `@ckb-lumos/types` as a standalone library, while this library integrates more with `config` module so it knows whether the framework is running under testnet, or mainnet environment. | ||
The difference between this and `@ckb-lumos/base`, is that `@ckb-lumos/base` contains only core definitions, while this module contains utilities used in a framework sense. One example is: you can pretty much use `@ckb-lumos/base` as a standalone library, while this library integrates more with `config` module so it knows whether the framework is running under testnet, or mainnet environment. | ||
@@ -8,0 +8,0 @@ ## Usage |
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
1
8103
3
213
+ Added@ckb-lumos/base@^0.4.0
+ Added@ckb-lumos/base@0.4.8(transitive)
- Removed@ckb-lumos/types@^0.3.0
- Removed@ckb-lumos/types@0.3.0(transitive)