Skip to content

Commit 5a2f11d

Browse files
authored
Merge pull request #28 from getsafle/dev
Dev [updating sign transaction]
2 parents 063062e + 7cc1657 commit 5a2f11d

5 files changed

Lines changed: 179 additions & 62 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@
1717

1818
### 1.0.1 (2024-7-25)
1919

20-
- Updated entry point in json package
20+
- Updated entry point in json package
21+
22+
### 1.0.2 (2025-05-05)
23+
24+
- Updating solana transaction function for lefi swap which supports different type of solana transaction

package-lock.json

Lines changed: 84 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@getsafle/vault-sol-controller",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "Solana chain controller for Safle Vault",
55
"main": "src/index.js",
66
"scripts": {
@@ -17,7 +17,7 @@
1717
"license": "MIT",
1818
"dependencies": {
1919
"@solana/spl-token": "^0.4.8",
20-
"@solana/web3.js": "^1.95.1",
20+
"@solana/web3.js": "^1.98.2",
2121
"bip39": "^3.1.0",
2222
"bs58": "^5.0.0",
2323
"ed25519-hd-key": "^1.3.0",

src/helper/generateTransactionObject.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
1-
const solanaWeb3 = require('@solana/web3.js');
2-
const { getOrCreateAssociatedTokenAccount, createTransferInstruction} = require("@solana/spl-token");
1+
const solanaWeb3 = require("@solana/web3.js");
2+
const {
3+
getOrCreateAssociatedTokenAccount,
4+
createTransferInstruction,
5+
} = require("@solana/spl-token");
36

4-
const { solana_transaction: { NATIVE_TRANSFER, TOKEN_TRANSFER }} = require("../config");
7+
const {
8+
solana_transaction: { NATIVE_TRANSFER, TOKEN_TRANSFER },
9+
} = require("../config");
510

611
async function generateTransactionObject(transaction, signer, connection) {
12+
// ✅ Handle base64 versioned transaction (bypasses normal logic)
13+
if (transaction.serializedTx) {
14+
const rawBuffer = Buffer.from(transaction.serializedTx, "base64");
15+
const versionedTx = solanaWeb3.VersionedTransaction.deserialize(rawBuffer);
16+
17+
// ✅ Sign the deserialized versioned transaction
18+
versionedTx.sign([signer]);
19+
20+
return versionedTx;
21+
}
22+
23+
// 🔁 Legacy transaction types
724
const { txnType } = transaction;
825
let rawTransaction = {};
926

@@ -48,16 +65,14 @@ async function generateTransactionObject(transaction, signer, connection) {
4865
);
4966
}
5067

51-
// set the desired priority fee in microLamport
52-
if(transaction?.priorityFee && transaction?.priorityFee > 0) {
68+
// ⏫ Optional: Add priority fee if needed
69+
if (transaction?.priorityFee && transaction?.priorityFee > 0) {
5370
const addPriorityFee = solanaWeb3.ComputeBudgetProgram.setComputeUnitPrice({
5471
microLamports: transaction.priorityFee,
5572
});
56-
5773
rawTransaction.add(addPriorityFee);
5874
}
5975

6076
return rawTransaction;
6177
}
62-
6378
module.exports = generateTransactionObject;

src/index.js

Lines changed: 66 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
const bs58 = require("bs58");
2-
const nacl = require('tweetnacl');
2+
const nacl = require("tweetnacl");
33
const helper = require("./helper");
44
const ObservableStore = require("obs-store");
5-
const solanaWeb3 = require('@solana/web3.js');
6-
5+
const solanaWeb3 = require("@solana/web3.js");
76

87
const {
98
solana: { HD_PATH },
@@ -61,7 +60,7 @@ class KeyringController {
6160
}
6261

6362
async signMessage(message, _address) {
64-
const { mnemonic, address } = this.store.getState()
63+
const { mnemonic, address } = this.store.getState();
6564
const idx = address.indexOf(_address);
6665

6766
if (idx < 0)
@@ -87,34 +86,55 @@ class KeyringController {
8786

8887
try {
8988
const signer = helper.setupAccount(mnemonic, helper.getHDPath(idx));
90-
9189
const connection = new solanaWeb3.Connection(network, "confirmed");
9290

93-
const rawTx = await helper.generateTransactionObject(transaction, signer, connection);
94-
95-
const rawSignedTxn = await helper.signTransaction(rawTx, signer, connection, []);
96-
97-
const signedTxn = rawSignedTxn.serialize().toString("hex");
98-
99-
return { signedTransaction: signedTxn };
91+
// 🔄 Generate the transaction object (legacy or versioned)
92+
const rawTx = await helper.generateTransactionObject(
93+
transaction,
94+
signer,
95+
connection
96+
);
97+
98+
// 🔁 Return serialized transaction in hex
99+
if (transaction.serializedTx) {
100+
// 🔐 Sign if it's not already signed
101+
if (
102+
rawTx.signatures &&
103+
rawTx.signatures.some((sig) => sig === undefined || sig.length === 0)
104+
) {
105+
rawTx.sign([signer]);
106+
}
107+
const signedTxn = Buffer.from(rawTx.serialize()).toString("hex");
108+
return { signedTransaction: signedTxn };
109+
} else {
110+
const rawSignedTxn = await helper.signTransaction(
111+
rawTx,
112+
signer,
113+
connection,
114+
[]
115+
);
116+
117+
const signedTxn = rawSignedTxn.serialize().toString("hex");
118+
return { signedTransaction: signedTxn };
119+
}
100120
} catch (err) {
101121
throw err;
102122
}
103123
}
104124

105125
async sendTransaction(rawTransaction) {
106-
107126
try {
108-
const { network } = this.store.getState()
109-
const stringBuff = Buffer.from(rawTransaction, 'hex')
110-
111-
const connection = new solanaWeb3.Connection(network, "confirmed")
112-
const transactionDetails = await connection.sendRawTransaction(stringBuff)
113-
return { transactionDetails: transactionDetails }
127+
const { network } = this.store.getState();
128+
const stringBuff = Buffer.from(rawTransaction, "hex");
114129

130+
const connection = new solanaWeb3.Connection(network, "confirmed");
131+
const transactionDetails = await connection.sendRawTransaction(
132+
stringBuff
133+
);
134+
return { transactionDetails: transactionDetails };
115135
} catch (err) {
116136
console.log(err);
117-
throw err
137+
throw err;
118138
}
119139
}
120140

@@ -126,18 +146,28 @@ class KeyringController {
126146
if (idx < 0)
127147
throw "Invalid address, the address is not available in the wallet";
128148

129-
const signer = helper.setupAccount(mnemonic, helper.getHDPath(idx));
149+
const signer = helper.setupAccount(mnemonic, helper.getHDPath(idx));
130150

131-
const connection = new solanaWeb3.Connection(network, "confirmed");
151+
const connection = new solanaWeb3.Connection(network, "confirmed");
132152

133-
const rawTx = await helper.generateTransactionObject(rawTransaction, signer, connection);
134-
135-
const rawSignedTxn = await helper.signTransaction(rawTx, signer, connection, []);
153+
const rawTx = await helper.generateTransactionObject(
154+
rawTransaction,
155+
signer,
156+
connection
157+
);
136158

137-
const fees = await connection.getFeeForMessage(rawSignedTxn.compileMessage());
159+
const rawSignedTxn = await helper.signTransaction(
160+
rawTx,
161+
signer,
162+
connection,
163+
[]
164+
);
138165

139-
return { fees: fees.value };
166+
const fees = await connection.getFeeForMessage(
167+
rawSignedTxn.compileMessage()
168+
);
140169

170+
return { fees: fees.value };
141171
}
142172

143173
persistAllAddress(_address) {
@@ -156,13 +186,16 @@ class KeyringController {
156186

157187
const getBalance = async (address, network) => {
158188
try {
159-
const _network = helper.getNetwork(network)
160-
const connection = new solanaWeb3.Connection(_network, "confirmed")
161-
const accInfo = await connection.getAccountInfo(new solanaWeb3.PublicKey(address), 'confirmed')
162-
return { balance: accInfo ? accInfo.lamports : 0 }
189+
const _network = helper.getNetwork(network);
190+
const connection = new solanaWeb3.Connection(_network, "confirmed");
191+
const accInfo = await connection.getAccountInfo(
192+
new solanaWeb3.PublicKey(address),
193+
"confirmed"
194+
);
195+
return { balance: accInfo ? accInfo.lamports : 0 };
163196
} catch (err) {
164-
throw err
197+
throw err;
165198
}
166-
}
199+
};
167200

168201
module.exports = { KeyringController, getBalance };

0 commit comments

Comments
 (0)