@ckb-lumos/helpers
Advanced tools
Comparing version 0.0.0-canary-ef5d099-20231211063519 to 0.0.0-canary-f2f5666-20240614035156
@@ -26,3 +26,3 @@ "use strict"; | ||
*/ | ||
const ADDRESS_FORMAT_FULL = 0x00; | ||
const ADDRESS_FORMAT_FULL = exports.ADDRESS_FORMAT_FULL = 0x00; | ||
/** | ||
@@ -32,4 +32,3 @@ * @deprecated | ||
*/ | ||
exports.ADDRESS_FORMAT_FULL = ADDRESS_FORMAT_FULL; | ||
const ADDRESS_FORMAT_SHORT = 0x01; | ||
const ADDRESS_FORMAT_SHORT = exports.ADDRESS_FORMAT_SHORT = 0x01; | ||
@@ -40,4 +39,3 @@ /** | ||
*/ | ||
exports.ADDRESS_FORMAT_SHORT = ADDRESS_FORMAT_SHORT; | ||
const ADDRESS_FORMAT_FULLDATA = 0x02; | ||
const ADDRESS_FORMAT_FULLDATA = exports.ADDRESS_FORMAT_FULLDATA = 0x02; | ||
@@ -48,5 +46,3 @@ /** | ||
*/ | ||
exports.ADDRESS_FORMAT_FULLDATA = ADDRESS_FORMAT_FULLDATA; | ||
const ADDRESS_FORMAT_FULLTYPE = 0x04; | ||
exports.ADDRESS_FORMAT_FULLTYPE = ADDRESS_FORMAT_FULLTYPE; | ||
const ADDRESS_FORMAT_FULLTYPE = exports.ADDRESS_FORMAT_FULLTYPE = 0x04; | ||
function parseFullFormatAddress(address, { | ||
@@ -53,0 +49,0 @@ config |
@@ -123,3 +123,4 @@ import { Address, Cell, CellDep, CellProvider, Hash, HexString, PackedSince, Script, Transaction, OutPoint } from "@ckb-lumos/base"; | ||
export declare function objectToTransactionSkeleton(obj: TransactionSkeletonObject): TransactionSkeletonType; | ||
export {}; | ||
export { refreshTypeIdCellDeps } from "./refresh"; | ||
export * from "./models"; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -6,2 +6,24 @@ "use strict"; | ||
}); | ||
var _exportNames = { | ||
minimalScriptCapacity: true, | ||
minimalScriptCapacityCompatible: true, | ||
minimalCellCapacity: true, | ||
minimalCellCapacityCompatible: true, | ||
locateCellDep: true, | ||
generateAddress: true, | ||
scriptToAddress: true, | ||
generateSecp256k1Blake160Address: true, | ||
generateSecp256k1Blake160MultisigAddress: true, | ||
parseAddress: true, | ||
addressToScript: true, | ||
encodeToAddress: true, | ||
encodeToConfigAddress: true, | ||
TransactionSkeleton: true, | ||
createTransactionFromSkeleton: true, | ||
createTransactionSkeleton: true, | ||
sealTransaction: true, | ||
transactionSkeletonToObject: true, | ||
objectToTransactionSkeleton: true, | ||
refreshTypeIdCellDeps: true | ||
}; | ||
exports.addressToScript = exports.TransactionSkeleton = void 0; | ||
@@ -22,2 +44,8 @@ exports.createTransactionFromSkeleton = createTransactionFromSkeleton; | ||
exports.parseAddress = parseAddress; | ||
Object.defineProperty(exports, "refreshTypeIdCellDeps", { | ||
enumerable: true, | ||
get: function () { | ||
return _refresh.refreshTypeIdCellDeps; | ||
} | ||
}); | ||
exports.scriptToAddress = void 0; | ||
@@ -36,2 +64,15 @@ exports.sealTransaction = sealTransaction; | ||
var _blockchain = require("@ckb-lumos/base/lib/blockchain"); | ||
var _refresh = require("./refresh"); | ||
var _models = require("./models"); | ||
Object.keys(_models).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return; | ||
if (key in exports && exports[key] === _models[key]) return; | ||
Object.defineProperty(exports, key, { | ||
enumerable: true, | ||
get: function () { | ||
return _models[key]; | ||
} | ||
}); | ||
}); | ||
const { | ||
@@ -50,2 +91,3 @@ bytify, | ||
} | ||
const ONE_CKB = 100_000_000; | ||
function minimalScriptCapacityCompatible(script, { | ||
@@ -62,3 +104,3 @@ validate = true | ||
bytes += 1; | ||
return _bi.BI.from(bytes).mul(100000000); | ||
return _bi.BI.from(bytes).mul(ONE_CKB); | ||
} | ||
@@ -93,3 +135,3 @@ function minimalCellCapacity(fullCell, { | ||
} | ||
return _bi.BI.from(bytes).mul(100000000); | ||
return _bi.BI.from(bytes).mul(ONE_CKB); | ||
} | ||
@@ -114,2 +156,12 @@ function locateCellDep(script, { | ||
// |format type|description | | ||
// |-----------|----------------------------------------------------------| | ||
// |0x00 |full version identifies the hash_type | | ||
// |0x01 |short version for locks with popular code_hash, deprecated| | ||
// |0x02 |full version with hash_type = "Data", deprecated | | ||
// |0x04 |full version with hash_type = "Type", deprecated | | ||
const CKB2019_ADDRESS_FORMAT_TYPE_DATA = 0x02; | ||
const CKB2019_ADDRESS_FORMAT_TYPE_TYPE = 0x04; | ||
/** | ||
@@ -136,3 +188,9 @@ * @deprecated please migrate to {@link encodeToAddress}, the short format address will be removed in the future | ||
} else { | ||
if (script.hashType === "type") data.push(0x04);else if (script.hashType === "data") data.push(0x02);else throw new Error(`Invalid hashType ${script.hashType}`); | ||
if (script.hashType === "type") { | ||
data.push(CKB2019_ADDRESS_FORMAT_TYPE_TYPE); | ||
} else if (script.hashType === "data") { | ||
data.push(CKB2019_ADDRESS_FORMAT_TYPE_DATA); | ||
} else { | ||
throw new Error(`Invalid hashType ${script.hashType}`); | ||
} | ||
data.push(...(0, _utils.hexToByteArray)(script.codeHash)); | ||
@@ -147,3 +205,3 @@ data.push(...(0, _utils.hexToByteArray)(script.args)); | ||
* @deprecated please migrate to {@link encodeToAddress}, the short format address will be removed in the future */ | ||
const scriptToAddress = generateAddress; | ||
const scriptToAddress = exports.scriptToAddress = generateAddress; | ||
@@ -157,3 +215,2 @@ /** | ||
*/ | ||
exports.scriptToAddress = scriptToAddress; | ||
function generatePredefinedAddress(args, scriptType, { | ||
@@ -218,3 +275,3 @@ config = undefined | ||
} | ||
const addressToScript = parseAddress; | ||
const addressToScript = exports.addressToScript = parseAddress; | ||
@@ -226,3 +283,2 @@ /** | ||
*/ | ||
exports.addressToScript = addressToScript; | ||
function encodeToAddress(script, { | ||
@@ -276,3 +332,3 @@ config = undefined | ||
} | ||
const TransactionSkeleton = (0, _immutable.Record)({ | ||
const TransactionSkeleton = exports.TransactionSkeleton = (0, _immutable.Record)({ | ||
cellProvider: null, | ||
@@ -288,3 +344,2 @@ cellDeps: (0, _immutable.List)(), | ||
}); | ||
exports.TransactionSkeleton = TransactionSkeleton; | ||
function createTransactionFromSkeleton(txSkeleton, { | ||
@@ -291,0 +346,0 @@ validate = true |
{ | ||
"name": "@ckb-lumos/helpers", | ||
"version": "0.0.0-canary-ef5d099-20231211063519", | ||
"version": "0.0.0-canary-f2f5666-20240614035156", | ||
"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-ef5d099-20231211063519", | ||
"@ckb-lumos/bi": "0.0.0-canary-ef5d099-20231211063519", | ||
"@ckb-lumos/codec": "0.0.0-canary-ef5d099-20231211063519", | ||
"@ckb-lumos/config-manager": "0.0.0-canary-ef5d099-20231211063519", | ||
"@ckb-lumos/toolkit": "0.0.0-canary-ef5d099-20231211063519", | ||
"@ckb-lumos/base": "0.0.0-canary-f2f5666-20240614035156", | ||
"@ckb-lumos/bi": "0.0.0-canary-f2f5666-20240614035156", | ||
"@ckb-lumos/codec": "0.0.0-canary-f2f5666-20240614035156", | ||
"@ckb-lumos/config-manager": "0.0.0-canary-f2f5666-20240614035156", | ||
"@ckb-lumos/toolkit": "0.0.0-canary-f2f5666-20240614035156", | ||
"bech32": "^2.0.0", | ||
@@ -36,0 +36,0 @@ "immutable": "^4.3.0" |
@@ -10,3 +10,7 @@ # `@ckb-lumos/helpers` | ||
```javascript | ||
const { minimalCellCapacity, generateAddress, parseAddress } = require("@ckb-lumos/helpers") | ||
const { | ||
minimalCellCapacity, | ||
generateAddress, | ||
parseAddress, | ||
} = require("@ckb-lumos/helpers"); | ||
@@ -29,3 +33,3 @@ // Get cell minimal capacity. | ||
outPoint: null, | ||
}) | ||
}); | ||
@@ -40,13 +44,16 @@ // result will be 6100000000n shannons. | ||
args: "0x36c329ed630d6ce750712a477543672adab57f4c", | ||
}) | ||
}); | ||
// Then you will get mainnet address "ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd", or you can generate testnet address by | ||
const { predefined } = require("@ckb-lumos/config-manager") | ||
const { predefined } = require("@ckb-lumos/config-manager"); | ||
const address = generateAddress({ | ||
codeHash: | ||
"0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", | ||
hashType: "type", | ||
args: "0x36c329ed630d6ce750712a477543672adab57f4c", | ||
}, { config: predefined.AGGRON4 }) | ||
const address = generateAddress( | ||
{ | ||
codeHash: | ||
"0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", | ||
hashType: "type", | ||
args: "0x36c329ed630d6ce750712a477543672adab57f4c", | ||
}, | ||
{ config: predefined.AGGRON4 } | ||
); | ||
@@ -56,3 +63,3 @@ // Will get testnet address "ckt1qyqrdsefa43s6m882pcj53m4gdnj4k440axqswmu83". | ||
// Use `parseAddress` to get lock script from an address. | ||
const script = parseAddress("ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd") | ||
const script = parseAddress("ckb1qyqrdsefa43s6m882pcj53m4gdnj4k440axqdt9rtd"); | ||
@@ -62,9 +69,34 @@ // TransactionSkeleton <=> Object | ||
// Convert TransactionSkeleton to js object | ||
const obj = transactionSkeletonToObject(txSkeleton) | ||
const obj = transactionSkeletonToObject(txSkeleton); | ||
// then your can write to json file | ||
fs.writeFileSync("your file", JSON.stringify(obj)) | ||
fs.writeFileSync("your file", JSON.stringify(obj)); | ||
// Or convert js object to TransactionSkeleton | ||
// If your object is from json file, make sure `cellProvider` is working properly. | ||
const txSkeleton = objectToTransactionSkeleton(obj) | ||
const txSkeleton = objectToTransactionSkeleton(obj); | ||
``` | ||
### ModelHelpers | ||
The `ModelHelper` provides a set of common methods, such as `create`, `hash`, `clone`, and `equals`, for CKB-related objects. | ||
This helper is designed to work with a `ModelLike` object for convenience, allowing developers to work with ambiguous objects instead of just the strict object. | ||
```ts | ||
export type ModelHelper<Model, ModelLike = Model> = { | ||
create(modelLike: ModelLike): Model; | ||
equals(modelLike: ModelLike, modelR: ModelLike): boolean; | ||
hash(modelLike: ModelLike): Uint8Array; | ||
clone(model: ModelLike): Model; | ||
}; | ||
``` | ||
```javascript | ||
import { cellHelper } from "@ckb-lumos/helpers"; | ||
const cell = cellHelper.create({ | ||
lock: "ckb1qzda0cr08m85hc8jlnfp3zer7xulejywt49kt2rr0vthywaa50xwsqgqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq5m759c", | ||
}); | ||
const _61CKB = 61 * 10 ** 8; | ||
asserts(BI.from(cell.cellOutput.capacity).eq(_61CKB)); // 61 CKB | ||
``` |
@@ -43,2 +43,4 @@ import { bytes } from "@ckb-lumos/codec"; | ||
const ONE_CKB = 100_000_000; | ||
export function minimalScriptCapacityCompatible( | ||
@@ -58,3 +60,3 @@ script: Script, | ||
return BI.from(bytes).mul(100000000); | ||
return BI.from(bytes).mul(ONE_CKB); | ||
} | ||
@@ -91,3 +93,3 @@ | ||
} | ||
return BI.from(bytes).mul(100000000); | ||
return BI.from(bytes).mul(ONE_CKB); | ||
} | ||
@@ -119,2 +121,12 @@ | ||
// |format type|description | | ||
// |-----------|----------------------------------------------------------| | ||
// |0x00 |full version identifies the hash_type | | ||
// |0x01 |short version for locks with popular code_hash, deprecated| | ||
// |0x02 |full version with hash_type = "Data", deprecated | | ||
// |0x04 |full version with hash_type = "Type", deprecated | | ||
const CKB2019_ADDRESS_FORMAT_TYPE_DATA = 0x02; | ||
const CKB2019_ADDRESS_FORMAT_TYPE_TYPE = 0x04; | ||
/** | ||
@@ -148,5 +160,9 @@ * @deprecated please migrate to {@link encodeToAddress}, the short format address will be removed in the future | ||
} else { | ||
if (script.hashType === "type") data.push(0x04); | ||
else if (script.hashType === "data") data.push(0x02); | ||
else throw new Error(`Invalid hashType ${script.hashType}`); | ||
if (script.hashType === "type") { | ||
data.push(CKB2019_ADDRESS_FORMAT_TYPE_TYPE); | ||
} else if (script.hashType === "data") { | ||
data.push(CKB2019_ADDRESS_FORMAT_TYPE_DATA); | ||
} else { | ||
throw new Error(`Invalid hashType ${script.hashType}`); | ||
} | ||
@@ -522,1 +538,4 @@ data.push(...hexToByteArray(script.codeHash)); | ||
} | ||
export { refreshTypeIdCellDeps } from "./refresh"; | ||
export * from "./models"; |
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
125509
48
1800
98
+ Added@ckb-lumos/base@0.0.0-canary-f2f5666-20240614035156(transitive)
+ Added@ckb-lumos/bi@0.0.0-canary-f2f5666-20240614035156(transitive)
+ Added@ckb-lumos/codec@0.0.0-canary-f2f5666-20240614035156(transitive)
+ Added@ckb-lumos/config-manager@0.0.0-canary-f2f5666-20240614035156(transitive)
+ Added@ckb-lumos/rpc@0.0.0-canary-f2f5666-20240614035156(transitive)
+ Added@ckb-lumos/toolkit@0.0.0-canary-f2f5666-20240614035156(transitive)
+ Addedabort-controller@3.0.0(transitive)
+ Addedcross-fetch@3.2.0(transitive)
+ Addedevent-target-shim@5.0.1(transitive)
+ Addednode-fetch@2.7.0(transitive)
+ Addedtr46@0.0.3(transitive)
+ Addedwebidl-conversions@3.0.1(transitive)
+ Addedwhatwg-url@5.0.0(transitive)
- Removed@ckb-lumos/base@0.0.0-canary-ef5d099-20231211063519(transitive)
- Removed@ckb-lumos/bi@0.0.0-canary-ef5d099-20231211063519(transitive)
- Removed@ckb-lumos/codec@0.0.0-canary-ef5d099-20231211063519(transitive)
- Removed@ckb-lumos/config-manager@0.0.0-canary-ef5d099-20231211063519(transitive)
- Removed@ckb-lumos/toolkit@0.0.0-canary-ef5d099-20231211063519(transitive)
- Removedlodash.isequal@4.5.0(transitive)
Updated@ckb-lumos/config-manager@0.0.0-canary-f2f5666-20240614035156
Updated@ckb-lumos/toolkit@0.0.0-canary-f2f5666-20240614035156