@ckb-lumos/codec
Advanced tools
Comparing version 0.0.0-canary-0353ba3-20240115043810 to 0.0.0-canary-14ed633-20240513054710
@@ -12,6 +12,6 @@ import { AnyCodec, BytesCodec, BytesLike, FixedBytesCodec, PackParam, UnpackResult } from "./base"; | ||
export declare const Bytes: BytesCodec<string, BytesLike>; | ||
export declare const BytesOpt: import("./molecule/layout").OptionCodec<BytesCodec<string, BytesLike>>; | ||
export declare const BytesVec: import("./molecule/layout").ArrayCodec<BytesCodec<string, BytesLike>>; | ||
export declare const BytesOpt: import("./molecule").OptionLayoutCodec<BytesCodec<string, BytesLike>>; | ||
export declare const BytesVec: import("./molecule").ArrayLayoutCodec<BytesCodec<string, BytesLike>>; | ||
export declare const Byte32: FixedBytesCodec<string, BytesLike>; | ||
export declare const Byte32Vec: import("./molecule/layout").ArrayCodec<FixedBytesCodec<string, BytesLike>>; | ||
export declare const Byte32Vec: import("./molecule").ArrayLayoutCodec<FixedBytesCodec<string, BytesLike>>; | ||
export declare function WitnessArgsOf<LockCodec extends AnyCodec, InputTypeCodec extends AnyCodec, OutputTypeCodec extends AnyCodec>(payload: { | ||
@@ -18,0 +18,0 @@ lock: LockCodec; |
export { byteOf, byteArrayOf, byteVecOf } from "./helper"; | ||
export { table, array, option, struct, vector, union } from "./layout"; | ||
export type { ObjectLayoutCodec, ArrayLayoutCodec, OptionLayoutCodec, UnionLayoutCodec, } from "./layout"; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -20,3 +20,6 @@ /** | ||
type PartialNullable<O extends Record<string, unknown>> = Partial<Pick<O, NullableKeys<O>>> & Pick<O, NonNullableKeys<O>>; | ||
export type ObjectCodec<T extends Record<string, BytesCodec>> = BytesCodec<PartialNullable<{ | ||
/** | ||
* A codec for struct and table of Molecule | ||
*/ | ||
export type ObjectLayoutCodec<T extends Record<string, BytesCodec>> = BytesCodec<PartialNullable<{ | ||
[key in keyof T]: UnpackResult<T[key]>; | ||
@@ -26,7 +29,16 @@ }>, PartialNullable<{ | ||
}>>; | ||
export interface OptionCodec<T extends BytesCodec> extends BytesCodec<UnpackResult<T> | undefined> { | ||
/** | ||
* A codec for option of Molecule | ||
*/ | ||
export interface OptionLayoutCodec<T extends BytesCodec> extends BytesCodec<UnpackResult<T> | undefined> { | ||
pack: (packable?: PackParam<T>) => Uint8Array; | ||
} | ||
export type ArrayCodec<T extends BytesCodec> = BytesCodec<Array<UnpackResult<T>>, Array<PackParam<T>>>; | ||
export type UnionCodec<T extends Record<string, BytesCodec>> = BytesCodec<{ | ||
/** | ||
* A code for array and vector of Molecule | ||
*/ | ||
export type ArrayLayoutCodec<T extends BytesCodec> = BytesCodec<Array<UnpackResult<T>>, Array<PackParam<T>>>; | ||
/** | ||
* A molecule codec for ` | ||
*/ | ||
export type UnionLayoutCodec<T extends Record<string, BytesCodec>> = BytesCodec<{ | ||
[key in keyof T]: { | ||
@@ -48,3 +60,3 @@ type: key; | ||
*/ | ||
export declare function array<T extends FixedBytesCodec>(itemCodec: T, itemCount: number): ArrayCodec<T> & Fixed; | ||
export declare function array<T extends FixedBytesCodec>(itemCodec: T, itemCount: number): ArrayLayoutCodec<T> & Fixed; | ||
/** | ||
@@ -56,3 +68,3 @@ * Struct is a fixed-size type: all fields in struct are fixed-size and it has a fixed quantity of fields. | ||
*/ | ||
export declare function struct<T extends Record<string, FixedBytesCodec>>(shape: T, fields: (keyof T)[]): ObjectCodec<T> & Fixed; | ||
export declare function struct<T extends Record<string, FixedBytesCodec>>(shape: T, fields: (keyof T)[]): ObjectLayoutCodec<T> & Fixed; | ||
/** | ||
@@ -62,3 +74,3 @@ * Vector with fixed size item codec | ||
*/ | ||
export declare function fixvec<T extends FixedBytesCodec>(itemCodec: T): ArrayCodec<T>; | ||
export declare function fixvec<T extends FixedBytesCodec>(itemCodec: T): ArrayLayoutCodec<T>; | ||
/** | ||
@@ -69,3 +81,3 @@ * Vector with dynamic size item codec | ||
*/ | ||
export declare function dynvec<T extends BytesCodec>(itemCodec: T): ArrayCodec<T>; | ||
export declare function dynvec<T extends BytesCodec>(itemCodec: T): ArrayLayoutCodec<T>; | ||
/** | ||
@@ -75,3 +87,3 @@ * General vector codec, if `itemCodec` is fixed size type, it will create a fixvec codec, otherwise a dynvec codec will be created. | ||
*/ | ||
export declare function vector<T extends BytesCodec>(itemCodec: T): ArrayCodec<T>; | ||
export declare function vector<T extends BytesCodec>(itemCodec: T): ArrayLayoutCodec<T>; | ||
/** | ||
@@ -82,3 +94,3 @@ * Table is a dynamic-size type. It can be considered as a dynvec but the length is fixed. | ||
*/ | ||
export declare function table<T extends Record<string, BytesCodec>>(shape: T, fields: (keyof T)[]): ObjectCodec<T>; | ||
export declare function table<T extends Record<string, BytesCodec>>(shape: T, fields: (keyof T)[]): ObjectLayoutCodec<T>; | ||
/** | ||
@@ -97,3 +109,3 @@ * Union is a dynamic-size type. | ||
*/ | ||
export declare function union<T extends Record<string, BytesCodec>>(itemCodec: T, fields: (keyof T)[] | Record<keyof T, number>): UnionCodec<T>; | ||
export declare function union<T extends Record<string, BytesCodec>>(itemCodec: T, fields: (keyof T)[] | Record<keyof T, number>): UnionLayoutCodec<T>; | ||
/** | ||
@@ -106,4 +118,4 @@ * Option is a dynamic-size type. | ||
*/ | ||
export declare function option<T extends BytesCodec>(itemCodec: T): OptionCodec<T>; | ||
export declare function option<T extends BytesCodec>(itemCodec: T): OptionLayoutCodec<T>; | ||
export {}; | ||
//# sourceMappingURL=layout.d.ts.map |
@@ -34,2 +34,18 @@ "use strict"; | ||
/** | ||
* A codec for struct and table of Molecule | ||
*/ | ||
/** | ||
* A codec for option of Molecule | ||
*/ | ||
/** | ||
* A code for array and vector of Molecule | ||
*/ | ||
/** | ||
* A molecule codec for ` | ||
*/ | ||
/** | ||
* The array is a fixed-size type: it has a fixed-size inner type and a fixed length. | ||
@@ -36,0 +52,0 @@ * The size of an array is the size of inner type times the length. |
import { BI, BIish } from "@ckb-lumos/bi"; | ||
import { FixedBytesCodec } from "../base"; | ||
export { BI, BIish }; | ||
export declare const Uint8: FixedBytesCodec<number, BIish>; | ||
@@ -4,0 +5,0 @@ export declare const Uint16LE: FixedBytesCodec<number, BIish>; |
@@ -6,2 +6,14 @@ "use strict"; | ||
}); | ||
Object.defineProperty(exports, "BI", { | ||
enumerable: true, | ||
get: function () { | ||
return _bi.BI; | ||
} | ||
}); | ||
Object.defineProperty(exports, "BIish", { | ||
enumerable: true, | ||
get: function () { | ||
return _bi.BIish; | ||
} | ||
}); | ||
exports.Uint8 = exports.Uint64LE = exports.Uint64BE = exports.Uint64 = exports.Uint512LE = exports.Uint512BE = exports.Uint512 = exports.Uint32LE = exports.Uint32BE = exports.Uint32 = exports.Uint256LE = exports.Uint256BE = exports.Uint256 = exports.Uint16LE = exports.Uint16BE = exports.Uint16 = exports.Uint128LE = exports.Uint128BE = exports.Uint128 = void 0; | ||
@@ -8,0 +20,0 @@ var _bi = require("@ckb-lumos/bi"); |
{ | ||
"name": "@ckb-lumos/codec", | ||
"version": "0.0.0-canary-0353ba3-20240115043810", | ||
"version": "0.0.0-canary-14ed633-20240513054710", | ||
"description": "Make your own molecule binding in JavaScript(TypeScript)", | ||
@@ -22,3 +22,3 @@ "author": "", | ||
"dependencies": { | ||
"@ckb-lumos/bi": "0.0.0-canary-0353ba3-20240115043810" | ||
"@ckb-lumos/bi": "0.0.0-canary-14ed633-20240513054710" | ||
}, | ||
@@ -25,0 +25,0 @@ "publishConfig": { |
@@ -170,3 +170,3 @@ # @ckb-lumos/codec | ||
will have encountered more complex scripts | ||
like [OmniLock](https://github.com/XuJiandong/docs-bank/blob/master/omni_lock.md), where it is easy to get confused | ||
like [OmniLock](https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0042-omnilock/0042-omnilock.md), where it is easy to get confused | ||
about how to handle bytes when we want to sign it, if we can combine `WitnessArgs.lock(BytesOpt)` | ||
@@ -173,0 +173,0 @@ with `OmniLockWitnessLock.signature(BytesOpt)`, then it will be easier to do the signing, we can check |
export { byteOf, byteArrayOf, byteVecOf } from "./helper"; | ||
export { table, array, option, struct, vector, union } from "./layout"; | ||
export type { | ||
ObjectLayoutCodec, | ||
ArrayLayoutCodec, | ||
OptionLayoutCodec, | ||
UnionLayoutCodec, | ||
} from "./layout"; |
@@ -44,8 +44,15 @@ /** | ||
export type ObjectCodec<T extends Record<string, BytesCodec>> = BytesCodec< | ||
PartialNullable<{ [key in keyof T]: UnpackResult<T[key]> }>, | ||
PartialNullable<{ [key in keyof T]: PackParam<T[key]> }> | ||
>; | ||
/** | ||
* A codec for struct and table of Molecule | ||
*/ | ||
export type ObjectLayoutCodec<T extends Record<string, BytesCodec>> = | ||
BytesCodec< | ||
PartialNullable<{ [key in keyof T]: UnpackResult<T[key]> }>, | ||
PartialNullable<{ [key in keyof T]: PackParam<T[key]> }> | ||
>; | ||
export interface OptionCodec<T extends BytesCodec> | ||
/** | ||
* A codec for option of Molecule | ||
*/ | ||
export interface OptionLayoutCodec<T extends BytesCodec> | ||
extends BytesCodec<UnpackResult<T> | undefined> { | ||
@@ -55,3 +62,6 @@ pack: (packable?: PackParam<T>) => Uint8Array; | ||
export type ArrayCodec<T extends BytesCodec> = BytesCodec< | ||
/** | ||
* A code for array and vector of Molecule | ||
*/ | ||
export type ArrayLayoutCodec<T extends BytesCodec> = BytesCodec< | ||
Array<UnpackResult<T>>, | ||
@@ -61,3 +71,6 @@ Array<PackParam<T>> | ||
export type UnionCodec<T extends Record<string, BytesCodec>> = BytesCodec< | ||
/** | ||
* A molecule codec for ` | ||
*/ | ||
export type UnionLayoutCodec<T extends Record<string, BytesCodec>> = BytesCodec< | ||
{ [key in keyof T]: { type: key; value: UnpackResult<T[key]> } }[keyof T], | ||
@@ -76,3 +89,3 @@ { [key in keyof T]: { type: key; value: PackParam<T[key]> } }[keyof T] | ||
itemCount: number | ||
): ArrayCodec<T> & Fixed { | ||
): ArrayLayoutCodec<T> & Fixed { | ||
const enhancedArrayCodec = createArrayCodec(itemCodec); | ||
@@ -124,3 +137,3 @@ return createFixedBytesCodec({ | ||
fields: (keyof T)[] | ||
): ObjectCodec<T> & Fixed { | ||
): ObjectLayoutCodec<T> & Fixed { | ||
checkShape(shape, fields); | ||
@@ -161,3 +174,5 @@ const objectCodec = createObjectCodec(shape); | ||
*/ | ||
export function fixvec<T extends FixedBytesCodec>(itemCodec: T): ArrayCodec<T> { | ||
export function fixvec<T extends FixedBytesCodec>( | ||
itemCodec: T | ||
): ArrayLayoutCodec<T> { | ||
return createBytesCodec({ | ||
@@ -190,3 +205,5 @@ pack(items) { | ||
*/ | ||
export function dynvec<T extends BytesCodec>(itemCodec: T): ArrayCodec<T> { | ||
export function dynvec<T extends BytesCodec>( | ||
itemCodec: T | ||
): ArrayLayoutCodec<T> { | ||
return createBytesCodec({ | ||
@@ -251,3 +268,5 @@ pack(obj) { | ||
*/ | ||
export function vector<T extends BytesCodec>(itemCodec: T): ArrayCodec<T> { | ||
export function vector<T extends BytesCodec>( | ||
itemCodec: T | ||
): ArrayLayoutCodec<T> { | ||
if (isFixedCodec(itemCodec)) { | ||
@@ -267,3 +286,3 @@ return fixvec(itemCodec); | ||
fields: (keyof T)[] | ||
): ObjectCodec<T> { | ||
): ObjectLayoutCodec<T> { | ||
checkShape(shape, fields); | ||
@@ -347,3 +366,3 @@ return createBytesCodec({ | ||
fields: (keyof T)[] | Record<keyof T, number> | ||
): UnionCodec<T> { | ||
): UnionLayoutCodec<T> { | ||
checkShape(itemCodec, Array.isArray(fields) ? fields : Object.keys(fields)); | ||
@@ -418,3 +437,5 @@ | ||
*/ | ||
export function option<T extends BytesCodec>(itemCodec: T): OptionCodec<T> { | ||
export function option<T extends BytesCodec>( | ||
itemCodec: T | ||
): OptionLayoutCodec<T> { | ||
return createBytesCodec({ | ||
@@ -421,0 +442,0 @@ pack(obj?) { |
@@ -5,2 +5,4 @@ import { BI, BIish } from "@ckb-lumos/bi"; | ||
export { BI, BIish }; | ||
function assertNumberRange( | ||
@@ -7,0 +9,0 @@ value: BIish, |
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
Sorry, the diff of this file is not supported yet
184245
2619
+ Added@ckb-lumos/bi@0.0.0-canary-14ed633-20240513054710(transitive)
- Removed@ckb-lumos/bi@0.0.0-canary-0353ba3-20240115043810(transitive)