@@ -8,7 +8,15 @@ import {
88 TransactionType ,
99 TransactionFee ,
1010} from '@bitgo/sdk-core' ;
11- import { utils as FlareUtils , Credential , pvmSerial , UnsignedTx , secp256k1 } from '@flarenetwork/flarejs' ;
11+ import {
12+ utils as FlareUtils ,
13+ Credential ,
14+ pvmSerial ,
15+ UnsignedTx ,
16+ secp256k1 ,
17+ EVMUnsignedTx ,
18+ Address ,
19+ } from '@flarenetwork/flarejs' ;
1220import { Buffer } from 'buffer' ;
1321import { DecodedUtxoObj , TransactionExplanation , Tx , TxData } from './iface' ;
1422import { KeyPair } from './keyPair' ;
@@ -27,6 +35,7 @@ interface CheckSignature {
2735 ( signature : string , addressHex : string ) : boolean ;
2836}
2937
38+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
3039function generateSelectorSignature ( signatures : string [ ] ) : CheckSignature {
3140 if ( signatures . length > 1 && signatures . every ( ( sig ) => isEmptySignature ( sig ) ) ) {
3241 return function ( sig , address ) : boolean {
@@ -97,8 +106,9 @@ export class Transaction extends BaseTransaction {
97106
98107 async sign ( keyPair : KeyPair ) : Promise < void > {
99108 const prv = keyPair . getPrivateKey ( ) as Uint8Array ;
100- const addressHex = keyPair . getAddressBuffer ( ) . toString ( 'hex' ) ;
101-
109+ console . log ( 'private key in sign method:' , Buffer . from ( prv ) . toString ( 'hex' ) ) ;
110+ const addressHex = keyPair . getEvmAddressBuffer ( ) . toString ( 'hex' ) ;
111+ console . log ( 'address hex in sign method:' , addressHex ) ;
102112 if ( ! prv ) {
103113 throw new SigningError ( 'Missing private key' ) ;
104114 }
@@ -109,31 +119,54 @@ export class Transaction extends BaseTransaction {
109119 throw new InvalidTransactionError ( 'empty credentials to sign' ) ;
110120 }
111121
112- const unsignedTx = this . _flareTransaction as UnsignedTx ;
122+ const unsignedTx = this . _flareTransaction as EVMUnsignedTx ;
113123 const unsignedBytes = unsignedTx . toBytes ( ) ;
114124 const publicKey = secp256k1 . getPublicKey ( prv ) ;
125+ console . log ( 'public key in sign method:' , Buffer . from ( publicKey ) . toString ( 'hex' ) ) ;
115126
116- if ( unsignedTx . hasPubkey ( publicKey ) ) {
127+ const addrAvax = new Address ( secp256k1 . publicKeyBytesToAddress ( publicKey ) ) ;
128+ const addrEVM = new Address ( secp256k1 . publicKeyToEthAddress ( publicKey ) ) ;
129+ console . log ( 'address AVAX in sign method:' , addrAvax . toString ( ) ) ;
130+ console . log ( 'address EVM in sign method:' , addrEVM . toString ( ) ) ;
131+
132+ const addressMap = unsignedTx . getAddresses ( ) ;
133+ console . log (
134+ 'address map in sign method:' ,
135+ addressMap . map ( ( addr ) => Buffer . from ( addr ) . toString ( 'hex' ) )
136+ ) ;
137+
138+ // Compare the EVM address with the address map
139+ const evmAddrHex = Buffer . from ( secp256k1 . publicKeyToEthAddress ( publicKey ) ) . toString ( 'hex' ) ;
140+ console . log ( 'EVM address hex:' , evmAddrHex ) ;
141+
142+ const hasMatchingAddress = addressMap . some (
143+ ( addr ) => Buffer . from ( addr ) . toString ( 'hex' ) . toLowerCase ( ) === evmAddrHex . toLowerCase ( )
144+ ) ;
145+ console . log ( 'Has matching address:' , hasMatchingAddress ) ;
146+
147+ if ( hasMatchingAddress ) {
117148 const signature = await secp256k1 . sign ( unsignedBytes , prv ) ;
118- let checkSign : CheckSignature | undefined = undefined ;
149+ console . log ( 'Generated signature:' , Buffer . from ( signature ) . toString ( 'hex' ) ) ;
119150
120- unsignedTx . credentials . forEach ( ( c ) => {
121- if ( checkSign === undefined ) {
122- checkSign = generateSelectorSignature ( c . getSignatures ( ) ) ;
123- }
124- let find = false ;
125- c . getSignatures ( ) . forEach ( ( sig , index ) => {
126- if ( checkSign && checkSign ( sig , addressHex ) ) {
127- c . setSignature ( index , signature ) ;
128- find = true ;
129- }
130- } ) ;
131- if ( ! find ) {
132- throw new SigningError (
133- `Private key cannot sign the transaction, address hex ${ addressHex } , public key: ${ publicKey } `
134- ) ;
151+ let signatureSet = false ;
152+ // Find first empty signature slot and set it
153+ for ( const credential of unsignedTx . credentials ) {
154+ const emptySlotIndex = credential . getSignatures ( ) . findIndex ( ( sig ) => isEmptySignature ( sig ) ) ;
155+ if ( emptySlotIndex !== - 1 ) {
156+ credential . setSignature ( emptySlotIndex , signature ) ;
157+ console . log ( 'Set signature at index:' , emptySlotIndex ) ;
158+ signatureSet = true ;
159+ break ;
135160 }
136- } ) ;
161+ }
162+
163+ if ( ! signatureSet ) {
164+ throw new SigningError ( 'No empty signature slot found' ) ;
165+ }
166+
167+ // Verify signature was set
168+ const sigs = this . signature ;
169+ console . log ( 'Final signatures:' , sigs ) ;
137170 }
138171 }
139172
0 commit comments