@ckb-lumos/helpers
Advanced tools
Comparing version 0.19.0-alpha.3 to 0.19.0-beta.0
import { Address, Cell, CellDep, CellProvider, Hash, HexString, PackedSince, Script, Transaction } from "@ckb-lumos/base"; | ||
import { List, Map as ImmutableMap, Record } from "immutable"; | ||
import { Config } from "@ckb-lumos/config-manager"; | ||
import { Config, predefined } from "@ckb-lumos/config-manager"; | ||
import { BI } from "@ckb-lumos/bi"; | ||
@@ -31,7 +31,28 @@ export interface Options { | ||
export declare const scriptToAddress: typeof generateAddress; | ||
/** | ||
* @deprecated please migrate to {@link encodeToConfigAddress} | ||
* @param args | ||
* @param param1 | ||
* @returns | ||
*/ | ||
export declare function generateSecp256k1Blake160Address(args: HexString, { config }?: Options): Address; | ||
/** | ||
* @deprecated please migrate to {@link encodeToConfigAddress} | ||
* @param args | ||
* @param config | ||
*/ | ||
export declare function generateSecp256k1Blake160MultisigAddress(args: HexString, { config }?: Options): Address; | ||
export declare function parseAddress(address: Address, { config }?: Options): Script; | ||
export declare const addressToScript: typeof parseAddress; | ||
/** | ||
* parse a lock script to an address | ||
* @param script | ||
* @param config | ||
*/ | ||
export declare function encodeToAddress(script: Script, { config }?: Options): Address; | ||
declare type PredefinedScriptName = keyof typeof predefined.LINA.SCRIPTS; | ||
export declare function encodeToConfigAddress(args: HexString, scriptType: PredefinedScriptName): string; | ||
export declare function encodeToConfigAddress<C extends Config>(args: HexString, scriptType: keyof C["SCRIPTS"], options: { | ||
config?: C; | ||
}): string; | ||
export interface TransactionSkeletonInterface { | ||
@@ -91,1 +112,2 @@ cellProvider: CellProvider | null; | ||
export declare function objectToTransactionSkeleton(obj: TransactionSkeletonObject): TransactionSkeletonType; | ||
export {}; |
@@ -9,2 +9,3 @@ "use strict"; | ||
exports.encodeToAddress = encodeToAddress; | ||
exports.encodeToConfigAddress = encodeToConfigAddress; | ||
exports.generateAddress = generateAddress; | ||
@@ -168,2 +169,10 @@ exports.generateSecp256k1Blake160Address = generateSecp256k1Blake160Address; | ||
const scriptToAddress = generateAddress; | ||
/** | ||
* @deprecated please migrate to {@link encodeToConfigAddress} | ||
* @param args | ||
* @param scriptType | ||
* @param param2 | ||
* @returns | ||
*/ | ||
exports.scriptToAddress = scriptToAddress; | ||
@@ -191,3 +200,10 @@ | ||
} | ||
/** | ||
* @deprecated please migrate to {@link encodeToConfigAddress} | ||
* @param args | ||
* @param param1 | ||
* @returns | ||
*/ | ||
function generateSecp256k1Blake160Address(args, { | ||
@@ -200,3 +216,9 @@ config = undefined | ||
} | ||
/** | ||
* @deprecated please migrate to {@link encodeToConfigAddress} | ||
* @param args | ||
* @param config | ||
*/ | ||
function generateSecp256k1Blake160MultisigAddress(args, { | ||
@@ -227,2 +249,8 @@ config = undefined | ||
const addressToScript = parseAddress; | ||
/** | ||
* parse a lock script to an address | ||
* @param script | ||
* @param config | ||
*/ | ||
exports.addressToScript = addressToScript; | ||
@@ -254,2 +282,40 @@ | ||
/** | ||
* encode a script to an address with args and a key of config | ||
* @example | ||
* ```ts | ||
* // parse a predefined lock to an address | ||
* encodeToConfigAddress('0x12345678123456781234567812345678', 'SECP256K1_BLAKE160'); | ||
* // parse a custom lock to an address | ||
* encodeToConfigAddress('0x12345678123456781234567812345678', 'MY_CUSTOM_LOCK', { | ||
* SCRIPTS: { | ||
* MY_CUSTOM_LOCK: {...} | ||
* } | ||
* }) | ||
* ``` | ||
* @param args script args | ||
* @param scriptType a key of `config.SCRIPTS` | ||
* @param options | ||
* @returns | ||
*/ | ||
function encodeToConfigAddress(args, scriptType, options) { | ||
const config = (options === null || options === void 0 ? void 0 : options.config) || (0, _configManager.getConfig)(); | ||
const template = config.SCRIPTS[scriptType]; | ||
if (!template) { | ||
const availableKeys = Object.keys(config.SCRIPTS); | ||
throw new Error( // prettier-ignore | ||
`Invalid script type: ${String(scriptType)}, only support: ${availableKeys}`); | ||
} | ||
const script = { | ||
codeHash: template.CODE_HASH, | ||
hashType: template.HASH_TYPE, | ||
args | ||
}; | ||
return encodeToAddress(script, { | ||
config | ||
}); | ||
} | ||
const TransactionSkeleton = (0, _immutable.Record)({ | ||
@@ -256,0 +322,0 @@ cellProvider: null, |
{ | ||
"name": "@ckb-lumos/helpers", | ||
"version": "0.19.0-alpha.3", | ||
"version": "0.19.0-beta.0", | ||
"description": "Helper functions for working with CKB", | ||
@@ -40,6 +40,6 @@ "author": "Xuejie Xiao <xxuejie@gmail.com>", | ||
"dependencies": { | ||
"@ckb-lumos/base": "^0.19.0-alpha.3", | ||
"@ckb-lumos/bi": "^0.19.0-alpha.3", | ||
"@ckb-lumos/config-manager": "^0.19.0-alpha.3", | ||
"@ckb-lumos/toolkit": "^0.19.0-alpha.3", | ||
"@ckb-lumos/base": "^0.19.0-beta.0", | ||
"@ckb-lumos/bi": "^0.19.0-beta.0", | ||
"@ckb-lumos/config-manager": "^0.19.0-beta.0", | ||
"@ckb-lumos/toolkit": "^0.19.0-beta.0", | ||
"bech32": "^2.0.0", | ||
@@ -62,3 +62,3 @@ "immutable": "^4.0.0-rc.12" | ||
}, | ||
"gitHead": "73af0bc3b396c40c2dbfe570d203f377f63aa993" | ||
"gitHead": "6f7065672f047445f109385a8bf56fe42d8d5f91" | ||
} |
@@ -17,3 +17,3 @@ import { bytes } from "@ckb-lumos/codec"; | ||
import { List, Map as ImmutableMap, Record } from "immutable"; | ||
import { Config, getConfig } from "@ckb-lumos/config-manager"; | ||
import { Config, getConfig, predefined } from "@ckb-lumos/config-manager"; | ||
import { BI } from "@ckb-lumos/bi"; | ||
@@ -158,2 +158,9 @@ import { | ||
/** | ||
* @deprecated please migrate to {@link encodeToConfigAddress} | ||
* @param args | ||
* @param scriptType | ||
* @param param2 | ||
* @returns | ||
*/ | ||
function generatePredefinedAddress( | ||
@@ -181,2 +188,8 @@ args: HexString, | ||
/** | ||
* @deprecated please migrate to {@link encodeToConfigAddress} | ||
* @param args | ||
* @param param1 | ||
* @returns | ||
*/ | ||
export function generateSecp256k1Blake160Address( | ||
@@ -189,2 +202,7 @@ args: HexString, | ||
/** | ||
* @deprecated please migrate to {@link encodeToConfigAddress} | ||
* @param args | ||
* @param config | ||
*/ | ||
export function generateSecp256k1Blake160MultisigAddress( | ||
@@ -214,2 +232,7 @@ args: HexString, | ||
/** | ||
* parse a lock script to an address | ||
* @param script | ||
* @param config | ||
*/ | ||
export function encodeToAddress( | ||
@@ -241,2 +264,56 @@ script: Script, | ||
type PredefinedScriptName = keyof typeof predefined.LINA.SCRIPTS; | ||
export function encodeToConfigAddress( | ||
args: HexString, | ||
scriptType: PredefinedScriptName | ||
): string; | ||
export function encodeToConfigAddress<C extends Config>( | ||
args: HexString, | ||
scriptType: keyof C["SCRIPTS"], | ||
options: { config?: C } | ||
): string; | ||
/** | ||
* encode a script to an address with args and a key of config | ||
* @example | ||
* ```ts | ||
* // parse a predefined lock to an address | ||
* encodeToConfigAddress('0x12345678123456781234567812345678', 'SECP256K1_BLAKE160'); | ||
* // parse a custom lock to an address | ||
* encodeToConfigAddress('0x12345678123456781234567812345678', 'MY_CUSTOM_LOCK', { | ||
* SCRIPTS: { | ||
* MY_CUSTOM_LOCK: {...} | ||
* } | ||
* }) | ||
* ``` | ||
* @param args script args | ||
* @param scriptType a key of `config.SCRIPTS` | ||
* @param options | ||
* @returns | ||
*/ | ||
export function encodeToConfigAddress<C extends Config>( | ||
args: HexString, | ||
scriptType: keyof C["SCRIPTS"], | ||
options?: { config?: C } | ||
): string { | ||
const config = (options?.config || getConfig()) as C; | ||
const template = config.SCRIPTS[scriptType as string]; | ||
if (!template) { | ||
const availableKeys = Object.keys(config.SCRIPTS); | ||
throw new Error( | ||
// prettier-ignore | ||
`Invalid script type: ${String(scriptType)}, only support: ${availableKeys}` | ||
); | ||
} | ||
const script: Script = { | ||
codeHash: template.CODE_HASH, | ||
hashType: template.HASH_TYPE, | ||
args, | ||
}; | ||
return encodeToAddress(script, { config }); | ||
} | ||
export interface TransactionSkeletonInterface { | ||
@@ -243,0 +320,0 @@ cellProvider: CellProvider | null; |
Sorry, the diff of this file is not supported yet
87035
19
1314