-
Notifications
You must be signed in to change notification settings - Fork 53
/
lock.ts
391 lines (350 loc) · 11.6 KB
/
lock.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import { LockScriptInfo, FromInfo, parseFromInfo, common } from "../../src";
import {
Script,
CellProvider,
QueryOptions,
CellCollector as CellCollectorInterface,
Cell,
HexString,
PackedSince,
OutPoint,
values,
WitnessArgs,
utils,
CellDep,
} from "@ckb-lumos/base";
import {
Options,
TransactionSkeletonType,
createTransactionFromSkeleton,
} from "@ckb-lumos/helpers";
import { bytes } from "@ckb-lumos/codec";
import { keccak256 } from "@ckb-lumos/crypto"
import { getConfig, Config, initializeConfig } from "@ckb-lumos/config-manager";
import { Set } from "immutable";
const { ScriptValue } = values;
const { bytify, hexify } = bytes;
// https://github.com/lay2dev/pw-lock/commit/b447c2bb3f855e933e36212b45af4dec92adf705 pw-lock is a lock script which uses secp256k1_keccak256 algorithm.
/* 65-byte zeros in hex */
export const SIGNATURE_PLACEHOLDER =
"0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
function isPwLock(script: Script, config: Config) {
const template = config.SCRIPTS.PW_LOCK!;
return (
script.codeHash === template.CODE_HASH &&
script.hashType === template.HASH_TYPE
);
}
// Help to deal with cell deps, add cell dep to txSkeleton.get("cellDeps") if not exists.
function addCellDep(
txSkeleton: TransactionSkeletonType,
newCellDep: CellDep
): TransactionSkeletonType {
const cellDep = txSkeleton.get("cellDeps").find((cellDep) => {
return (
cellDep.depType === newCellDep.depType &&
new values.OutPointValue(cellDep.outPoint, { validate: false }).equals(
new values.OutPointValue(newCellDep.outPoint, { validate: false })
)
);
});
if (!cellDep) {
txSkeleton = txSkeleton.update("cellDeps", (cellDeps) => {
return cellDeps.push({
outPoint: newCellDep.outPoint,
depType: newCellDep.depType,
});
});
}
return txSkeleton;
}
// Defined a `CellCollector` class that implements `CellCollectorInterface`.
// `collect` method will collect pw-lock cells.
class CellCollector implements CellCollectorInterface {
private cellCollector: CellCollectorInterface;
private config: Config;
private fromScript: Script;
constructor(
fromInfo: FromInfo,
cellProvider: CellProvider,
{
config = undefined,
queryOptions = {},
}: Options & {
queryOptions?: QueryOptions;
}
) {
if (!cellProvider) {
throw new Error(`Cell provider is missing!`);
}
config = config || getConfig();
this.fromScript = parseFromInfo(fromInfo, { config }).fromScript;
this.config = config;
queryOptions = {
...queryOptions,
lock: this.fromScript,
type: queryOptions.type || "empty",
};
this.cellCollector = cellProvider.collector(queryOptions);
}
async *collect(): AsyncGenerator<Cell> {
if (!isPwLock(this.fromScript, this.config)) {
return;
}
for await (const inputCell of this.cellCollector.collect()) {
yield inputCell;
}
}
}
// `setupInputCell` accpet a input and transfer this input to an output.
// Then add the input and output to txSkeleton, it should be noted that the output must be added to the end of txSkeleton.get("outputs").
// And this function should also add required cell deps and witnesses.
async function setupInputCell(
txSkeleton: TransactionSkeletonType,
inputCell: Cell,
_fromInfo?: FromInfo,
{
config = undefined,
defaultWitness = "0x",
since = undefined,
}: Options & {
defaultWitness?: HexString;
since?: PackedSince;
} = {}
): Promise<TransactionSkeletonType> {
config = config || getConfig();
const fromScript = inputCell.cellOutput.lock;
if (!isPwLock(fromScript, config)) {
throw new Error(`Not PW_LOCK input!`);
}
// add inputCell to txSkeleton
txSkeleton = txSkeleton.update("inputs", (inputs) => {
return inputs.push(inputCell);
});
const output: Cell = {
cellOutput: {
capacity: inputCell.cellOutput.capacity,
lock: inputCell.cellOutput.lock,
type: inputCell.cellOutput.type,
},
data: inputCell.data,
};
txSkeleton = txSkeleton.update("outputs", (outputs) => {
return outputs.push(output);
});
if (since) {
txSkeleton = txSkeleton.update("inputSinces", (inputSinces) => {
return inputSinces.set(txSkeleton.get("inputs").size - 1, since);
});
}
txSkeleton = txSkeleton.update("witnesses", (witnesses) => {
return witnesses.push(defaultWitness);
});
const template = config.SCRIPTS.PW_LOCK;
if (!template) {
throw new Error(`PW_LOCK script not defined in config!`);
}
const scriptOutPoint: OutPoint = {
txHash: template.TX_HASH,
index: template.INDEX,
};
// add cell dep
txSkeleton = addCellDep(txSkeleton, {
outPoint: scriptOutPoint,
depType: template.DEP_TYPE,
});
// add witness
/*
* Modify the skeleton, so the first witness of the fromAddress script group
* has a WitnessArgs construct with 65-byte zero filled values. While this
* is not required, it helps in transaction fee estimation.
*/
const firstIndex = txSkeleton
.get("inputs")
.findIndex((input) =>
new ScriptValue(input.cellOutput.lock, { validate: false }).equals(
new ScriptValue(fromScript, { validate: false })
)
);
if (firstIndex !== -1) {
while (firstIndex >= txSkeleton.get("witnesses").size) {
txSkeleton = txSkeleton.update("witnesses", (witnesses) =>
witnesses.push("0x")
);
}
let witness: string = txSkeleton.get("witnesses").get(firstIndex)!;
const newWitnessArgs: WitnessArgs = {
/* 65-byte zeros in hex */
lock: SIGNATURE_PLACEHOLDER,
};
if (witness !== "0x") {
const witnessArgs = new core.WitnessArgs(new Reader(witness));
const lock = witnessArgs.getLock();
if (
lock.hasValue() &&
new Reader(lock.value().raw()).serializeJson() !== newWitnessArgs.lock
) {
throw new Error(
"Lock field in first witness is set aside for signature!"
);
}
const inputType = witnessArgs.getInputType();
if (inputType.hasValue()) {
newWitnessArgs.inputType = new Reader(
inputType.value().raw()
).serializeJson();
}
const outputType = witnessArgs.getOutputType();
if (outputType.hasValue()) {
newWitnessArgs.outputType = new Reader(
outputType.value().raw()
).serializeJson();
}
}
witness = new Reader(
core.SerializeWitnessArgs(
normalizers.NormalizeWitnessArgs(newWitnessArgs)
)
).serializeJson();
txSkeleton = txSkeleton.update("witnesses", (witnesses) =>
witnesses.set(firstIndex, witness)
);
}
return txSkeleton;
}
// It's a secp256k1_keccak256 sighash all lock script, so we need a keccak256 hash method.
class Keccak256Hasher {
private hasher: ReturnType<typeof keccak256.create>;
constructor() {
this.hasher = keccak256.create();
}
update(data: string | ArrayBuffer | Reader): this {
const reader = new Reader(data);
const array = bytify(reader.serializeJson());
this.hasher.update(array);
return this;
}
digestReader(): Reader {
const hex = hexify(this.hasher.digest())
return new Reader(hex);
}
digestHex() {
return this.digestReader().serializeJson();
}
}
function hashWitness(hasher: any, witness: HexString): void {
const lengthBuffer = new ArrayBuffer(8);
const view = new DataView(lengthBuffer);
const witnessReader = new Reader(witness);
view.setBigUint64(0, BigInt(witnessReader.length()), true);
hasher.update(view.buffer);
hasher.update(witnessReader);
}
// This function help to generate signing messages from pw-lock inputs.
function prepareSigningEntries(
txSkeleton: TransactionSkeletonType,
{ config = undefined }: Options = {}
): TransactionSkeletonType {
config = config || getConfig();
const template = config.SCRIPTS.PW_LOCK;
if (!template) {
throw new Error(`Provided config does not have PW_LOCK script setup!`);
}
let processedArgs = Set<string>();
const tx = createTransactionFromSkeleton(txSkeleton);
const txHash = utils
.ckbHash(
core.SerializeRawTransaction(normalizers.NormalizeRawTransaction(tx))
)
.serializeJson();
const inputs = txSkeleton.get("inputs");
const witnesses = txSkeleton.get("witnesses");
let signingEntries = txSkeleton.get("signingEntries");
for (let i = 0; i < inputs.size; i++) {
const input = inputs.get(i)!;
if (
template.CODE_HASH === input.cellOutput.lock.codeHash &&
template.HASH_TYPE === input.cellOutput.lock.hashType &&
!processedArgs.has(input.cellOutput.lock.args)
) {
processedArgs = processedArgs.add(input.cellOutput.lock.args);
const lockValue = new values.ScriptValue(input.cellOutput.lock, {
validate: false,
});
const hasher = new Keccak256Hasher();
hasher.update(txHash);
if (i >= witnesses.size) {
throw new Error(
`The first witness in the script group starting at input index ${i} does not exist, maybe some other part has invalidly tampered the transaction?`
);
}
hashWitness(hasher, witnesses.get(i)!);
for (let j = i + 1; j < inputs.size && j < witnesses.size; j++) {
const otherInput = inputs.get(j)!;
if (
lockValue.equals(
new values.ScriptValue(otherInput.cellOutput.lock, {
validate: false,
})
)
) {
hashWitness(hasher, witnesses.get(j)!);
}
}
for (let j = inputs.size; j < witnesses.size; j++) {
hashWitness(hasher, witnesses.get(j)!);
}
const hh = new Keccak256Hasher();
// This magic number is from https://github.com/lay2dev/pw-lock/blob/b447c2bb3f855e933e36212b45af4dec92adf705/c/secp256k1_keccak256_lock.h#L523
hh.update("0x19457468657265756d205369676e6564204d6573736167653a0a3332");
hh.update(hasher.digestHex());
const signingEntry = {
type: "witness_args_lock",
index: i,
message: hh.digestHex(),
};
signingEntries = signingEntries.push(signingEntry);
}
}
txSkeleton = txSkeleton.set("signingEntries", signingEntries);
return txSkeleton;
}
export async function main() {
// set config
// deploy your own pw-lock and update config.json
process.env.LUMOS_CONFIG_FILE = __dirname + "/config.json";
initializeConfig();
const config = getConfig();
const template = config.SCRIPTS.PW_LOCK!;
// Get a lockScriptInfo and register to common
// `setupOutputCell` is an optional method, if you only want to add a to output, you can ignore this.
// `anyone_can_pay` script shows how to use `setupOutputCell`.
const lockScriptInfo: LockScriptInfo = {
codeHash: template.CODE_HASH,
hashType: template.HASH_TYPE,
lockScriptInfo: {
CellCollector,
setupInputCell,
prepareSigningEntries,
},
};
common.registerCustomLockScriptInfos([lockScriptInfo]);
// Then you can use functions like `common.setupInputCell` and `common.transfer` as other lock scripts.
// Flowing is a example to show how to do.
// let txSkeleton = TransactionSkeleton({ cellProvider: indexer })
// const fromScript: Script = {
// codeHash: template.CODE_HASH,
// hashType: template.HASH_TYPE,
// args: pwLockArgs,
// }
// const fromAddress = generateAddress(fromScript)
// const toAddress = "ckt1qyqrdsefa43s6m882pcj53m4gdnj4k440axqswmu83"
// txSkeleton = await common.transfer(
// txSkeleton,
// [fromAddress],
// toAddress,
// BigInt(200*10**8),
// )
// txSkeleton = common.prepareSigningEntries(txSkeleton)
// Then sign messages by key pair.
}