Skip to content

Commit 600c0eb

Browse files
committed
refactor: update dependencies and improve transaction type handling in builders
TICKET: WIN-7770
1 parent ed8cbc9 commit 600c0eb

File tree

8 files changed

+41
-37
lines changed

8 files changed

+41
-37
lines changed

modules/sdk-coin-flrp/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343
]
4444
},
4545
"dependencies": {
46-
"@bitgo/sdk-core": "^36.21.0",
47-
"@bitgo/statics": "^58.14.0",
46+
"@bitgo/sdk-core": "^36.22.0",
47+
"@bitgo/statics": "^58.15.0",
4848
"@bitgo/secp256k1": "^1.7.0",
4949
"@flarenetwork/flarejs": "4.1.1",
5050
"@noble/curves": "1.8.1",

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ import {
99
Int,
1010
Id,
1111
TransferableOutput,
12-
TypeSymbols,
1312
Address,
1413
TransferOutput,
1514
OutputOwners,
1615
utils as FlareUtils,
1716
} from '@flarenetwork/flarejs';
1817
import utils from './utils';
19-
import { DecodedUtxoObj, Tx } from './iface';
18+
import { DecodedUtxoObj, Tx, FlareTransactionType } from './iface';
2019

2120
export class ExportInCTxBuilder extends AtomicInCTransactionBuilder {
2221
private _amount: bigint;
@@ -91,9 +90,6 @@ export class ExportInCTxBuilder extends AtomicInCTransactionBuilder {
9190
const output = outputs[0];
9291

9392
// TODO validate assetId
94-
// if (Buffer.from(output.assetId.toBytes()).toString('hex') !== assetIdStr) {
95-
// throw new Error('AssetID are not equals');
96-
// }
9793

9894
// The inputs is not an utxo.
9995
// It's expected to have only one input from C-Chain address.
@@ -121,11 +117,11 @@ export class ExportInCTxBuilder extends AtomicInCTransactionBuilder {
121117
return this;
122118
}
123119

124-
static verifyTxType(txnType: TypeSymbols): boolean {
125-
return txnType === TypeSymbols.EvmExportTx;
120+
static verifyTxType(txnType: string): boolean {
121+
return txnType === FlareTransactionType.EvmExportTx;
126122
}
127123

128-
verifyTxType(txnType: TypeSymbols): boolean {
124+
verifyTxType(txnType: string): boolean {
129125
return ExportInCTxBuilder.verifyTxType(txnType);
130126
}
131127

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ import {
88
Int,
99
Id,
1010
TransferableOutput,
11-
TypeSymbols,
1211
Address,
1312
utils as FlareUtils,
1413
TransferOutput,
1514
OutputOwners,
1615
} from '@flarenetwork/flarejs';
1716
import utils from './utils';
18-
import { DecodedUtxoObj, SECP256K1_Transfer_Output } from './iface';
17+
import { DecodedUtxoObj, SECP256K1_Transfer_Output, FlareTransactionType } from './iface';
1918

2019
export class ExportInPTxBuilder extends AtomicTransactionBuilder {
2120
private _amount: bigint;
@@ -42,7 +41,7 @@ export class ExportInPTxBuilder extends AtomicTransactionBuilder {
4241

4342
initBuilder(tx: UnsignedTx): this {
4443
const baseTx = tx.getTx() as evmSerial.ExportTx;
45-
if (!this.verifyTxType(baseTx)) {
44+
if (!this.verifyTxType(baseTx._type)) {
4645
throw new NotSupported('Transaction cannot be parsed or has an unsupported transaction type');
4746
}
4847

@@ -81,12 +80,12 @@ export class ExportInPTxBuilder extends AtomicTransactionBuilder {
8180
return this;
8281
}
8382

84-
static verifyTxType(baseTx: evmSerial.ExportTx): boolean {
85-
return baseTx._type === TypeSymbols.PvmExportTx;
83+
static verifyTxType(txnType: string): boolean {
84+
return txnType === FlareTransactionType.PvmExportTx;
8685
}
8786

88-
verifyTxType(baseTx: evmSerial.ExportTx): boolean {
89-
return ExportInPTxBuilder.verifyTxType(baseTx);
87+
verifyTxType(txnType: string): boolean {
88+
return ExportInPTxBuilder.verifyTxType(txnType);
9089
}
9190

9291
/**

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
avmSerial,
1616
} from '@flarenetwork/flarejs';
1717
import utils from './utils';
18-
import { DecodedUtxoObj, SECP256K1_Transfer_Output } from './iface';
18+
import { DecodedUtxoObj, FlareTransactionType, SECP256K1_Transfer_Output } from './iface';
1919

2020
export class ImportInCTxBuilder extends AtomicInCTransactionBuilder {
2121
constructor(_coinConfig: Readonly<CoinConfig>) {
@@ -38,7 +38,7 @@ export class ImportInCTxBuilder extends AtomicInCTransactionBuilder {
3838

3939
initBuilder(tx: UnsignedTx): this {
4040
const baseTx = tx.getTx() as evmSerial.ImportTx;
41-
if (!this.verifyTxType(baseTx)) {
41+
if (!this.verifyTxType(baseTx._type)) {
4242
throw new NotSupported('Transaction cannot be parsed or has an unsupported transaction type');
4343
}
4444

@@ -78,12 +78,12 @@ export class ImportInCTxBuilder extends AtomicInCTransactionBuilder {
7878
return this;
7979
}
8080

81-
static verifyTxType(baseTx: evmSerial.ImportTx): boolean {
82-
return baseTx._type === TypeSymbols.BaseTx;
81+
static verifyTxType(txnType: string): boolean {
82+
return txnType === FlareTransactionType.PvmImportTx;
8383
}
8484

85-
verifyTxType(baseTx: evmSerial.ImportTx): boolean {
86-
return ImportInCTxBuilder.verifyTxType(baseTx);
85+
verifyTxType(txnType: string): boolean {
86+
return ImportInCTxBuilder.verifyTxType(txnType);
8787
}
8888

8989
/**

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ import {
77
Int,
88
Id,
99
TransferableInput,
10-
TypeSymbols,
1110
utils as FlareUtils,
1211
Address,
1312
BigIntPr,
1413
} from '@flarenetwork/flarejs';
1514
import utils from './utils';
16-
import { DecodedUtxoObj, SECP256K1_Transfer_Output } from './iface';
15+
import { DecodedUtxoObj, FlareTransactionType, SECP256K1_Transfer_Output } from './iface';
1716

1817
export class ImportInPTxBuilder extends AtomicTransactionBuilder {
1918
constructor(_coinConfig: Readonly<CoinConfig>) {
@@ -32,7 +31,7 @@ export class ImportInPTxBuilder extends AtomicTransactionBuilder {
3231

3332
initBuilder(tx: UnsignedTx): this {
3433
const baseTx = tx.getTx() as evmSerial.ImportTx;
35-
if (!this.verifyTxType(baseTx)) {
34+
if (!this.verifyTxType(baseTx._type)) {
3635
throw new NotSupported('Transaction cannot be parsed or has an unsupported transaction type');
3736
}
3837

@@ -68,12 +67,12 @@ export class ImportInPTxBuilder extends AtomicTransactionBuilder {
6867
return this;
6968
}
7069

71-
static verifyTxType(baseTx: evmSerial.ImportTx): boolean {
72-
return baseTx._type === TypeSymbols.PvmImportTx;
70+
static verifyTxType(txnType: string): boolean {
71+
return txnType === FlareTransactionType.PvmImportTx;
7372
}
7473

75-
verifyTxType(baseTx: evmSerial.ImportTx): boolean {
76-
return ImportInPTxBuilder.verifyTxType(baseTx);
74+
verifyTxType(txnType: string): boolean {
75+
return ImportInPTxBuilder.verifyTxType(txnType);
7776
}
7877

7978
/**

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
import { TransactionExplanation as BaseTransactionExplanation, Entry, TransactionType } from '@bitgo/sdk-core';
22
import { pvmSerial, UnsignedTx, TransferableOutput, evmSerial } from '@flarenetwork/flarejs';
33

4+
/**
5+
* Enum for Flare transaction types
6+
*/
7+
export enum FlareTransactionType {
8+
EvmExportTx = 'evm.ExportTx',
9+
EvmImportTx = 'evm.ImportTx',
10+
PvmExportTx = 'pvm.ExportTx',
11+
PvmImportTx = 'pvm.ImportTx',
12+
}
13+
414
export interface FlrpEntry extends Entry {
515
id: string;
616
}

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

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

@@ -131,7 +130,8 @@ export class KeyPair extends Secp256k1ExtendedKeyPair {
131130
try {
132131
// Use the safe buffer method for address derivation
133132
const publicKey = Buffer.from(this.keyPair.publicKey);
134-
return Buffer.from(secp256k1.publicKeyBytesToAddress(publicKey));
133+
const sha256 = createHash('sha256').update(publicKey).digest();
134+
return Buffer.from(createHash('ripemd160').update(sha256).digest());
135135
} catch (error) {
136136
return this.getAddressSafeBuffer();
137137
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
import { FlareNetwork } from '@bitgo/statics';
1212
import { Buffer } from 'buffer';
1313
import { createHash } from 'crypto';
14-
import { secp256k1 } from '@noble/curves/secp256k1';
14+
import { ecc } from '@bitgo/secp256k1';
1515
import { ADDRESS_SEPARATOR, Output, DeprecatedTx } from './iface';
1616
import bs58 from 'bs58';
1717
import { bech32 } from 'bech32';
@@ -90,7 +90,7 @@ export class Utils implements BaseUtils {
9090
}
9191

9292
try {
93-
secp256k1.ProjectivePoint.fromHex(pubBuf.toString('hex'));
93+
ecc.isPoint(pubBuf);
9494
return true;
9595
} catch (e) {
9696
return false;
@@ -121,8 +121,8 @@ export class Utils implements BaseUtils {
121121
*/
122122
createSignature(network: FlareNetwork, message: Buffer, prv: Buffer): Buffer {
123123
const messageHash = this.sha256(message);
124-
const signature = secp256k1.sign(messageHash, prv);
125-
return Buffer.from(signature.toCompactRawBytes());
124+
const signature = ecc.sign(messageHash, prv);
125+
return Buffer.from(signature);
126126
}
127127

128128
/**
@@ -131,7 +131,7 @@ export class Utils implements BaseUtils {
131131
verifySignature(network: FlareNetwork, message: Buffer, signature: Buffer, publicKey: Buffer): boolean {
132132
try {
133133
const messageHash = this.sha256(message);
134-
return secp256k1.verify(signature, messageHash, publicKey);
134+
return ecc.verify(signature, messageHash, publicKey);
135135
} catch (e) {
136136
return false;
137137
}

0 commit comments

Comments
 (0)