@openfort/openfort-node 0.10.0 → 0.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/dist/index.js +22 -6
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +20 -4
- package/dist/index.mjs.map +1 -1
- package/examples/solana/accounts/importAccount.ts +14 -8
- package/examples/solana/signing/signTransaction.ts +42 -21
- package/package.json +1 -1
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
// Usage: npx tsx solana/accounts/importAccount.ts
|
|
2
2
|
|
|
3
3
|
import Openfort from "@openfort/openfort-node";
|
|
4
|
+
import {
|
|
5
|
+
createKeyPairFromPrivateKeyBytes,
|
|
6
|
+
getAddressFromPublicKey,
|
|
7
|
+
} from "@solana/kit";
|
|
4
8
|
import "dotenv/config";
|
|
5
9
|
|
|
6
10
|
const openfort = new Openfort(process.env.OPENFORT_API_KEY!, {
|
|
@@ -8,18 +12,20 @@ const openfort = new Openfort(process.env.OPENFORT_API_KEY!, {
|
|
|
8
12
|
walletSecret: process.env.OPENFORT_WALLET_SECRET,
|
|
9
13
|
});
|
|
10
14
|
|
|
11
|
-
//
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
Math.floor(Math.random() * 16).toString(16)
|
|
15
|
-
).join("");
|
|
15
|
+
// Generate a random 32-byte private key using @solana/kit
|
|
16
|
+
const privateKeyBytes = new Uint8Array(32);
|
|
17
|
+
crypto.getRandomValues(privateKeyBytes);
|
|
16
18
|
|
|
17
|
-
|
|
19
|
+
const { publicKey } = await createKeyPairFromPrivateKeyBytes(privateKeyBytes);
|
|
20
|
+
const solanaAddress = await getAddressFromPublicKey(publicKey);
|
|
21
|
+
console.log("Generated Solana address:", solanaAddress);
|
|
22
|
+
|
|
23
|
+
// Convert private key bytes to hex for import
|
|
24
|
+
const privateKeyHex = Buffer.from(privateKeyBytes).toString("hex");
|
|
18
25
|
|
|
19
26
|
// Import the account to Openfort
|
|
20
|
-
// Openfort accepts both base58 and hex format for Solana keys
|
|
21
27
|
const account = await openfort.accounts.solana.backend.import({
|
|
22
|
-
privateKey: privateKeyHex,
|
|
28
|
+
privateKey: privateKeyHex,
|
|
23
29
|
});
|
|
24
30
|
|
|
25
31
|
console.log("\nImported Solana account:");
|
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
// Usage: npx tsx solana/signing/signTransaction.ts
|
|
2
2
|
|
|
3
3
|
import Openfort from "@openfort/openfort-node";
|
|
4
|
+
import { getTransferSolInstruction } from "@solana-program/system";
|
|
5
|
+
import {
|
|
6
|
+
address,
|
|
7
|
+
createNoopSigner,
|
|
8
|
+
createSolanaRpc,
|
|
9
|
+
createTransactionMessage,
|
|
10
|
+
setTransactionMessageFeePayerSigner,
|
|
11
|
+
setTransactionMessageLifetimeUsingBlockhash,
|
|
12
|
+
appendTransactionMessageInstruction,
|
|
13
|
+
compileTransaction,
|
|
14
|
+
getBase64EncodedWireTransaction,
|
|
15
|
+
} from "@solana/kit";
|
|
4
16
|
import "dotenv/config";
|
|
5
17
|
|
|
6
18
|
const openfort = new Openfort(process.env.OPENFORT_API_KEY!, {
|
|
@@ -12,28 +24,37 @@ const openfort = new Openfort(process.env.OPENFORT_API_KEY!, {
|
|
|
12
24
|
const account = await openfort.accounts.solana.backend.create();
|
|
13
25
|
console.log("Created Solana account:", account.address);
|
|
14
26
|
|
|
15
|
-
//
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
27
|
+
// Build a transaction using @solana/kit
|
|
28
|
+
const rpc = createSolanaRpc("https://api.devnet.solana.com");
|
|
29
|
+
const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
|
|
30
|
+
|
|
31
|
+
const sender = createNoopSigner(address(account.address));
|
|
32
|
+
const destination = address("FDx9mfVqTvXUaSPQDELwDtGgMqxirmAFsEK2s4YsKfsc");
|
|
33
|
+
|
|
34
|
+
const transferIx = getTransferSolInstruction({
|
|
35
|
+
source: sender,
|
|
36
|
+
destination,
|
|
37
|
+
amount: 1_000n,
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const transactionMessage = appendTransactionMessageInstruction(
|
|
41
|
+
transferIx,
|
|
42
|
+
setTransactionMessageLifetimeUsingBlockhash(
|
|
43
|
+
latestBlockhash,
|
|
44
|
+
setTransactionMessageFeePayerSigner(
|
|
45
|
+
sender,
|
|
46
|
+
createTransactionMessage({ version: 0 }),
|
|
47
|
+
),
|
|
48
|
+
),
|
|
20
49
|
);
|
|
21
50
|
|
|
22
|
-
|
|
51
|
+
const compiledTransaction = compileTransaction(transactionMessage);
|
|
52
|
+
const base64Transaction = getBase64EncodedWireTransaction(compiledTransaction);
|
|
53
|
+
|
|
54
|
+
console.log("\nTransaction (base64):", base64Transaction);
|
|
23
55
|
|
|
24
56
|
// Sign the transaction
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
transaction: sampleTransaction,
|
|
30
|
-
});
|
|
31
|
-
console.log("Signed transaction:", signedTransaction);
|
|
32
|
-
} catch (error) {
|
|
33
|
-
console.log(
|
|
34
|
-
"\nNote: This example uses a placeholder transaction."
|
|
35
|
-
);
|
|
36
|
-
console.log(
|
|
37
|
-
"In production, create a valid Solana transaction using @solana/web3.js"
|
|
38
|
-
);
|
|
39
|
-
}
|
|
57
|
+
const signedTransaction = await account.signTransaction({
|
|
58
|
+
transaction: base64Transaction,
|
|
59
|
+
});
|
|
60
|
+
console.log("Signed transaction:", signedTransaction);
|