@@ -12,33 +12,38 @@ import {
1212 AccountAddress ,
1313 AccountAuthenticatorEd25519 ,
1414 Aptos ,
15- APTOS_COIN ,
1615 AptosConfig ,
1716 DEFAULT_MAX_GAS_AMOUNT ,
1817 Ed25519PublicKey ,
1918 Ed25519Signature ,
19+ FeePayerRawTransaction ,
20+ generateSigningMessage ,
2021 generateUserTransactionHash ,
2122 Hex ,
2223 Network ,
24+ RAW_TRANSACTION_SALT ,
25+ RAW_TRANSACTION_WITH_DATA_SALT ,
2326 RawTransaction ,
2427 SignedTransaction ,
2528 SimpleTransaction ,
26- TransactionAuthenticatorEd25519 ,
29+ TransactionAuthenticatorFeePayer ,
2730} from '@aptos-labs/ts-sdk' ;
2831import { DEFAULT_GAS_UNIT_PRICE , SECONDS_PER_WEEK , UNAVAILABLE_TEXT } from '../constants' ;
2932import utils from '../utils' ;
3033import BigNumber from 'bignumber.js' ;
3134
3235export abstract class Transaction extends BaseTransaction {
3336 protected _rawTransaction : RawTransaction ;
34- protected _signature : Signature ;
37+ protected _senderSignature : Signature ;
38+ protected _feePayerSignature : Signature ;
3539 protected _sender : string ;
3640 protected _recipient : TransactionRecipient ;
3741 protected _sequenceNumber : number ;
3842 protected _maxGasAmount : number ;
3943 protected _gasUnitPrice : number ;
4044 protected _gasUsed : number ;
4145 protected _expirationTime : number ;
46+ protected _feePayerAddress : string ;
4247
4348 static EMPTY_PUBLIC_KEY = Buffer . alloc ( 32 ) ;
4449 static EMPTY_SIGNATURE = Buffer . alloc ( 64 ) ;
@@ -50,7 +55,13 @@ export abstract class Transaction extends BaseTransaction {
5055 this . _gasUsed = 0 ;
5156 this . _expirationTime = Math . floor ( Date . now ( ) / 1e3 ) + SECONDS_PER_WEEK ;
5257 this . _sequenceNumber = 0 ;
53- this . _signature = {
58+ this . _senderSignature = {
59+ publicKey : {
60+ pub : Hex . fromHexInput ( Transaction . EMPTY_PUBLIC_KEY ) . toString ( ) ,
61+ } ,
62+ signature : Transaction . EMPTY_SIGNATURE ,
63+ } ;
64+ this . _feePayerSignature = {
5465 publicKey : {
5566 pub : Hex . fromHexInput ( Transaction . EMPTY_PUBLIC_KEY ) . toString ( ) ,
5667 } ,
@@ -120,6 +131,10 @@ export abstract class Transaction extends BaseTransaction {
120131 this . _expirationTime = value ;
121132 }
122133
134+ get feePayerAddress ( ) : string {
135+ return this . _feePayerAddress ;
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 ) ;
156+
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 ) ;
141162
142- const signature = new Ed25519Signature ( this . _signature . signature ) ;
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 TransactionAuthenticatorFeePayer ;
235+ this . _feePayerAddress = authenticator . fee_payer . address . toString ( ) ;
236+ const senderAuthenticator = authenticator . sender as AccountAuthenticatorEd25519 ;
237+ const senderSignature = Buffer . from ( senderAuthenticator . signature . toUint8Array ( ) ) ;
238+ this . addSenderSignature ( { pub : senderAuthenticator . public_key . toString ( ) } , senderSignature ) ;
239+
240+ const feePayerAuthenticator = authenticator . fee_payer . authenticator as AccountAuthenticatorEd25519 ;
241+ const feePayerSignature = Buffer . from ( feePayerAuthenticator . signature . toUint8Array ( ) ) ;
242+ this . addFeePayerSignature ( { pub : feePayerAuthenticator . public_key . toString ( ) } , feePayerSignature ) ;
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 : {
@@ -247,14 +286,41 @@ export abstract class Transaction extends BaseTransaction {
247286 return new BigNumber ( this . gasUsed ) . multipliedBy ( this . gasUnitPrice ) . toString ( ) ;
248287 }
249288
289+ public get signablePayload ( ) : Buffer {
290+ return this . feePayerAddress ? this . getSignablePayloadWithFeePayer ( ) : this . getSignablePayloadWithoutFeePayer ( ) ;
291+ }
292+
293+ private getSignablePayloadWithFeePayer ( ) : Buffer {
294+ const feePayerRawTxn = new FeePayerRawTransaction (
295+ this . _rawTransaction ,
296+ [ ] ,
297+ AccountAddress . fromString ( this . _feePayerAddress )
298+ ) ;
299+ return Buffer . from ( generateSigningMessage ( feePayerRawTxn . bcsToBytes ( ) , RAW_TRANSACTION_WITH_DATA_SALT ) ) ;
300+ }
301+
302+ private getSignablePayloadWithoutFeePayer ( ) : Buffer {
303+ return Buffer . from ( generateSigningMessage ( this . _rawTransaction . bcsToBytes ( ) , RAW_TRANSACTION_SALT ) ) ;
304+ }
305+
250306 private generateTxnId ( ) {
251- if ( ! this . _signature || ! this . _signature . publicKey || ! this . _signature . signature ) {
307+ if (
308+ ! this . _senderSignature ||
309+ ! this . _senderSignature . publicKey ||
310+ ! this . _senderSignature . signature ||
311+ ! this . _feePayerSignature ||
312+ ! this . _feePayerSignature . publicKey ||
313+ ! this . _feePayerSignature . signature
314+ ) {
252315 return ;
253316 }
254317 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 } ) ;
318+ const senderPublicKey = new Ed25519PublicKey ( utils . getBufferFromHexString ( this . _senderSignature . publicKey . pub ) ) ;
319+ const senderSignature = new Ed25519Signature ( this . _senderSignature . signature ) ;
320+ const senderAuthenticator = new AccountAuthenticatorEd25519 ( senderPublicKey , senderSignature ) ;
321+ const feePayerPublicKey = new Ed25519PublicKey ( utils . getBufferFromHexString ( this . _feePayerSignature . publicKey . pub ) ) ;
322+ const feePayerSignature = new Ed25519Signature ( this . _feePayerSignature . signature ) ;
323+ const feePayerAuthenticator = new AccountAuthenticatorEd25519 ( feePayerPublicKey , feePayerSignature ) ;
324+ this . _id = generateUserTransactionHash ( { transaction, senderAuthenticator, feePayerAuthenticator } ) ;
259325 }
260326}
0 commit comments