Skip to content

Commit 6e07394

Browse files
EllzerLeTamanoir
authored andcommitted
Elliottdenis/sof 5159 TRX tx (#173)
* feat: add trx example * up * add `00` in fullsig + switch keccack to sha256 * up * up * up * feat(signTrxTx) update signature + use tx_id from tx-api * up * up url * remove unused import --------- Co-authored-by: Martin Saldinger <[email protected]>
1 parent 4e780b6 commit 6e07394

File tree

4 files changed

+110
-2
lines changed

4 files changed

+110
-2
lines changed

examples/trx.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { Kiln, trxToSun } from '../src/kiln';
2+
import type { FireblocksIntegration } from '../src/fireblocks';
3+
import fs from 'node:fs';
4+
import 'dotenv/config';
5+
6+
const apiSecret = fs.readFileSync(`${__dirname}/fireblocks_secret_prod.key`, 'utf8');
7+
8+
const k = new Kiln({
9+
baseUrl: process.env.KILN_API_URL as string,
10+
apiToken: process.env.KILN_API_KEY,
11+
});
12+
13+
const vault: FireblocksIntegration = {
14+
provider: 'fireblocks',
15+
fireblocksApiKey: process.env.FIREBLOCKS_API_KEY as string,
16+
fireblocksSecretKey: apiSecret,
17+
vaultId: 17,
18+
};
19+
20+
try {
21+
console.log('crafting...');
22+
const tx = await k.client.POST('/trx/transaction/stake', {
23+
body: {
24+
account_id: '',
25+
owner_address: 'TAERHY5gyzDRmAaeqqa6C4Fuyc9HLnnHx7',
26+
amount_sun: trxToSun('1').toString(),
27+
resource: 'BANDWIDTH',
28+
},
29+
});
30+
31+
if (!tx.data) throw new Error('No data in response');
32+
33+
console.log('signing...');
34+
const signResponse = await k.fireblocks.signTrxTx(vault, tx.data.data);
35+
36+
console.log('broadcasting...');
37+
const broadcastedTx = await k.client.POST('/trx/transaction/broadcast', {
38+
body: {
39+
signed_tx_serialized: signResponse.signed_tx.data.signed_tx_serialized,
40+
},
41+
});
42+
console.log(broadcastedTx);
43+
} catch (err) {
44+
console.log(err);
45+
}

src/fireblocks.ts

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,7 @@ export class FireblocksService {
10051005
}
10061006

10071007
/**
1008-
* Sign a NEAR transaction on Fireblocks
1008+
* Sign a Near transaction on Fireblocks
10091009
*/
10101010
async signNearTx(
10111011
integration: FireblocksIntegration,
@@ -1051,4 +1051,56 @@ export class FireblocksService {
10511051
fireblocks_tx: fbTx,
10521052
};
10531053
}
1054+
1055+
/**
1056+
* Sign a Trx transaction on Fireblocks
1057+
*/
1058+
async signTrxTx(
1059+
integration: FireblocksIntegration,
1060+
tx: components['schemas']['TRXUnsignedTx'],
1061+
note?: string,
1062+
): Promise<{
1063+
signed_tx: { data: components['schemas']['TRXSignedTx'] };
1064+
fireblocks_tx: TransactionResponse;
1065+
}> {
1066+
const payload = {
1067+
rawMessageData: {
1068+
messages: [
1069+
{
1070+
content: tx.unsigned_tx_id,
1071+
preHash: {
1072+
content: tx.unsigned_tx_serialized,
1073+
hashAlgorithm: 'SHA256',
1074+
},
1075+
},
1076+
],
1077+
},
1078+
};
1079+
1080+
const fbSigner = this.getSigner(integration);
1081+
const fbNote = note ? note : 'TRX tx from @kilnfi/sdk';
1082+
const fbTx = await fbSigner.sign(payload, 'TRX', fbNote);
1083+
1084+
if (!fbTx.signedMessages?.[0]?.signature) {
1085+
throw new Error('Fireblocks signature is missing');
1086+
}
1087+
1088+
const signature = `${fbTx.signedMessages[0].signature.fullSig}0${fbTx.signedMessages[0].signature.v}`;
1089+
1090+
const preparedTx = await this.client.POST('/trx/transaction/prepare', {
1091+
body: {
1092+
unsigned_tx_serialized: tx.unsigned_tx_serialized,
1093+
signature: signature,
1094+
},
1095+
});
1096+
1097+
if (preparedTx.error) {
1098+
throw new Error('Failed to prepare transaction');
1099+
}
1100+
1101+
return {
1102+
signed_tx: preparedTx.data,
1103+
fireblocks_tx: fbTx,
1104+
};
1105+
}
10541106
}

src/fireblocks_signer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ export type FireblocksAssetId =
3939
| 'INJ_INJ'
4040
| 'TON_TEST'
4141
| 'TON'
42-
| 'KAVA_KAVA';
42+
| 'KAVA_KAVA'
43+
| 'TRX';
4344

4445
export class FireblocksSigner {
4546
protected fireblocks: FireblocksSDK;

src/utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,13 @@ export const tiaToUtia = (tia: string): bigint => {
239239
export const fetToAfet = (fet: string): bigint => {
240240
return parseUnits(fet, 18); // afet uses 18 decimals
241241
};
242+
243+
// Convert TRX to sun
244+
export const trxToSun = (trx: string): bigint => {
245+
return parseUnits(trx, 6);
246+
};
247+
248+
// Convert sun to TRX
249+
export const sunToTrx = (trx: bigint): string => {
250+
return formatUnits(trx, 6);
251+
};

0 commit comments

Comments
 (0)