Skip to content

Commit 48a0709

Browse files
committed
feat(KeyPair, Transaction): update address derivation methods and remove redundant logging in sign method
TICKET: WIN-7770
1 parent 347238a commit 48a0709

File tree

2 files changed

+7
-20
lines changed

2 files changed

+7
-20
lines changed

modules/sdk-coin-flrp/src/lib/keyPair.ts

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import {
1111
import { bip32, ECPair } from '@bitgo/secp256k1';
1212
import { randomBytes, createHash } from 'crypto';
1313
import utils from './utils';
14+
import { secp256k1 } from '@flarenetwork/flarejs';
15+
import { Buffer as SafeBuffer } from 'safe-buffer';
1416

1517
const DEFAULT_SEED_SIZE_BYTES = 16;
1618
export enum addressFormat {
@@ -127,7 +129,8 @@ export class KeyPair extends Secp256k1ExtendedKeyPair {
127129
getAddressBuffer(): Buffer {
128130
try {
129131
// Use the safe buffer method for address derivation
130-
return this.getAddressSafeBuffer();
132+
const publicKey = Buffer.from(this.keyPair.publicKey);
133+
return Buffer.from(secp256k1.publicKeyBytesToAddress(publicKey));
131134
} catch (error) {
132135
return this.getAddressSafeBuffer();
133136
}
@@ -139,22 +142,8 @@ export class KeyPair extends Secp256k1ExtendedKeyPair {
139142
* @returns {Buffer}
140143
*/
141144
private getAddressSafeBuffer(): Buffer {
142-
// For EVM addresses, we need to use keccak256 hash of the uncompressed public key
143-
const pubKey = this.keyPair.publicKey;
144-
// Skip the first byte (compression flag) for EVM address generation
145-
const sha256 = createHash('sha256').update(pubKey.slice(1)).digest();
146-
const ripemd = createHash('ripemd160').update(sha256).digest();
147-
return ripemd;
148-
}
149-
150-
/**
151-
* Get the EVM format address buffer
152-
* @returns {Buffer} The EVM address buffer
153-
*/
154-
getEvmAddressBuffer(): Buffer {
155-
const pubKey = this.keyPair.publicKey;
156-
// Remove compression flag and get last 20 bytes of keccak256 hash
157-
const keccak = createHash('sha3-256').update(pubKey.slice(1)).digest();
158-
return keccak.slice(-20);
145+
const publicKeySafe = SafeBuffer.from(this.keyPair.publicKey);
146+
const sha256 = SafeBuffer.from(createHash('sha256').update(publicKeySafe).digest());
147+
return Buffer.from(createHash('ripemd160').update(sha256).digest());
159148
}
160149
}

modules/sdk-coin-flrp/src/lib/transaction.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,6 @@ export class Transaction extends BaseTransaction {
107107
async sign(keyPair: KeyPair): Promise<void> {
108108
const prv = keyPair.getPrivateKey() as Uint8Array;
109109
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);
112110
if (!prv) {
113111
throw new SigningError('Missing private key');
114112
}

0 commit comments

Comments
 (0)