@@ -12,7 +12,6 @@ import {
1212 AccountAddress ,
1313 AccountAuthenticatorEd25519 ,
1414 Aptos ,
15- APTOS_COIN ,
1615 AptosConfig ,
1716 DEFAULT_MAX_GAS_AMOUNT ,
1817 Ed25519PublicKey ,
@@ -23,22 +22,24 @@ import {
2322 RawTransaction ,
2423 SignedTransaction ,
2524 SimpleTransaction ,
26- TransactionAuthenticatorEd25519 ,
25+ TransactionAuthenticatorFeePayer ,
2726} from '@aptos-labs/ts-sdk' ;
2827import { DEFAULT_GAS_UNIT_PRICE , SECONDS_PER_WEEK , UNAVAILABLE_TEXT } from '../constants' ;
2928import utils from '../utils' ;
3029import BigNumber from 'bignumber.js' ;
3130
3231export abstract class Transaction extends BaseTransaction {
3332 protected _rawTransaction : RawTransaction ;
34- protected _signature : Signature ;
33+ protected _senderSignature : Signature ;
34+ protected _feePayerSignature : Signature ;
3535 protected _sender : string ;
3636 protected _recipient : TransactionRecipient ;
3737 protected _sequenceNumber : number ;
3838 protected _maxGasAmount : number ;
3939 protected _gasUnitPrice : number ;
4040 protected _gasUsed : number ;
4141 protected _expirationTime : number ;
42+ protected _feePayerAddress : string ;
4243
4344 static EMPTY_PUBLIC_KEY = Buffer . alloc ( 32 ) ;
4445 static EMPTY_SIGNATURE = Buffer . alloc ( 64 ) ;
@@ -50,7 +51,13 @@ export abstract class Transaction extends BaseTransaction {
5051 this . _gasUsed = 0 ;
5152 this . _expirationTime = Math . floor ( Date . now ( ) / 1e3 ) + SECONDS_PER_WEEK ;
5253 this . _sequenceNumber = 0 ;
53- this . _signature = {
54+ this . _senderSignature = {
55+ publicKey : {
56+ pub : Hex . fromHexInput ( Transaction . EMPTY_PUBLIC_KEY ) . toString ( ) ,
57+ } ,
58+ signature : Transaction . EMPTY_SIGNATURE ,
59+ } ;
60+ this . _feePayerSignature = {
5461 publicKey : {
5562 pub : Hex . fromHexInput ( Transaction . EMPTY_PUBLIC_KEY ) . toString ( ) ,
5663 } ,
@@ -120,6 +127,14 @@ export abstract class Transaction extends BaseTransaction {
120127 this . _expirationTime = value ;
121128 }
122129
130+ get feePayerAddress ( ) : string {
131+ return this . _feePayerAddress ;
132+ }
133+
134+ set feePayerAddress ( value : string ) {
135+ this . _feePayerAddress = value ;
136+ }
137+
123138 set transactionType ( transactionType : TransactionType ) {
124139 this . _type = transactionType ;
125140 }
@@ -136,19 +151,38 @@ export abstract class Transaction extends BaseTransaction {
136151 }
137152
138153 serialize ( ) : string {
139- const publicKeyBuffer = utils . getBufferFromHexString ( this . _signature . publicKey . pub ) ;
140- const publicKey = new Ed25519PublicKey ( publicKeyBuffer ) ;
154+ const senderPublicKeyBuffer = utils . getBufferFromHexString ( this . _senderSignature . publicKey . pub ) ;
155+ const senderPublicKey = new Ed25519PublicKey ( senderPublicKeyBuffer ) ;
141156
142- const signature = new Ed25519Signature ( this . _signature . signature ) ;
157+ const senderSignature = new Ed25519Signature ( this . _senderSignature . signature ) ;
158+ const senderAuthenticator = new AccountAuthenticatorEd25519 ( senderPublicKey , senderSignature ) ;
159+
160+ const feePayerPublicKeyBuffer = utils . getBufferFromHexString ( this . _feePayerSignature . publicKey . pub ) ;
161+ const feePayerPublicKey = new Ed25519PublicKey ( feePayerPublicKeyBuffer ) ;
162+
163+ const feePayerSignature = new Ed25519Signature ( this . _feePayerSignature . signature ) ;
164+ const feePayerAuthenticator = new AccountAuthenticatorEd25519 ( feePayerPublicKey , feePayerSignature ) ;
165+
166+ const txnAuthenticator = new TransactionAuthenticatorFeePayer ( senderAuthenticator , [ ] , [ ] , {
167+ address : AccountAddress . fromString ( this . _feePayerAddress ) ,
168+ authenticator : feePayerAuthenticator ,
169+ } ) ;
143170
144- const txnAuthenticator = new TransactionAuthenticatorEd25519 ( publicKey , signature ) ;
145171 const signedTxn = new SignedTransaction ( this . _rawTransaction , txnAuthenticator ) ;
146172 return signedTxn . toString ( ) ;
147173 }
148174
149- addSignature ( publicKey : PublicKey , signature : Buffer ) : void {
175+ addSenderSignature ( publicKey : PublicKey , signature : Buffer ) : void {
150176 this . _signatures = [ signature . toString ( 'hex' ) ] ;
151- this . _signature = { publicKey, signature } ;
177+ this . _senderSignature = { publicKey, signature } ;
178+ }
179+
180+ addFeePayerSignature ( publicKey : PublicKey , signature : Buffer ) : void {
181+ this . _feePayerSignature = { publicKey, signature } ;
182+ }
183+
184+ addFeePayerAddress ( address : string ) : void {
185+ this . _feePayerAddress = address ;
152186 }
153187
154188 async build ( ) : Promise < void > {
@@ -197,9 +231,15 @@ export abstract class Transaction extends BaseTransaction {
197231 this . _rawTransaction = rawTxn ;
198232
199233 this . loadInputsAndOutputs ( ) ;
200- const authenticator = signedTxn . authenticator as TransactionAuthenticatorEd25519 ;
201- const signature = Buffer . from ( authenticator . signature . toUint8Array ( ) ) ;
202- this . addSignature ( { pub : authenticator . public_key . toString ( ) } , signature ) ;
234+ const authenticator = signedTxn . authenticator as any ;
235+ this . _feePayerAddress = authenticator . fee_payer . address . toString ( ) ;
236+ const senderSignature = Buffer . from ( authenticator . sender . signature . toUint8Array ( ) ) ;
237+ this . addSenderSignature ( { pub : authenticator . sender . public_key . toString ( ) } , senderSignature ) ;
238+ const feePayerSignature = Buffer . from ( authenticator . fee_payer . authenticator . signature . toUint8Array ( ) ) ;
239+ this . addFeePayerSignature (
240+ { pub : authenticator . fee_payer . authenticator . public_key . toString ( ) } ,
241+ feePayerSignature
242+ ) ;
203243 } catch ( e ) {
204244 console . error ( 'invalid signed transaction' , e ) ;
205245 throw new Error ( 'invalid signed transaction' ) ;
@@ -229,8 +269,7 @@ export abstract class Transaction extends BaseTransaction {
229269 const simpleTxn = await aptos . transaction . build . simple ( {
230270 sender : senderAddress ,
231271 data : {
232- function : '0x1::coin::transfer' ,
233- typeArguments : [ APTOS_COIN ] ,
272+ function : '0x1::aptos_account::transfer' ,
234273 functionArguments : [ recipientAddress , this . recipient . amount ] ,
235274 } ,
236275 options : {
@@ -248,13 +287,22 @@ export abstract class Transaction extends BaseTransaction {
248287 }
249288
250289 private generateTxnId ( ) {
251- if ( ! this . _signature || ! this . _signature . publicKey || ! this . _signature . signature ) {
290+ if (
291+ ! this . _senderSignature ||
292+ ! this . _senderSignature . publicKey ||
293+ ! this . _senderSignature . signature ||
294+ ! this . _feePayerSignature . publicKey ||
295+ ! this . _feePayerSignature . signature
296+ ) {
252297 return ;
253298 }
254299 const transaction = new SimpleTransaction ( this . _rawTransaction ) ;
255- const publicKey = new Ed25519PublicKey ( utils . getBufferFromHexString ( this . _signature . publicKey . pub ) ) ;
256- const signature = new Ed25519Signature ( this . _signature . signature ) ;
257- const senderAuthenticator = new AccountAuthenticatorEd25519 ( publicKey , signature ) ;
258- this . _id = generateUserTransactionHash ( { transaction, senderAuthenticator } ) ;
300+ const senderPublicKey = new Ed25519PublicKey ( utils . getBufferFromHexString ( this . _senderSignature . publicKey . pub ) ) ;
301+ const senderSignature = new Ed25519Signature ( this . _senderSignature . signature ) ;
302+ const senderAuthenticator = new AccountAuthenticatorEd25519 ( senderPublicKey , senderSignature ) ;
303+ const feePayerPublicKey = new Ed25519PublicKey ( utils . getBufferFromHexString ( this . _feePayerSignature . publicKey . pub ) ) ;
304+ const feePayerSignature = new Ed25519Signature ( this . _feePayerSignature . signature ) ;
305+ const feePayerAuthenticator = new AccountAuthenticatorEd25519 ( feePayerPublicKey , feePayerSignature ) ;
306+ this . _id = generateUserTransactionHash ( { transaction, senderAuthenticator, feePayerAuthenticator } ) ;
259307 }
260308}
0 commit comments