Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Elliottdenis/sof 5159 TRX tx #173

Merged
merged 10 commits into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions examples/trx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Kiln, trxToSun } from '../src/kiln';
import type { FireblocksIntegration } from '../src/fireblocks';
import fs from 'node:fs';
import 'dotenv/config';

const apiSecret = fs.readFileSync(`${__dirname}/fireblocks_secret_prod.key`, 'utf8');

const k = new Kiln({
baseUrl: process.env.KILN_API_URL as string,
apiToken: process.env.KILN_API_KEY,
});

const vault: FireblocksIntegration = {
provider: 'fireblocks',
fireblocksApiKey: process.env.FIREBLOCKS_API_KEY as string,
fireblocksSecretKey: apiSecret,
vaultId: 17,
};

try {
console.log('crafting...');
const tx = await k.client.POST('/trx/transaction/stake', {
body: {
account_id: '',
owner_address: 'TAERHY5gyzDRmAaeqqa6C4Fuyc9HLnnHx7',
amount_sun: trxToSun('1').toString(),
resource: 'BANDWIDTH',
},
});

if (!tx.data) throw new Error('No data in response');

console.log('signing...');
const signResponse = await k.fireblocks.signTrxTx(vault, tx.data.data);

console.log('broadcasting...');
const broadcastedTx = await k.client.POST('/trx/transaction/broadcast', {
body: {
signed_tx_serialized: signResponse.signed_tx.data.signed_tx_serialized,
},
});
console.log(broadcastedTx);
} catch (err) {
console.log(err);
}
54 changes: 53 additions & 1 deletion src/fireblocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1005,7 +1005,7 @@ export class FireblocksService {
}

/**
* Sign a NEAR transaction on Fireblocks
* Sign a Near transaction on Fireblocks
*/
async signNearTx(
integration: FireblocksIntegration,
Expand Down Expand Up @@ -1051,4 +1051,56 @@ export class FireblocksService {
fireblocks_tx: fbTx,
};
}

/**
* Sign a Trx transaction on Fireblocks
*/
async signTrxTx(
integration: FireblocksIntegration,
tx: components['schemas']['TRXUnsignedTx'],
note?: string,
): Promise<{
signed_tx: { data: components['schemas']['TRXSignedTx'] };
fireblocks_tx: TransactionResponse;
}> {
const payload = {
rawMessageData: {
messages: [
{
content: tx.unsigned_tx_id,
preHash: {
content: tx.unsigned_tx_serialized,
hashAlgorithm: 'SHA256',
},
},
],
},
};

const fbSigner = this.getSigner(integration);
const fbNote = note ? note : 'TRX tx from @kilnfi/sdk';
const fbTx = await fbSigner.sign(payload, 'TRX', fbNote);

if (!fbTx.signedMessages?.[0]?.signature) {
throw new Error('Fireblocks signature is missing');
}

const signature = `${fbTx.signedMessages[0].signature.fullSig}0${fbTx.signedMessages[0].signature.v}`;

const preparedTx = await this.client.POST('/trx/transaction/prepare', {
body: {
unsigned_tx_serialized: tx.unsigned_tx_serialized,
signature: signature,
},
});

if (preparedTx.error) {
throw new Error('Failed to prepare transaction');
}

return {
signed_tx: preparedTx.data,
fireblocks_tx: fbTx,
};
}
}
3 changes: 2 additions & 1 deletion src/fireblocks_signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ export type FireblocksAssetId =
| 'INJ_INJ'
| 'TON_TEST'
| 'TON'
| 'KAVA_KAVA';
| 'KAVA_KAVA'
| 'TRX';

export class FireblocksSigner {
protected fireblocks: FireblocksSDK;
Expand Down
10 changes: 10 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,13 @@ export const tiaToUtia = (tia: string): bigint => {
export const fetToAfet = (fet: string): bigint => {
return parseUnits(fet, 18); // afet uses 18 decimals
};

// Convert TRX to sun
export const trxToSun = (trx: string): bigint => {
return parseUnits(trx, 6);
};

// Convert sun to TRX
export const sunToTrx = (trx: bigint): string => {
return formatUnits(trx, 6);
};
Loading