@ckb-lumos/helpers
Advanced tools
Comparing version 0.20.0-alpha.0 to 0.20.0-alpha.1
@@ -1,2 +0,2 @@ | ||
import { Address, Cell, CellDep, CellProvider, Hash, HexString, PackedSince, Script, Transaction } from "@ckb-lumos/base"; | ||
import { Address, Cell, CellDep, CellProvider, Hash, HexString, PackedSince, Script, Transaction, OutPoint } from "@ckb-lumos/base"; | ||
import { List, Map as ImmutableMap, Record } from "immutable"; | ||
@@ -80,2 +80,14 @@ import { Config, predefined } from "@ckb-lumos/config-manager"; | ||
}): Transaction; | ||
declare type Promisable<T> = Promise<T> | T; | ||
export declare type LiveCellFetcher = (outPoint: OutPoint) => Promisable<Cell>; | ||
/** | ||
* create a {@link TransactionSkeleton} from a {@link Transaction} | ||
* @example | ||
* ```js | ||
* const rpc = new RPC('localhost:8114') | ||
* const fetcher = (outPoint: OutPoint) => rpc.getLiveCell(outPoint) | ||
* const skeleton = await createTransactionSkeleton({ transaction, fetcher }); | ||
* ``` | ||
*/ | ||
export declare function createTransactionSkeleton(transaction: Transaction, fetcher: LiveCellFetcher): Promise<TransactionSkeletonType>; | ||
export declare function sealTransaction(txSkeleton: TransactionSkeletonType, sealingContents: HexString[]): Transaction; | ||
@@ -82,0 +94,0 @@ export interface TransactionSkeletonObject { |
@@ -8,2 +8,3 @@ "use strict"; | ||
exports.createTransactionFromSkeleton = createTransactionFromSkeleton; | ||
exports.createTransactionSkeleton = createTransactionSkeleton; | ||
exports.encodeToAddress = encodeToAddress; | ||
@@ -358,2 +359,31 @@ exports.encodeToConfigAddress = encodeToConfigAddress; | ||
/** | ||
* create a {@link TransactionSkeleton} from a {@link Transaction} | ||
* @example | ||
* ```js | ||
* const rpc = new RPC('localhost:8114') | ||
* const fetcher = (outPoint: OutPoint) => rpc.getLiveCell(outPoint) | ||
* const skeleton = await createTransactionSkeleton({ transaction, fetcher }); | ||
* ``` | ||
*/ | ||
async function createTransactionSkeleton(transaction, fetcher) { | ||
let txSkeleton = TransactionSkeleton(); | ||
txSkeleton = txSkeleton.update("cellDeps", cellDeps => cellDeps.push(...transaction.cellDeps)); | ||
txSkeleton = txSkeleton.update("headerDeps", headerDeps => headerDeps.push(...transaction.headerDeps)); | ||
const inputCells = await Promise.all(transaction.inputs.map(input => fetcher(input.previousOutput))); | ||
txSkeleton = txSkeleton.update("inputs", inputs => inputs.push(...inputCells)); | ||
txSkeleton = txSkeleton.update("inputSinces", inputSinces => transaction.inputs.reduce((map, input, i) => map.set(i, input.since), inputSinces)); | ||
const outputCells = transaction.outputs.map((output, index) => { | ||
var _transaction$outputsD; | ||
return { | ||
cellOutput: output, | ||
data: (_transaction$outputsD = transaction.outputsData[index]) !== null && _transaction$outputsD !== void 0 ? _transaction$outputsD : "0x" | ||
}; | ||
}); | ||
txSkeleton = txSkeleton.update("outputs", outputs => outputs.push(...outputCells)); | ||
txSkeleton = txSkeleton.update("witnesses", witnesses => witnesses.push(...transaction.witnesses)); | ||
return txSkeleton; | ||
} | ||
function sealTransaction(txSkeleton, sealingContents) { | ||
@@ -360,0 +390,0 @@ const tx = createTransactionFromSkeleton(txSkeleton); |
{ | ||
"name": "@ckb-lumos/helpers", | ||
"version": "0.20.0-alpha.0", | ||
"version": "0.20.0-alpha.1", | ||
"description": "Helper functions for working with CKB", | ||
@@ -40,6 +40,6 @@ "author": "Xuejie Xiao <xxuejie@gmail.com>", | ||
"dependencies": { | ||
"@ckb-lumos/base": "^0.20.0-alpha.0", | ||
"@ckb-lumos/bi": "^0.20.0-alpha.0", | ||
"@ckb-lumos/config-manager": "^0.20.0-alpha.0", | ||
"@ckb-lumos/toolkit": "^0.20.0-alpha.0", | ||
"@ckb-lumos/base": "0.20.0-alpha.1", | ||
"@ckb-lumos/bi": "0.20.0-alpha.1", | ||
"@ckb-lumos/config-manager": "0.20.0-alpha.1", | ||
"@ckb-lumos/toolkit": "0.20.0-alpha.1", | ||
"bech32": "^2.0.0", | ||
@@ -62,3 +62,3 @@ "immutable": "^4.0.0-rc.12" | ||
}, | ||
"gitHead": "de4cd6b4a77ee8fce537c84952e4f4abf657e1d0" | ||
"gitHead": "437b641279317a70abc5dd4d0f4790455c13aa61" | ||
} |
@@ -14,2 +14,3 @@ import { bytes } from "@ckb-lumos/codec"; | ||
blockchain, | ||
OutPoint, | ||
} from "@ckb-lumos/base"; | ||
@@ -379,2 +380,56 @@ import { bech32, bech32m } from "bech32"; | ||
type Promisable<T> = Promise<T> | T; | ||
export type LiveCellFetcher = (outPoint: OutPoint) => Promisable<Cell>; | ||
/** | ||
* create a {@link TransactionSkeleton} from a {@link Transaction} | ||
* @example | ||
* ```js | ||
* const rpc = new RPC('localhost:8114') | ||
* const fetcher = (outPoint: OutPoint) => rpc.getLiveCell(outPoint) | ||
* const skeleton = await createTransactionSkeleton({ transaction, fetcher }); | ||
* ``` | ||
*/ | ||
export async function createTransactionSkeleton( | ||
transaction: Transaction, | ||
fetcher: LiveCellFetcher | ||
): Promise<TransactionSkeletonType> { | ||
let txSkeleton = TransactionSkeleton(); | ||
txSkeleton = txSkeleton.update("cellDeps", (cellDeps) => | ||
cellDeps.push(...transaction.cellDeps) | ||
); | ||
txSkeleton = txSkeleton.update("headerDeps", (headerDeps) => | ||
headerDeps.push(...transaction.headerDeps) | ||
); | ||
const inputCells = await Promise.all( | ||
transaction.inputs.map((input) => fetcher(input.previousOutput)) | ||
); | ||
txSkeleton = txSkeleton.update("inputs", (inputs) => | ||
inputs.push(...inputCells) | ||
); | ||
txSkeleton = txSkeleton.update("inputSinces", (inputSinces) => | ||
transaction.inputs.reduce( | ||
(map, input, i) => map.set(i, input.since), | ||
inputSinces | ||
) | ||
); | ||
const outputCells: Array<Cell> = transaction.outputs.map((output, index) => ({ | ||
cellOutput: output, | ||
data: transaction.outputsData[index] ?? "0x", | ||
})); | ||
txSkeleton = txSkeleton.update("outputs", (outputs) => | ||
outputs.push(...outputCells) | ||
); | ||
txSkeleton = txSkeleton.update("witnesses", (witnesses) => | ||
witnesses.push(...transaction.witnesses) | ||
); | ||
return txSkeleton; | ||
} | ||
export function sealTransaction( | ||
@@ -381,0 +436,0 @@ txSkeleton: TransactionSkeletonType, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
98332
1395
+ Added@ckb-lumos/base@0.20.0-alpha.1(transitive)
+ Added@ckb-lumos/bi@0.20.0-alpha.1(transitive)
+ Added@ckb-lumos/codec@0.20.0-alpha.1(transitive)
+ Added@ckb-lumos/config-manager@0.20.0-alpha.1(transitive)
+ Added@ckb-lumos/toolkit@0.20.0-alpha.1(transitive)
- Removed@ckb-lumos/base@0.20.0(transitive)
- Removed@ckb-lumos/bi@0.20.0(transitive)
- Removed@ckb-lumos/codec@0.20.0(transitive)
- Removed@ckb-lumos/config-manager@0.20.0(transitive)
- Removed@ckb-lumos/toolkit@0.20.0(transitive)
Updated@ckb-lumos/bi@0.20.0-alpha.1