-
Notifications
You must be signed in to change notification settings - Fork 7
/
cell.ts
177 lines (157 loc) · 5.36 KB
/
cell.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { bytes } from '@ckb-lumos/codec';
import { Config } from '@ckb-lumos/config-manager';
import { common, FromInfo } from '@ckb-lumos/common-scripts';
import { Hash, OutPoint, PackedSince, Script } from '@ckb-lumos/base';
import { Cell, helpers, HexString, Indexer, RPC } from '@ckb-lumos/lumos';
import { CKBComponents } from '@ckb-lumos/rpc/lib/types/api';
import { ScriptId } from '../codec';
import { isScriptValueEquals } from './script';
export async function getCellByLock(props: { lock: Script; indexer: Indexer; has_type: boolean }) {
const collector = props.indexer.collector({
lock: props.lock,
});
for await (const cell of collector.collect()) {
if (props.has_type && cell.cellOutput.type) {
return cell;
} else if (!props.has_type && !cell.cellOutput.type) return cell;
}
return void 0;
}
/**
* Find and return the first cell of target type script from CKB Indexer.
*/
export async function getCellByType(props: { type: Script; indexer: Indexer }) {
const collector = props.indexer.collector({
type: props.type,
});
for await (const cell of collector.collect()) {
return cell;
}
return void 0;
}
/**
* A wrapper function, to get a Cell structure from RPC.getLiveCell() method.
*/
export async function getCellWithStatusByOutPoint(props: { outPoint: OutPoint; rpc: RPC }): Promise<{
cell?: Cell;
status: CKBComponents.CellStatus;
}> {
const liveCell = await props.rpc.getLiveCell(props.outPoint, true);
const cell: Cell | undefined = liveCell.cell
? {
cellOutput: liveCell.cell.output,
data: liveCell.cell.data.content,
outPoint: props.outPoint,
}
: void 0;
return {
cell,
status: liveCell.status,
};
}
/**
* Inject a cell to Transaction.inputs, and to Transaction.outputs if needed.
* The function also adds needed/supported witness placeholders and cellDeps.
*/
export async function setupCell(props: {
txSkeleton: helpers.TransactionSkeletonType;
input: Cell;
fromInfo?: FromInfo;
addOutput?: boolean;
updateOutput?(cell: Cell): Cell;
defaultWitness?: HexString;
updateWitness?: HexString | ((witness: HexString) => HexString);
since?: PackedSince;
config?: Config;
}): Promise<{
txSkeleton: helpers.TransactionSkeletonType;
inputIndex: number;
outputIndex: number;
}> {
// Env
const addOutput = props.addOutput ?? false;
// Get TransactionSkeleton
let txSkeleton = props.txSkeleton;
// Add target cell to inputs and outputs,
// the function also handles witnesses and cellDeps
txSkeleton = await common.setupInputCell(txSkeleton, props.input, props.fromInfo, {
defaultWitness: props.defaultWitness,
config: props.config,
since: props.since,
});
// Remove it from outputs if not needed
if (!addOutput) {
txSkeleton = txSkeleton.update('outputs', (outputs) => {
return outputs.remove(outputs.size - 1);
});
}
// Indices
const inputIndex = txSkeleton.get('inputs').size - 1;
const outputIndex = addOutput ? txSkeleton.get('outputs').size - 1 : -1;
// If added output, and need to update the added output
if (addOutput && props.updateOutput instanceof Function) {
txSkeleton = txSkeleton.update('outputs', (outputs) => {
const output = outputs.last();
if (!output) {
throw new Error('Cannot update output because the added output could not be found');
}
return outputs.set(outputIndex, props.updateOutput!(output));
});
}
// If required to update the resulting witness placeholder
if (props.updateWitness) {
txSkeleton = txSkeleton.update('witnesses', (witnesses) => {
if (props.updateWitness instanceof Function) {
const witness = witnesses.get(inputIndex);
if (!witness) {
throw new Error(`Cannot update Transaction.witnesses[${inputIndex}] because it's undefined`);
}
return witnesses.set(inputIndex, props.updateWitness(witness));
}
if (typeof props.updateWitness === 'string') {
return witnesses.set(inputIndex, props.updateWitness);
}
return witnesses;
});
}
return {
txSkeleton,
inputIndex,
outputIndex,
};
}
/**
* Group cells by TypeScriptID.
*/
export function groupCells(cells: Cell[]): Record<Hash | 'null', { index: number; cell: Cell }[]> {
const groups: Record<Hash | 'null', { index: number; cell: Cell }[]> = {};
for (let i = 0; i < cells.length; i++) {
const cell = cells[i];
const scriptIdHash = cell.cellOutput.type ? bytes.hexify(ScriptId.pack(cell.cellOutput.type)) : 'null';
if (groups[scriptIdHash] === void 0) {
groups[scriptIdHash] = [];
}
groups[scriptIdHash].push({
index: i,
cell,
});
}
return groups;
}
export function findCellIndexByScriptFromTransactionSkeleton(props: {
txSkeleton: helpers.TransactionSkeletonType;
source: 'inputs' | 'outputs';
scriptName: 'lock' | 'type';
script: Script;
}): number {
if (!['inputs', 'outputs'].includes(props.source)) {
throw new Error('Can only find cell from the following source: "inputs" | "outputs"');
}
if (!['lock', 'type'].includes(props.scriptName)) {
throw new Error('Can only find cell from the following scriptType: "lock" | "type"');
}
const list = props.txSkeleton.get(props.source);
return list.findIndex((cell) => {
return !!cell.cellOutput[props.scriptName] && isScriptValueEquals(cell.cellOutput[props.scriptName]!, props.script);
});
}