@ckb-lumos/helpers
Advanced tools
Comparing version 0.18.0-rc1 to 0.18.0-rc2
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.objectToTransactionSkeleton = exports.transactionSkeletonToObject = exports.sealTransaction = exports.createTransactionFromSkeleton = exports.TransactionSkeleton = exports.addressToScript = exports.parseAddress = exports.generateSecp256k1Blake160MultisigAddress = exports.generateSecp256k1Blake160Address = exports.scriptToAddress = exports.generateAddress = exports.locateCellDep = exports.minimalCellCapacity = void 0; | ||
const base_1 = require("@ckb-lumos/base"); | ||
const bech32 = __importStar(require("bech32")); | ||
const bech32_1 = require("bech32"); | ||
const ckb_js_toolkit_1 = require("ckb-js-toolkit"); | ||
@@ -90,2 +71,18 @@ const immutable_1 = require("immutable"); | ||
} | ||
else if (config.CKB2021) { | ||
const hash_type = (() => { | ||
if (script.hash_type === "data") | ||
return 0; | ||
if (script.hash_type === "type") | ||
return 1; | ||
if (script.hash_type === "data1") | ||
return 2; | ||
throw new Error(`unknown hash_type ${script.hash_type}`); | ||
})(); | ||
data.push(0x00); | ||
data.push(...hexToByteArray(script.code_hash)); | ||
data.push(hash_type); | ||
data.push(...hexToByteArray(script.args)); | ||
return bech32_1.bech32m.encode(config.PREFIX, bech32_1.bech32m.toWords(data), BECH32_LIMIT); | ||
} | ||
else { | ||
@@ -96,4 +93,4 @@ data.push(script.hash_type === "type" ? 4 : 2); | ||
} | ||
const words = bech32.toWords(data); | ||
return bech32.encode(config.PREFIX, words, BECH32_LIMIT); | ||
const words = bech32_1.bech32.toWords(data); | ||
return bech32_1.bech32.encode(config.PREFIX, words, BECH32_LIMIT); | ||
} | ||
@@ -122,10 +119,42 @@ exports.generateAddress = generateAddress; | ||
exports.generateSecp256k1Blake160MultisigAddress = generateSecp256k1Blake160MultisigAddress; | ||
function trySeries(...fns) { | ||
let latestCatch; | ||
for (let fn of fns) { | ||
try { | ||
return fn(); | ||
} | ||
catch (e) { | ||
latestCatch = e; | ||
} | ||
} | ||
throw latestCatch; | ||
} | ||
function parseAddress(address, { config = undefined } = {}) { | ||
config = config || config_manager_1.getConfig(); | ||
const { prefix, words } = bech32.decode(address, BECH32_LIMIT); | ||
const { prefix, words } = trySeries(() => bech32_1.bech32.decode(address, BECH32_LIMIT), () => bech32_1.bech32m.decode(address, BECH32_LIMIT)); | ||
if (prefix !== config.PREFIX) { | ||
throw Error(`Invalid prefix! Expected: ${config.PREFIX}, actual: ${prefix}`); | ||
} | ||
const data = bech32.fromWords(words); | ||
const data = trySeries(() => bech32_1.bech32.fromWords(words), () => bech32_1.bech32m.fromWords(words)); | ||
switch (data[0]) { | ||
case 0: | ||
// 1 + 32 + 1 | ||
// 00 code_hash hash_type | ||
if (data.length < 34) { | ||
throw new Error(`Invalid payload length!`); | ||
} | ||
const serializedHashType = data.slice(33, 34)[0]; | ||
return { | ||
code_hash: byteArrayToHex(data.slice(1, 33)), | ||
hash_type: (() => { | ||
if (serializedHashType === 0) | ||
return "data"; | ||
if (serializedHashType === 1) | ||
return "type"; | ||
if (serializedHashType === 2) | ||
return "data1"; | ||
throw new Error(`unknown hash_type ${serializedHashType}`); | ||
})(), | ||
args: byteArrayToHex(data.slice(34)), | ||
}; | ||
case 1: | ||
@@ -132,0 +161,0 @@ if (data.length < 2) { |
{ | ||
"name": "@ckb-lumos/helpers", | ||
"version": "0.18.0-rc1", | ||
"version": "0.18.0-rc2", | ||
"description": "Helper functions for working with CKB", | ||
@@ -27,3 +27,3 @@ "author": "Xuejie Xiao <xxuejie@gmail.com>", | ||
"access": "public", | ||
"registry": "http://registry.npmjs.org/" | ||
"registry": "https://registry.npmjs.org/" | ||
}, | ||
@@ -47,6 +47,6 @@ "repository": { | ||
"dependencies": { | ||
"@ckb-lumos/base": "^0.18.0-rc1", | ||
"@ckb-lumos/config-manager": "^0.18.0-rc1", | ||
"bech32": "^1.1.4", | ||
"ckb-js-toolkit": "^0.10.2", | ||
"@ckb-lumos/base": "^0.18.0-rc2", | ||
"@ckb-lumos/config-manager": "^0.18.0-rc2", | ||
"bech32": "^2.0.0", | ||
"ckb-js-toolkit": "^0.100.0-rc1", | ||
"immutable": "^4.0.0-rc.12" | ||
@@ -53,0 +53,0 @@ }, |
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
29083
368
1
+ Addedbech32@2.0.0(transitive)
+ Addedckb-js-toolkit@0.100.0-rc1(transitive)
- Removedbech32@1.1.4(transitive)
- Removedckb-js-toolkit@0.10.2(transitive)
Updated@ckb-lumos/base@^0.18.0-rc2
Updatedbech32@^2.0.0
Updatedckb-js-toolkit@^0.100.0-rc1