11import BigNumber from 'bignumber.js' ;
2- import {
3- BaseTransaction ,
4- TransactionType ,
5- InvalidTransactionError ,
6- TransactionRecipient ,
7- PublicKey ,
8- } from '@bitgo/sdk-core' ;
2+ import { BaseTransaction , TransactionType , InvalidTransactionError , TransactionRecipient } from '@bitgo/sdk-core' ;
93import { BaseCoin as CoinConfig } from '@bitgo/statics' ;
104import {
115 TransactionBody ,
@@ -35,13 +29,9 @@ export class Transaction extends BaseTransaction {
3529 private _dependsOn : string | null ;
3630 private _nonce : number ;
3731 private _sender : string ;
38- private _senderSignature : Buffer ;
32+ private _senderSignature : Buffer | null ;
3933 private _feePayerAddress : string ;
40- private _feePayerSignature : Buffer ;
41- private _feePayerPubKey : PublicKey ;
42-
43- static EMPTY_PUBLIC_KEY = Buffer . alloc ( 32 ) ;
44- static EMPTY_SIGNATURE = Buffer . alloc ( 64 ) ;
34+ private _feePayerSignature : Buffer | null ;
4535
4636 constructor ( _coinConfig : Readonly < CoinConfig > ) {
4737 super ( _coinConfig ) ;
@@ -55,15 +45,12 @@ export class Transaction extends BaseTransaction {
5545 this . _dependsOn = null ;
5646 this . _nonce = 0x0 ;
5747 this . _recipients = [ ] ;
58- this . _feePayerPubKey = {
59- pub : HexUInt . of ( Transaction . EMPTY_PUBLIC_KEY ) . toString ( ) ,
60- } ;
61- this . _senderSignature = Transaction . EMPTY_SIGNATURE ;
62- this . _feePayerSignature = Transaction . EMPTY_SIGNATURE ;
48+ this . _senderSignature = null ;
49+ this . _feePayerSignature = null ;
6350 }
6451
6552 public get id ( ) : string {
66- this . generateTxnId ( ) ;
53+ this . generateTxnIdAndSetSender ( ) ;
6754 return this . _id ?? 'UNAVAILABLE' ;
6855 }
6956
@@ -92,7 +79,11 @@ export class Transaction extends BaseTransaction {
9279 }
9380
9481 get senderSignature ( ) : Uint8Array | undefined {
95- return this . _senderSignature ;
82+ if ( this . _senderSignature ) {
83+ return new Uint8Array ( this . _senderSignature ) ;
84+ } else {
85+ return undefined ;
86+ }
9687 }
9788
9889 set senderSignature ( sig : Buffer ) {
@@ -107,8 +98,12 @@ export class Transaction extends BaseTransaction {
10798 this . _feePayerAddress = address ;
10899 }
109100
110- get feePayerSignature ( ) : Uint8Array {
111- return this . _feePayerSignature ;
101+ get feePayerSignature ( ) : Uint8Array | undefined {
102+ if ( this . _feePayerSignature ) {
103+ return new Uint8Array ( this . _feePayerSignature ) ;
104+ } else {
105+ return undefined ;
106+ }
112107 }
113108
114109 set feePayerSignature ( sig : Buffer ) {
@@ -187,14 +182,6 @@ export class Transaction extends BaseTransaction {
187182 this . _nonce = n ;
188183 }
189184
190- get feePayerPubKey ( ) : PublicKey {
191- return this . _feePayerPubKey ;
192- }
193-
194- set feePayerPubKey ( pubKey : PublicKey ) {
195- this . _feePayerPubKey = pubKey ;
196- }
197-
198185 get contract ( ) : string {
199186 return this . _contract ;
200187 }
@@ -300,7 +287,7 @@ export class Transaction extends BaseTransaction {
300287 this . loadInputsAndOutputs ( ) ;
301288
302289 // Set sender address
303- if ( signedTx . origin ) {
290+ if ( signedTx . signature && signedTx . origin ) {
304291 this . sender = signedTx . origin . toString ( ) . toLowerCase ( ) ;
305292 }
306293
@@ -323,56 +310,52 @@ export class Transaction extends BaseTransaction {
323310 this . senderSignature = signature ;
324311 }
325312
326- getFeePayerPubKey ( ) : string {
327- return this . feePayerPubKey . pub ;
328- }
329-
330- addFeePayerSignature ( publicKey : PublicKey , signature : Buffer ) : void {
331- this . feePayerPubKey = publicKey ;
313+ addFeePayerSignature ( signature : Buffer ) : void {
332314 this . feePayerSignature = signature ;
333315 }
334316
335317 async build ( ) : Promise < void > {
336318 this . buildClauses ( ) ;
337319 await this . buildRawTransaction ( ) ;
338- this . generateTxnId ( ) ;
320+ this . generateTxnIdAndSetSender ( ) ;
339321 this . loadInputsAndOutputs ( ) ;
340322 }
341323
342324 /**
343325 * Sets the transaction ID from the raw transaction if it is signed
344326 * @protected
345327 */
346- protected generateTxnId ( ) : void {
328+ protected generateTxnIdAndSetSender ( ) : void {
347329 // Check if we have a raw transaction
348330 if ( ! this . rawTransaction ) {
349331 return ;
350332 }
351-
352- // Check if the transaction is signed by verifying signature exists
353- if ( ! this . senderSignature || ! this . feePayerSignature ) {
354- return ;
355- }
356-
357- const halfSignedTransaction : VetTransaction = VetTransaction . of ( this . rawTransaction . body , this . senderSignature ) ;
358- if ( ! halfSignedTransaction . signature ) {
359- return ;
360- }
361- const fullSignedTransaction : VetTransaction = VetTransaction . of (
362- halfSignedTransaction . body ,
363- nc_utils . concatBytes (
364- // Drop any previous gas payer signature.
365- halfSignedTransaction . signature . slice ( 0 , Secp256k1 . SIGNATURE_LENGTH ) ,
366- this . feePayerSignature
367- )
368- ) ;
369- if ( ! fullSignedTransaction . signature ) {
370- return ;
371- }
372- try {
373- this . _id = fullSignedTransaction . id . toString ( ) ;
374- } catch ( e ) {
333+ if ( ! this . senderSignature ) {
375334 return ;
335+ } else {
336+ const halfSignedTransaction : VetTransaction = VetTransaction . of ( this . rawTransaction . body , this . senderSignature ) ;
337+ if ( halfSignedTransaction . signature ) {
338+ this . _rawTransaction = halfSignedTransaction ;
339+ this . _sender = halfSignedTransaction . origin . toString ( ) . toLowerCase ( ) ;
340+ } else {
341+ return ;
342+ }
343+ if ( this . feePayerSignature ) {
344+ const fullSignedTransaction : VetTransaction = VetTransaction . of (
345+ halfSignedTransaction . body ,
346+ nc_utils . concatBytes (
347+ // Drop any previous gas payer signature.
348+ halfSignedTransaction . signature . slice ( 0 , Secp256k1 . SIGNATURE_LENGTH ) ,
349+ this . feePayerSignature
350+ )
351+ ) ;
352+ if ( fullSignedTransaction . signature ) {
353+ this . _rawTransaction = fullSignedTransaction ;
354+ this . _id = fullSignedTransaction . id . toString ( ) ;
355+ } else {
356+ return ;
357+ }
358+ }
376359 }
377360 }
378361
@@ -468,22 +451,34 @@ export class Transaction extends BaseTransaction {
468451 }
469452
470453 serialize ( ) : string {
471- const halfSignedTransaction : VetTransaction = VetTransaction . of ( this . rawTransaction . body , this . senderSignature ) ;
472- if ( ! halfSignedTransaction . signature ) {
473- throw new InvalidTransactionError ( 'Invalid sender signature in half-signed transaction' ) ;
474- }
475- if ( ! this . feePayerSignature ) {
476- throw new InvalidTransactionError ( 'Missing fee payer signature' ) ;
454+ if ( ! this . senderSignature ) {
455+ return HexUInt . of ( this . rawTransaction . encoded ) . toString ( ) ;
456+ } else {
457+ if ( ! this . feePayerSignature ) {
458+ const senderSignedTransaction : VetTransaction = VetTransaction . of (
459+ this . rawTransaction . body ,
460+ this . senderSignature
461+ ) ;
462+ return HexUInt . of ( senderSignedTransaction . encoded ) . toString ( ) ;
463+ } else {
464+ const senderSignedTransaction : VetTransaction = VetTransaction . of (
465+ this . rawTransaction . body ,
466+ this . senderSignature
467+ ) ;
468+ if ( senderSignedTransaction . signature ) {
469+ const fullSignedTransaction : VetTransaction = VetTransaction . of (
470+ senderSignedTransaction . body ,
471+ nc_utils . concatBytes (
472+ senderSignedTransaction . signature . slice ( 0 , Secp256k1 . SIGNATURE_LENGTH ) ,
473+ this . feePayerSignature
474+ )
475+ ) ;
476+ return HexUInt . of ( fullSignedTransaction . encoded ) . toString ( ) ;
477+ } else {
478+ throw new InvalidTransactionError ( 'Transaction is not signed properly' ) ;
479+ }
480+ }
477481 }
478- const fullSignedTransaction : VetTransaction = VetTransaction . of (
479- halfSignedTransaction . body ,
480- nc_utils . concatBytes (
481- // Drop any previous gas payer signature.
482- halfSignedTransaction . signature . slice ( 0 , Secp256k1 . SIGNATURE_LENGTH ) ,
483- this . feePayerSignature
484- )
485- ) ;
486- return HexUInt . of ( fullSignedTransaction . encoded ) . toString ( ) ;
487482 }
488483
489484 static deserializeTransaction ( rawTx : string ) : VetTransaction {
0 commit comments