-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathDeploy.ts
542 lines (491 loc) · 17 KB
/
Deploy.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
import { jsonArrayMember, jsonMember, jsonObject, TypedJSON } from 'typedjson';
import { concat } from '@ethersproject/bytes';
import { Hash } from './key';
import { HexBytes } from './HexBytes';
import { PublicKey, PrivateKey } from './keypair';
import { Duration, Timestamp } from './Time';
import { Approval, Transaction } from './Transaction';
import {
TransactionEntryPoint,
TransactionEntryPointEnum
} from './TransactionEntryPoint';
import { InitiatorAddr } from './InitiatorAddr';
import { PaymentLimitedMode, PricingMode } from './PricingMode';
import { TransactionTarget } from './TransactionTarget';
import { TransactionScheduling } from './TransactionScheduling';
import { ExecutableDeployItem } from './ExecutableDeployItem';
import {
byteHash,
toBytesString,
toBytesU32,
toBytesU64
} from './ByteConverters';
/**
* Represents the header of a deploy in the blockchain.
* The header contains metadata such as the account initiating the deploy, the body hash, gas price, timestamp, TTL, and dependencies.
*/
@jsonObject
export class DeployHeader {
/**
* The public key of the account initiating the deploy.
* This key is used to verify the identity of the account making the deploy request.
*/
@jsonMember({
constructor: PublicKey,
deserializer: json => {
if (!json) return;
return PublicKey.fromJSON(json);
},
serializer: value => {
if (!value) return;
return value.toJSON();
}
})
public account?: PublicKey;
/**
* The hash of the body of the deploy, which is used to verify the contents of the deploy.
* The body contains the session logic and payment logic of the deploy.
*/
@jsonMember({
name: 'body_hash',
constructor: Hash,
deserializer: json => {
if (!json) return;
return Hash.fromJSON(json);
},
serializer: value => {
if (!value) return;
return value.toJSON();
}
})
public bodyHash?: Hash;
/**
* The name of the blockchain chain that the deploy is associated with.
* This helps prevent the deploy from being accidentally or maliciously included in a different chain.
*/
@jsonMember({ name: 'chain_name', constructor: String })
public chainName = '';
/**
* A list of other deploys that must be executed before this one.
* This ensures dependencies are executed in the correct order.
*/
@jsonArrayMember(Hash, {
name: 'dependencies',
serializer: (value: Hash[]) => value.map(it => it.toJSON()),
deserializer: (json: any) => json.map((it: string) => Hash.fromJSON(it))
})
public dependencies: Hash[] = [];
/**
* The price of gas for executing the deploy.
* Gas is used to pay for the computational resources required to process the deploy.
*/
@jsonMember({ name: 'gas_price', constructor: Number })
public gasPrice = 1;
/**
* The timestamp when the deploy was created.
* This timestamp is used to determine the deploy's position in time.
*/
@jsonMember({
constructor: Timestamp,
deserializer: json => Timestamp.fromJSON(json),
serializer: value => value.toJSON()
})
public timestamp: Timestamp = new Timestamp(new Date());
/**
* The time-to-live (TTL) for the deploy, after which it will expire if not executed.
* The default TTL is 30 minutes.
*/
@jsonMember({
constructor: Duration,
deserializer: json => Duration.fromJSON(json),
serializer: value => value.toJSON()
})
public ttl: Duration = new Duration(DEFAULT_DEPLOY_TTL);
/**
* Constructs a `DeployHeader` instance with the specified parameters.
* @param chainName The name of the blockchain chain.
* @param dependencies A list of deploys that must be executed before this one.
* @param gasPrice The gas price for the deploy.
* @param timestamp The timestamp when the deploy is created.
* @param ttl The TTL for the deploy.
* @param account The public key of the account initiating the deploy (optional).
* @param bodyHash The hash of the body of the deploy (optional).
*/
constructor(
chainName = '',
dependencies: Hash[] = [],
gasPrice = 1,
timestamp: Timestamp = new Timestamp(new Date()),
ttl: Duration = new Duration(DEFAULT_DEPLOY_TTL),
account?: PublicKey,
bodyHash?: Hash
) {
this.chainName = chainName;
this.dependencies = dependencies;
this.gasPrice = gasPrice;
this.timestamp = timestamp;
this.ttl = ttl;
this.account = account;
this.bodyHash = bodyHash;
}
/**
* Converts the deploy header to a byte array for transmission or storage.
* @returns A `Uint8Array` representing the deploy header in byte format.
*/
public toBytes(): Uint8Array {
const dependenciesBytes = this.dependencies.map(e => e.toBytes());
dependenciesBytes.splice(0, 0, toBytesU32(this.dependencies?.length));
return concat([
this.account!.bytes(),
toBytesU64(Date.parse(this.timestamp.toJSON())),
toBytesU64(this.ttl.duration),
toBytesU64(this.gasPrice),
this.bodyHash!.toBytes(),
concat(dependenciesBytes),
toBytesString(this.chainName)
]);
}
/**
* Returns a default `DeployHeader` instance with default values.
* @returns A `DeployHeader` instance with default values.
*/
public static default(): DeployHeader {
return new DeployHeader();
}
}
/**
* Represents a deploy in the blockchain, including the header, payment, session, and approvals.
* A `Deploy` object is used to package the logic for executing a contract, payment, or transfer on the blockchain.
*/
@jsonObject
export class Deploy {
/**
* A list of approvals, including signatures from accounts that have approved the deploy.
*/
@jsonArrayMember(() => Approval)
public approvals: Approval[] = [];
/**
* The unique hash that identifies this deploy. This hash is used to verify the integrity of the deploy.
*/
@jsonMember({
deserializer: json => Hash.fromJSON(json),
serializer: value => value.toJSON()
})
public hash: Hash;
/**
* The header of the deploy, which contains metadata such as the account, gas price, timestamp, and TTL.
*/
@jsonMember({ constructor: DeployHeader })
public header: DeployHeader;
/**
* The executable item representing the payment logic of the deploy.
*/
@jsonMember({ constructor: ExecutableDeployItem })
public payment: ExecutableDeployItem;
/**
* The executable item representing the session logic of the deploy.
*/
@jsonMember({ constructor: ExecutableDeployItem })
public session: ExecutableDeployItem;
/**
* Constructs a `Deploy` object.
*
* @param hash The deploy hash identifying this deploy.
* @param header The deploy header containing metadata.
* @param payment The executable deploy item representing the payment logic.
* @param session The executable deploy item representing the session logic.
* @param approvals An array of signatures and accounts who have approved this deploy.
*/
constructor(
hash: Hash,
header: DeployHeader,
payment: ExecutableDeployItem,
session: ExecutableDeployItem,
approvals: Approval[]
) {
this.approvals = approvals;
this.session = session;
this.payment = payment;
this.header = header;
this.hash = hash;
}
/**
* Validates the deploy by checking its body hash, deploy hash, and approval signatures.
*
* @returns `true` if the deploy is valid, otherwise throws an error.
*/
public validate(): boolean {
const paymentBytes = this.payment.bytes();
const sessionBytes = this.session.bytes();
const concatenatedBytes = concat([paymentBytes, sessionBytes]);
const calculatedBodyHash = new Hash(byteHash(concatenatedBytes));
const headerBytes = this.header.toBytes();
const calculatedHash = new Hash(byteHash(headerBytes));
if (
!this.header.bodyHash?.equals(calculatedBodyHash) ||
!this.hash.equals(calculatedHash)
) {
throw new Error('Invalid deploy hash or body hash');
}
this.approvals.forEach(approval => {
if (
!approval.signer.verifySignature(
this.hash.toBytes(),
approval.signature.bytes
)
) {
throw new Error('Invalid approval signature');
}
});
return true;
}
/**
* Signs the deploy with a given private key and adds the signature to the approvals list.
*
* @param keys The private key used to sign the deploy.
*/
public async sign(keys: PrivateKey): Promise<void> {
const signatureBytes = await keys.signAndAddAlgorithmBytes(
this.hash.toBytes()
);
const signature = new HexBytes(signatureBytes);
this.approvals.push(new Approval(keys.publicKey, signature));
}
/**
* Converts the deploy object into a byte array for transmission or storage.
*
* @returns A `Uint8Array` representing the deploy in byte format.
*/
toBytes(): Uint8Array {
return concat([
this.header.toBytes(),
this.hash.toBytes(),
concat([this.payment.bytes(), this.session.bytes()]),
serializeApprovals(this.approvals)
]);
}
/**
* Sets an already generated signature for the deploy.
*
* @param deploy The deploy instance.
* @param signature The Ed25519 or Secp256K1 signature.
* @param publicKey The public key used to generate the signature.
* @returns A new `Deploy` instance with the added signature.
*/
public static setSignature(
deploy: Deploy,
signature: Uint8Array,
publicKey: PublicKey
): Deploy {
const hex = new HexBytes(signature);
deploy.approvals.push(new Approval(publicKey, hex));
return deploy;
}
/**
* Creates a new `Deploy` instance with the provided parameters.
*
* @param hash The deploy hash identifying this deploy.
* @param header The deploy header.
* @param payment The executable deploy item for the payment logic.
* @param session The executable deploy item for the session logic.
* @param approvals An array of approvals for the deploy.
* @returns A new `Deploy` object.
*/
public static createNew(
hash: Hash,
header: DeployHeader,
payment: ExecutableDeployItem,
session: ExecutableDeployItem,
approvals: Approval[] = []
): Deploy {
return new Deploy(hash, header, payment, session, approvals);
}
/**
* Creates a `Deploy` instance from the deploy header and session/payment logic.
*
* @param deployHeader The deploy header.
* @param payment The payment logic of the deploy.
* @param session The session logic of the deploy.
* @returns A new `Deploy` object.
*/
public static makeDeploy(
deployHeader: DeployHeader,
payment: ExecutableDeployItem,
session: ExecutableDeployItem
): Deploy {
const paymentBytes = payment.bytes();
const sessionBytes = session.bytes();
const serializedBody = concat([paymentBytes, sessionBytes]);
deployHeader.bodyHash = new Hash(byteHash(serializedBody));
const deployHash = new Hash(byteHash(deployHeader.toBytes()));
return Deploy.createNew(deployHash, deployHeader, payment, session);
}
/**
* Converts the `Deploy` into a `Transaction` object.
* This method creates a transaction based on the deploy, including its payment and session logic.
*
* @param deploy The deploy object.
* @returns A new `Transaction` object created from the deploy.
*/
static newTransactionFromDeploy(deploy: Deploy): Transaction {
let paymentAmount = 0;
let transactionEntryPoint: TransactionEntryPoint;
if (deploy.session.transfer) {
transactionEntryPoint = new TransactionEntryPoint(
TransactionEntryPointEnum.Transfer
);
} else if (deploy.session.moduleBytes) {
transactionEntryPoint = new TransactionEntryPoint(
TransactionEntryPointEnum.Call
);
} else {
let entryPoint = '';
if (deploy.session.storedContractByHash) {
entryPoint = deploy.session.storedContractByHash.entryPoint;
} else if (deploy.session.storedContractByName) {
entryPoint = deploy.session.storedContractByName.entryPoint;
} else if (deploy.session.storedVersionedContractByHash) {
entryPoint = deploy.session.storedVersionedContractByHash.entryPoint;
} else if (deploy.session.storedVersionedContractByName) {
entryPoint = deploy.session.storedVersionedContractByName.entryPoint;
}
transactionEntryPoint = new TransactionEntryPoint(
TransactionEntryPointEnum.Custom,
entryPoint
);
}
const amountArgument = deploy.payment.getArgs();
if (amountArgument) {
const parsed = amountArgument.args.get('amount');
if (parsed) {
paymentAmount = parseInt(parsed.toString(), 10) || 0;
}
}
const standardPayment = paymentAmount === 0 && !deploy.payment.moduleBytes;
const pricingMode = new PricingMode();
const paymentLimitedMode = new PaymentLimitedMode();
paymentLimitedMode.gasPriceTolerance = 1;
paymentLimitedMode.paymentAmount = paymentAmount;
paymentLimitedMode.standardPayment = standardPayment;
return new Transaction(
deploy.hash,
deploy.header.chainName,
deploy.header.timestamp,
deploy.header.ttl,
new InitiatorAddr(deploy.header.account),
pricingMode,
deploy.session.getArgs(),
TransactionTarget.newTransactionTargetFromSession(deploy.session),
transactionEntryPoint,
new TransactionScheduling({ standard: {} }),
deploy.approvals,
undefined,
deploy
);
}
/**
* Converts a JSON representation of a deploy to a `Deploy` object.
*
* @param json The JSON representation of a `Deploy`.
* @returns A `Deploy` object if successful, or throws an error if parsing fails.
*/
public static fromJSON(json: any): Deploy {
let deploy: Deploy | undefined;
try {
const data: Record<string, any> =
typeof json === 'string' ? JSON.parse(json) : json;
const deployJson: Record<string, any> | null =
data.deploy ?? data.Deploy ?? data?.transaction?.Deploy ?? data ?? null;
if (!(deployJson?.hash && deployJson?.header?.account)) {
throw new Error("The JSON can't be parsed as a Deploy.");
}
const serializer = new TypedJSON(Deploy);
deploy = serializer.parse(deployJson);
if (!deploy) {
throw new Error("The JSON can't be parsed as a Deploy.");
}
} catch (e) {
throw new Error(`Serialization error: ${e.message}`);
}
const isDeployValid = deploy.validate();
if (!isDeployValid) {
throw new Error(`Deploy validation failed`);
}
return deploy;
}
/**
* Converts the `Deploy` object into a JSON representation.
*
* @param deploy The deploy object to convert to JSON.
* @returns A JSON representation of the deploy.
*/
public static toJson = (deploy: Deploy) => {
const serializer = new TypedJSON(Deploy);
return serializer.toPlainJson(deploy);
};
/**
* Identifies whether this `Deploy` represents a transfer of CSPR.
*
* @returns `true` if the deploy is a transfer, otherwise `false`.
*/
public isTransfer(): boolean {
return this.session.isTransfer();
}
/**
* Identifies whether this `Deploy` represents a standard payment, like a gas payment.
*
* @returns `true` if the deploy is a standard payment, otherwise `false`.
*/
public isStandardPayment(): boolean {
if (this.payment.isModuleBytes()) {
return this.payment.asModuleBytes()?.moduleBytes.length === 0;
}
return false;
}
/**
* Gets the byte-size of a deploy
* @param deploy The `Deploy` for which to calculate the size
* @returns The size of the `Deploy` in its serialized representation
*/
public static getDeploySizeInBytes = (deploy: Deploy): number => {
const hashSize = deploy.hash.toBytes().length;
const bodySize = concat([deploy.payment.bytes(), deploy.session.bytes()])
.length;
const headerSize = deploy.header.toBytes().length;
const approvalsSize = deploy.approvals
.map(approval => {
return (
(approval.signature.bytes.length + approval.signer.bytes().length) / 2
);
})
.reduce((a, b) => a + b, 0);
return hashSize + headerSize + bodySize + approvalsSize;
};
}
/**
* Serializes an array of `Approval`s into a `Uint8Array` typed byte array.
* This is used to store or transmit the approvals associated with a deploy.
*
* @param approvals An array of `Approval` objects that represent signatures from accounts that have approved the deploy.
* @returns A `Uint8Array` typed byte array that can be deserialized back into an array of `Approval` objects.
*
* @example
* const approvals = [new Approval(publicKey, signature)];
* const serializedApprovals = serializeApprovals(approvals);
*/
export const serializeApprovals = (approvals: Approval[]): Uint8Array => {
const len = toBytesU32(approvals.length);
const bytes = concat(
approvals.map(approval => {
return concat([
Uint8Array.from(Buffer.from(approval.signer.toString(), 'hex')),
Uint8Array.from(Buffer.from(approval.signature.toString(), 'hex'))
]);
})
);
return concat([len, bytes]);
};
/**
* Default TTL value used for deploys (30 minutes).
*/
export const DEFAULT_DEPLOY_TTL = 1800000;