-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Signer.ts
798 lines (737 loc) · 23.7 KB
/
Signer.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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
/* eslint-disable no-param-reassign, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-assignment */
import { sha256 } from "@noble/hashes/sha256";
import * as secp from "@noble/secp256k1";
import { Provider } from "./Provider";
import { Transaction } from "./Transaction";
import {
TransactionJson,
TransactionJsonWait,
BlockJson,
RecoverPublicKeyOptions,
Abi,
TypeField,
TransactionReceipt,
SendTransactionOptions,
ResourceCreditsOptions,
} from "./interface";
import {
bitcoinAddress,
bitcoinDecode,
bitcoinEncode,
btypeDecode,
calculateMerkleRoot,
decodeBase64url,
encodeBase64url,
toHexString,
toUint8Array,
} from "./utils";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import { koinos } from "./protoModules/protocol-proto.js";
export interface SignerInterface {
provider?: Provider;
getAddress: (compressed?: boolean) => string;
signHash: (hash: Uint8Array) => Promise<Uint8Array>;
signMessage: (message: string | Uint8Array) => Promise<Uint8Array>;
// Transaction
signTransaction: (
transaction: TransactionJson | TransactionJsonWait,
abis?: Record<string, Abi>
) => Promise<TransactionJson>;
sendTransaction: (
transaction: TransactionJson | TransactionJsonWait,
options?: SendTransactionOptions
) => Promise<{
receipt: TransactionReceipt;
transaction: TransactionJsonWait;
}>;
// Block
prepareBlock: (block: BlockJson) => Promise<BlockJson>;
signBlock: (block: BlockJson) => Promise<BlockJson>;
}
const btypeBlockHeader: TypeField["subtypes"] = {
previous: { type: "bytes", btype: "BLOCK_ID" },
height: { type: "uint64" },
timestamp: { type: "uint64" },
previous_state_merkle_root: { type: "bytes" },
transaction_merkle_root: { type: "bytes" },
signer: { type: "bytes", btype: "ADDRESS" },
};
const btypeTransactionHeader: TypeField["subtypes"] = {
chain_id: { type: "bytes" },
rc_limit: { type: "uint64" },
nonce: { type: "bytes" },
operation_merkle_root: { type: "bytes" },
payer: { type: "bytes", btype: "ADDRESS" },
payee: { type: "bytes", btype: "ADDRESS" },
};
/**
* The Signer Class contains the private key needed to sign transactions.
* It can be created using the seed, wif, or private key
*
* @example
* using private key as hex string
* ```ts
* var privateKey = "ec8601a24f81decd57f4b611b5ac6eb801cb3780bb02c0f9cdfe9d09daaddf9c";
* var signer = new Signer({ privateKey });
* ```
* <br>
*
* using private key as Uint8Array
* ```ts
* var buffer = new Uint8Array([
* 236, 134, 1, 162, 79, 129, 222, 205,
* 87, 244, 182, 17, 181, 172, 110, 184,
* 1, 203, 55, 128, 187, 2, 192, 249,
* 205, 254, 157, 9, 218, 173, 223, 156
* ]);
* var signer = new Signer({ privateKey: buffer });
* ```
*
* <br>
*
* using private key as bigint
* ```ts
* var privateKey = 106982601049961974618234078204952280507266494766432547312316920283818886029212n;
* var signer = new Signer({ privateKey });
* ```
*
* <br>
*
* using the seed
* ```ts
* var signer = Signer.fromSeed("my seed");
* ```
*
* <br>
*
* using private key in WIF format
* ```ts
* var signer = Signer.fromWif("L59UtJcTdNBnrH2QSBA5beSUhRufRu3g6tScDTite6Msuj7U93tM");
* ```
*
* <br>
*
* defining a provider
* ```ts
* var provider = new Provider(["https://example.com/jsonrpc"]);
* var privateKey = "ec8601a24f81decd57f4b611b5ac6eb801cb3780bb02c0f9cdfe9d09daaddf9c";
* var signer = new Signer({ privateKey, provider });
* ```
*/
export class Signer implements SignerInterface {
/**
* Boolean determining if the public/private key
* is using the compressed format
*/
compressed: boolean;
private privateKey: string | number | bigint | Uint8Array;
publicKey: string | Uint8Array;
/**
* Account address
*/
address: string;
/**
* Provider to connect with the blockchain
*/
provider?: Provider;
/**
* Options to apply when sending a transaction.
* By default broadcast is true and the other fields
* are undefined
*/
sendOptions?: SendTransactionOptions;
/**
* Options related to the estimation of the rcLimit.
* By default the estimation is enabled and increased
* by 10%.
*
* @example
* This code estimates the mana by multiplying the rc_used
* by 2.
* ```ts
* const signer = new Signer({
* privateKey,
* provider,
* rcOptions: {
* estimateRc: true,
* adjustRcLimit: (receipt) => 2 * Number(receipt.rc_used),
* },
* });
*
* // you can also update rcOptions
* signer.rcOptions = {
* estimateRc: true,
* adjustRcLimit: (receipt) => 2 * Number(receipt.rc_used),
* }
* ```
*/
rcOptions: ResourceCreditsOptions;
/**
* The constructor receives de private key as hexstring, bigint or Uint8Array.
* See also the functions [[Signer.fromWif]] and [[Signer.fromSeed]]
* to create the signer from the WIF or Seed respectively.
*
* @param privateKey - Private key as hexstring, bigint or Uint8Array
* @param compressed - compressed format is true by default
* @param provider - provider to connect with the blockchain
* @param sendOptions - Send options
* @param rcOptions - options for mana estimation
* @example
* ```ts
* const privateKey = "ec8601a24f81decd57f4b611b5ac6eb801cb3780bb02c0f9cdfe9d09daaddf9c";
* cons signer = new Signer({ privateKey });
* console.log(signer.getAddress());
* // 1MbL6mG8ASAvSYdoMnGUfG3ZXkmQ2dpL5b
* ```
*
* By default the mana is estimated as 110% the mana used. This
* estimation is computed using the "broadcast:false" option.
* For instance, if the transaction consumes 1 mana, the rc_limit
* will be set to 1.1 mana.
*
* You can also adjust the rc limit.
* @example
* ```ts
* const privateKey = "ec8601a24f81decd57f4b611b5ac6eb801cb3780bb02c0f9cdfe9d09daaddf9c";
* cons signer = new Signer({
* privateKey,
* provider,
* rcOptions: {
* estimateRc: true,
* // use 2 times rc_used as rc_limit
* adjustRcLimit: (r) => 2 * Number(r.rc_used),
* },
* });
* ```
*
* The rpc node must be highly trusted because the transaction
* is signed during the estimation of the mana. You can also
* disable the mana estimation:
* @example
* ```ts
* const privateKey = "ec8601a24f81decd57f4b611b5ac6eb801cb3780bb02c0f9cdfe9d09daaddf9c";
* cons signer = new Signer({
* privateKey,
* provider,
* rcOptions: { estimateRc: false },
* });
* ```
*/
constructor(c: {
privateKey: string | number | bigint | Uint8Array;
compressed?: boolean;
provider?: Provider;
sendOptions?: SendTransactionOptions;
rcOptions?: ResourceCreditsOptions;
}) {
this.compressed = typeof c.compressed === "undefined" ? true : c.compressed;
this.privateKey = c.privateKey;
this.provider = c.provider;
if (typeof c.privateKey === "string") {
this.publicKey = secp.getPublicKey(c.privateKey, this.compressed);
this.address = bitcoinAddress(this.publicKey);
} else {
this.publicKey = secp.getPublicKey(c.privateKey, this.compressed);
this.address = bitcoinAddress(this.publicKey);
}
this.sendOptions = {
broadcast: true,
...c.sendOptions,
};
this.rcOptions = c.rcOptions ?? {
estimateRc: true,
adjustRcLimit: (receipt) =>
Promise.resolve(
Math.min(
Number(receipt.max_payer_rc),
Math.floor(1.1 * Number(receipt.rc_used))
)
),
};
}
/**
* Function to import a private key from the WIF
* @param wif - Private key in WIF format
* @example
* ```ts
* const signer = Signer.fromWif("L59UtJcTdNBnrH2QSBA5beSUhRufRu3g6tScDTite6Msuj7U93tM")
* console.log(signer.getAddress());
* // 1MbL6mG8ASAvSYdoMnGUfG3ZXkmQ2dpL5b
* ```
* @returns Signer object
*/
static fromWif(wif: string, compressed = true): Signer {
const comp = compressed === undefined ? wif[0] !== "5" : compressed;
const privateKey = bitcoinDecode(wif);
return new Signer({
privateKey: toHexString(privateKey),
compressed: comp,
});
}
/**
* Function to import a private key from the seed
* @param seed - Seed words
* @param compressed -
* @example
* ```ts
* const signer = Signer.fromSeed("my seed");
* console.log(signer.getAddress());
* // 1BqtgWBcqm9cSZ97avLGZGJdgso7wx6pCA
* ```
* @returns Signer object
*/
static fromSeed(seed: string, compressed = true): Signer {
const privateKey = sha256(seed);
return new Signer({ privateKey, compressed });
}
/**
* @param compressed - determines if the address should be
* derived from the compressed public key (default) or the public key
* @returns Signer address
*/
getAddress(compressed = true): string {
if (typeof this.privateKey === "string") {
const publicKey = secp.getPublicKey(this.privateKey, compressed);
return bitcoinAddress(publicKey);
}
const publicKey = secp.getPublicKey(this.privateKey, compressed);
return bitcoinAddress(publicKey);
}
/**
* Function to get the private key in hex format or wif format
* @param format - The format must be "hex" (default) or "wif"
* @param compressed - Optional arg when using WIF format. By default it
* uses the compressed value defined in the signer
* @example
* ```ts
* const signer = Signer.fromSeed("one two three four five six");
* console.log(signer.getPrivateKey());
* // bab7fd6e5bd624f4ea0c33f7e7219262a6fa93a945a8964d9f110148286b7b37
*
* console.log(signer.getPrivateKey("wif"));
* // L3UfgFJWmbVziGB1uZBjkG1UjKkF7hhpXWY7mbTUdmycmvXCVtiL
*
* console.log(signer.getPrivateKey("wif", false));
* // 5KEX4TMHG66fT7cM9HMZLmdp4hVq4LC4X2Fkg6zeypM5UteWmtd
* ```
*/
getPrivateKey(format: "wif" | "hex" = "hex", compressed = true): string {
let stringPrivateKey: string;
if (this.privateKey instanceof Uint8Array) {
stringPrivateKey = toHexString(this.privateKey);
} else if (typeof this.privateKey === "string") {
stringPrivateKey = this.privateKey;
} else {
stringPrivateKey = BigInt(this.privateKey).toString(16).padStart(64, "0");
}
const comp = compressed === undefined ? this.compressed : compressed;
switch (format) {
case "hex":
return stringPrivateKey;
case "wif":
return bitcoinEncode(toUint8Array(stringPrivateKey), "private", comp);
default:
/* eslint-disable-next-line @typescript-eslint/restrict-template-expressions */
throw new Error(`Invalid format ${format}`);
}
}
/**
* Function to sign a hash value. It returns the bytes signature.
* The signature is in compact format with the recovery byte
* @param hash - Hash value. Also known as digest
*/
async signHash(hash: Uint8Array): Promise<Uint8Array> {
const [compSignature, recovery] = await secp.sign(hash, this.privateKey, {
recovered: true,
canonical: true,
der: false, // compact signature
});
const compactSignature = new Uint8Array(65);
compactSignature.set([recovery + 31], 0);
compactSignature.set(compSignature, 1);
return compactSignature;
}
/**
* Function to sign a message, which could be a string or a Uint8Array
*/
async signMessage(message: string | Uint8Array): Promise<Uint8Array> {
return this.signHash(sha256(message));
}
/**
* Function to sign a transaction. It's important to remark that
* the transaction parameter is modified inside this function.
* @param tx - Unsigned transaction
*/
async signTransaction(
tx: TransactionJson | TransactionJsonWait,
_abis?: Record<string, Abi>
): Promise<TransactionJson> {
if (!tx.id) throw new Error("Missing transaction id");
// estimation of rcLimit
if (
this.rcOptions.estimateRc &&
(!tx.signatures || tx.signatures.length === 0)
) {
const receipt = await this.estimateReceipt(tx);
tx.header!.rc_limit = this.rcOptions.adjustRcLimit
? await this.rcOptions.adjustRcLimit(receipt)
: receipt.rc_used;
tx.id = Transaction.computeTransactionId(tx.header!);
}
// multihash 0x1220. 12: code sha2-256. 20: length (32 bytes)
// tx id is a stringified multihash, need to extract the hash digest only
const hash = toUint8Array(tx.id.slice(6));
const signature = await this.signHash(hash);
if (!tx.signatures) tx.signatures = [];
tx.signatures.push(encodeBase64url(signature));
return tx;
}
/**
* Function to sign a block for federated consensus. That is,
* just the ecdsa signature. For other algorithms, like PoW,
* you have to sign the block and then process the signature
* to add the extra data (nonce in the case of PoW).
* @param block - Unsigned block
*/
async signBlock(block: BlockJson): Promise<BlockJson> {
if (!block.id) throw new Error("Missing block id");
// multihash 0x1220. 12: code sha2-256. 20: length (32 bytes)
// block id is a stringified multihash, need to extract the hash digest only
const hash = toUint8Array(block.id.slice(6));
const signature = await this.signHash(hash);
block.signature = encodeBase64url(signature);
return block;
}
/**
* Function to sign and send a transaction. It internally uses
* [[Provider.sendTransaction]]
* @param transaction - Transaction to send. It will be signed inside this
* function if it is not signed yet
* @param options - Options for sending the transaction
*/
async sendTransaction(
transaction: TransactionJson | TransactionJsonWait,
options?: SendTransactionOptions
): Promise<{
receipt: TransactionReceipt;
transaction: TransactionJsonWait;
}> {
if (!transaction.signatures || !transaction.signatures?.length)
transaction = await this.signTransaction(
transaction,
options?.sendAbis ? options.abis : undefined
);
if (!this.provider) throw new Error("provider is undefined");
const opts: SendTransactionOptions = {
...this.sendOptions,
...options,
};
if (opts.beforeSend) {
await opts.beforeSend(transaction, options);
}
return this.provider.sendTransaction(transaction, opts.broadcast);
}
/**
* Estimate the receipt associated to the transaction if
* it sent to the blockchain. It is useful to estimate the
* consumption of mana.
* The transaction is signed during this process and sent
* to the rpc node with the "broadcast:false" option to
* just compute the transaction without broadcasting it to
* the network.
* After that, the initial signatures are restored (if any)
* and the ones used for the estimation will be removed.
*/
async estimateReceipt(
tx: TransactionJson | TransactionJsonWait
): Promise<TransactionReceipt> {
if (!tx.id) throw new Error("Missing transaction id");
if (!tx.signatures) tx.signatures = [];
const signaturesCopy = [...tx.signatures];
// sign if there are no signatures
if (tx.signatures.length === 0) {
const hash = toUint8Array(tx.id.slice(6));
const signature = await this.signHash(hash);
tx.signatures.push(encodeBase64url(signature));
}
try {
const { receipt } = await this.sendTransaction(tx, {
broadcast: false,
});
// restore signatures
tx.signatures = signaturesCopy;
return receipt;
} catch (error) {
// restore signatures
tx.signatures = signaturesCopy;
throw error;
}
}
/**
* Function to recover the public key from hash and signature
* @param hash - hash sha256
* @param signature - compact signature
* @param compressed - default true
*/
static recoverPublicKey(
hash: Uint8Array,
signature: Uint8Array,
compressed = true
): string {
const compactSignatureHex = toHexString(signature);
const recovery = Number(`0x${compactSignatureHex.slice(0, 2)}`) - 31;
const rHex = compactSignatureHex.slice(2, 66);
const sHex = compactSignatureHex.slice(66);
const r = BigInt(`0x${rHex}`);
const s = BigInt(`0x${sHex}`);
const sig = new secp.Signature(r, s);
const publicKey = secp.recoverPublicKey(
toHexString(hash),
sig.toHex(),
recovery
);
if (!publicKey) throw new Error("Public key cannot be recovered");
if (!compressed) {
return toHexString(publicKey);
} else {
return secp.Point.fromHex(publicKey).toHex(true);
}
}
static recoverAddress(
hash: Uint8Array,
signature: Uint8Array,
compressed = true
): string {
return bitcoinAddress(
toUint8Array(Signer.recoverPublicKey(hash, signature, compressed))
);
}
/**
* Function to recover the publics keys from a signed
* transaction or block.
* The output format can be compressed (default) or uncompressed.
*
* @example
* ```ts
* const publicKeys = await Signer.recoverPublicKeys(tx);
* ```
*
* If the signature data contains more data, like in the
* blocks for PoW consensus, use the "transformSignature"
* function to extract the signature.
*
* @example
* ```ts
* const powDescriptorJson = {
* nested: {
* mypackage: {
* nested: {
* pow_signature_data: {
* fields: {
* nonce: {
* type: "bytes",
* id: 1,
* },
* recoverable_signature: {
* type: "bytes",
* id: 2,
* },
* },
* },
* },
* },
* },
* };
*
* const serializer = new Serializer(powDescriptorJson, {
* defaultTypeName: "pow_signature_data",
* });
*
* const publicKeys = await signer.recoverPublicKeys(block, {
* transformSignature: async (signatureData) => {
* const powSignatureData = await serializer.deserialize(signatureData);
* return powSignatureData.recoverable_signature;
* },
* });
* ```
*/
async recoverPublicKeys(
txOrBlock: TransactionJson | BlockJson,
opts?: RecoverPublicKeyOptions
): Promise<string[]> {
let compressed = true;
if (opts && opts.compressed !== undefined) {
compressed = opts.compressed;
}
let signatures: string[] = [];
let headerBytes: Uint8Array;
const block = txOrBlock as BlockJson;
if (block.signature) {
if (!block.header) throw new Error("Missing block header");
if (!block.signature) throw new Error("Missing block signature");
signatures = [block.signature];
const headerDecoded = btypeDecode(block.header, btypeBlockHeader!, false);
const message = koinos.protocol.block_header.create(headerDecoded);
headerBytes = koinos.protocol.block_header.encode(message).finish();
} else {
const transaction = txOrBlock as TransactionJson;
if (!transaction.header) throw new Error("Missing transaction header");
if (!transaction.signatures)
throw new Error("Missing transaction signatures");
signatures = transaction.signatures;
const headerDecoded = btypeDecode(
transaction.header,
btypeTransactionHeader!,
false
);
const message = koinos.protocol.transaction_header.create(headerDecoded);
headerBytes = koinos.protocol.transaction_header.encode(message).finish();
}
const hash = sha256(headerBytes);
return Promise.all(
signatures.map(async (signature) => {
if (opts && typeof opts.transformSignature === "function") {
signature = await opts.transformSignature(signature);
}
return Signer.recoverPublicKey(
hash,
decodeBase64url(signature),
compressed
);
})
);
}
/**
* Function to recover the signer addresses from a signed
* transaction or block.
* The output format can be compressed (default) or uncompressed.
* @example
* ```ts
* const addresses = await signer.recoverAddress(tx);
* ```
*
* If the signature data contains more data, like in the
* blocks for PoW consensus, use the "transformSignature"
* function to extract the signature.
*
* @example
* ```ts
* const powDescriptorJson = {
* nested: {
* mypackage: {
* nested: {
* pow_signature_data: {
* fields: {
* nonce: {
* type: "bytes",
* id: 1,
* },
* recoverable_signature: {
* type: "bytes",
* id: 2,
* },
* },
* },
* },
* },
* },
* };
*
* const serializer = new Serializer(powDescriptorJson, {
* defaultTypeName: "pow_signature_data",
* });
*
* const addresses = await signer.recoverAddress(block, {
* transformSignature: async (signatureData) => {
* const powSignatureData = await serializer.deserialize(signatureData);
* return powSignatureData.recoverable_signature;
* },
* });
* ```
*/
async recoverAddresses(
txOrBlock: TransactionJson | BlockJson,
opts?: RecoverPublicKeyOptions
): Promise<string[]> {
const publicKeys = await this.recoverPublicKeys(txOrBlock, opts);
return publicKeys.map((publicKey) =>
bitcoinAddress(toUint8Array(publicKey))
);
}
/**
* Function to prepare a block
* @param block -
* @returns A prepared block. ()
*/
async prepareBlock(block: BlockJson): Promise<BlockJson> {
if (!block.header) {
block.header = {};
}
const hashes: Uint8Array[] = [];
if (block.transactions) {
for (let index = 0; index < block.transactions.length; index++) {
const tx = block.transactions[index];
const headerDecoded = btypeDecode(
tx.header!,
btypeTransactionHeader!,
false
);
const message =
koinos.protocol.transaction_header.create(headerDecoded);
const headerBytes = koinos.protocol.transaction_header
.encode(message)
.finish() as Uint8Array;
hashes.push(sha256(headerBytes));
let signaturesBytes = new Uint8Array();
tx.signatures?.forEach((sig) => {
signaturesBytes = new Uint8Array([
...signaturesBytes,
...decodeBase64url(sig),
]);
});
hashes.push(sha256(signaturesBytes));
}
}
// retrieve head info if not provided
// eslint-disable-next-line @typescript-eslint/naming-convention
let { height, previous, previous_state_merkle_root } = block.header;
if (!height || !previous || !previous_state_merkle_root) {
if (!this.provider) {
throw new Error(
"Cannot get the head info because provider is undefined."
);
}
const headInfo = await this.provider.getHeadInfo();
height = height || `${Number(headInfo.head_topology.height) + 1}`;
previous = previous || headInfo.head_topology.id;
previous_state_merkle_root =
previous_state_merkle_root || headInfo.head_state_merkle_root;
}
block.header = {
height,
previous,
previous_state_merkle_root,
timestamp: block.header.timestamp || `${Date.now()}`,
transaction_merkle_root: encodeBase64url(
new Uint8Array([
// multihash sha256: 18, 32
18,
32,
...calculateMerkleRoot(hashes),
])
),
signer: this.address,
};
const headerDecoded = btypeDecode(block.header, btypeBlockHeader!, false);
const message = koinos.protocol.block_header.create(headerDecoded);
const headerBytes = koinos.protocol.block_header
.encode(message)
.finish() as Uint8Array;
const hash = sha256(headerBytes);
// multihash 0x1220. 12: code sha2-256. 20: length (32 bytes)
block.id = `0x1220${toHexString(hash)}`;
return block;
}
}
export default Signer;