@openfort/openfort-node 0.6.78 → 0.7.0
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/Makefile +75 -0
- package/README.md +174 -65
- package/biome.json +34 -4
- package/dist/index.d.mts +8871 -14863
- package/dist/index.d.ts +8871 -14863
- package/dist/index.js +4978 -24180
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +4677 -23904
- package/dist/index.mjs.map +1 -1
- package/examples/.env.example +16 -0
- package/examples/README.md +154 -0
- package/examples/contracts/createContract.ts +25 -0
- package/examples/contracts/listContracts.ts +18 -0
- package/examples/contracts/readContract.ts +29 -0
- package/examples/evm/accounts/createAccount.ts +18 -0
- package/examples/evm/accounts/exportAccount.ts +39 -0
- package/examples/evm/accounts/getAccount.ts +30 -0
- package/examples/evm/accounts/importAccount.ts +31 -0
- package/examples/evm/accounts/listAccounts.ts +32 -0
- package/examples/evm/signing/signHash.ts +28 -0
- package/examples/evm/signing/signMessage.ts +32 -0
- package/examples/evm/signing/signTransaction.ts +42 -0
- package/examples/evm/signing/signTypedData.ts +71 -0
- package/examples/exchange/createSwap.ts +49 -0
- package/examples/exchange/quoteSwap.ts +34 -0
- package/examples/iam/deleteUser.ts +22 -0
- package/examples/iam/getSession.ts +30 -0
- package/examples/iam/getUser.ts +32 -0
- package/examples/iam/listUsers.ts +18 -0
- package/examples/iam/pregenerateUser.ts +33 -0
- package/examples/package.json +31 -0
- package/examples/pnpm-lock.yaml +1231 -0
- package/examples/policies/createPolicy.ts +25 -0
- package/examples/policies/createPolicyRule.ts +43 -0
- package/examples/policies/disableEnablePolicy.ts +29 -0
- package/examples/policies/getPolicy.ts +30 -0
- package/examples/policies/listPolicies.ts +17 -0
- package/examples/policies/listPolicyRules.ts +37 -0
- package/examples/policies/updatePolicy.ts +29 -0
- package/examples/solana/accounts/createAccount.ts +18 -0
- package/examples/solana/accounts/exportAccount.ts +29 -0
- package/examples/solana/accounts/getAccount.ts +24 -0
- package/examples/solana/accounts/importAccount.ts +28 -0
- package/examples/solana/accounts/listAccounts.ts +32 -0
- package/examples/solana/signing/signMessage.ts +22 -0
- package/examples/solana/signing/signTransaction.ts +41 -0
- package/examples/transactions/createTransactionIntent.ts +49 -0
- package/examples/transactions/estimateGas.ts +52 -0
- package/examples/transactions/getTransactionIntent.ts +54 -0
- package/examples/transactions/listTransactionIntents.ts +31 -0
- package/examples/tsconfig.json +15 -0
- package/examples/webhook/index.ts +43 -0
- package/openapi-auth.json +6526 -0
- package/openapi.json +20425 -0
- package/orval.config.ts +41 -0
- package/package.json +22 -12
- package/pnpm-workspace.yaml +2 -0
- package/tsconfig.json +1 -1
- package/vitest.config.ts +20 -0
- package/updateGeneratedCode.sh +0 -8
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# Openfort API Key (required)
|
|
2
|
+
OPENFORT_API_KEY=sk_test_...
|
|
3
|
+
OPENFORT_PUBLISHABLE_KEY=pk_test_...
|
|
4
|
+
# Optional: Custom API base URL
|
|
5
|
+
# OPENFORT_BASE_URL=https://api.openfort.io
|
|
6
|
+
|
|
7
|
+
# Optional: Wallet secret for backend wallet operations
|
|
8
|
+
# OPENFORT_WALLET_SECRET=your-wallet-secret
|
|
9
|
+
|
|
10
|
+
# Chain ID for EVM operations (e.g., 80002 for Amoy, 84532 for Base Sepolia)
|
|
11
|
+
CHAIN_ID=80002
|
|
12
|
+
|
|
13
|
+
# Shield Configuration (required for iam/pregenerateUser.ts)
|
|
14
|
+
SHIELD_API_KEY=your_shield_api_key
|
|
15
|
+
SHIELD_API_SECRET=your_shield_api_secret
|
|
16
|
+
SHIELD_ENCRYPTION_SHARE=your_encryption_share
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
# Openfort Node SDK Examples
|
|
2
|
+
|
|
3
|
+
This directory contains examples demonstrating how to use the Openfort Node SDK.
|
|
4
|
+
|
|
5
|
+
## Setup
|
|
6
|
+
|
|
7
|
+
1. Build the SDK and install dependencies:
|
|
8
|
+
```bash
|
|
9
|
+
pnpm setup
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
This will:
|
|
13
|
+
- Build the parent SDK package
|
|
14
|
+
- Install example dependencies (linked to local SDK)
|
|
15
|
+
|
|
16
|
+
2. Copy the environment template and configure your API keys:
|
|
17
|
+
```bash
|
|
18
|
+
cp .env.example .env
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
3. Edit `.env` with your Openfort API key:
|
|
22
|
+
```bash
|
|
23
|
+
OPENFORT_API_KEY=sk_test_...
|
|
24
|
+
CHAIN_ID=80002 # Polygon Amoy testnet
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Running Examples
|
|
28
|
+
|
|
29
|
+
Run any example using `pnpm example`:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
pnpm example evm/accounts/createAccount.ts
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Or directly with tsx:
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
npx tsx evm/accounts/createAccount.ts
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Development Workflow
|
|
42
|
+
|
|
43
|
+
When making changes to the SDK:
|
|
44
|
+
|
|
45
|
+
1. Make your changes in the parent `src/` directory
|
|
46
|
+
2. Rebuild the SDK:
|
|
47
|
+
```bash
|
|
48
|
+
pnpm build:sdk
|
|
49
|
+
```
|
|
50
|
+
3. Run an example to test:
|
|
51
|
+
```bash
|
|
52
|
+
pnpm example evm/accounts/createAccount.ts
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Examples Overview
|
|
56
|
+
|
|
57
|
+
### EVM Wallet Examples
|
|
58
|
+
|
|
59
|
+
#### Accounts (`evm/accounts/`)
|
|
60
|
+
| Example | Description |
|
|
61
|
+
| ------------------ | ------------------------------------ |
|
|
62
|
+
| `createAccount.ts` | Create a new EVM account |
|
|
63
|
+
| `getAccount.ts` | Retrieve an account by ID or address |
|
|
64
|
+
| `listAccounts.ts` | List all EVM accounts |
|
|
65
|
+
| `importAccount.ts` | Import an account from private key |
|
|
66
|
+
| `exportAccount.ts` | Export an account's private key |
|
|
67
|
+
|
|
68
|
+
#### Signing (`evm/signing/`)
|
|
69
|
+
| Example | Description |
|
|
70
|
+
| -------------------- | ---------------------------------- |
|
|
71
|
+
| `signMessage.ts` | Sign a message with an EVM account |
|
|
72
|
+
| `signTypedData.ts` | Sign EIP-712 typed data |
|
|
73
|
+
| `signHash.ts` | Sign a raw hash |
|
|
74
|
+
| `signTransaction.ts` | Sign a transaction |
|
|
75
|
+
|
|
76
|
+
### Solana Wallet Examples
|
|
77
|
+
|
|
78
|
+
#### Accounts (`solana/accounts/`)
|
|
79
|
+
| Example | Description |
|
|
80
|
+
| ------------------ | ------------------------------------ |
|
|
81
|
+
| `createAccount.ts` | Create a new Solana account |
|
|
82
|
+
| `getAccount.ts` | Retrieve an account by ID or address |
|
|
83
|
+
| `listAccounts.ts` | List all Solana accounts |
|
|
84
|
+
| `importAccount.ts` | Import an account from private key |
|
|
85
|
+
| `exportAccount.ts` | Export an account's private key |
|
|
86
|
+
|
|
87
|
+
#### Signing (`solana/signing/`)
|
|
88
|
+
| Example | Description |
|
|
89
|
+
| -------------------- | ------------------------------------ |
|
|
90
|
+
| `signMessage.ts` | Sign a message with a Solana account |
|
|
91
|
+
| `signTransaction.ts` | Sign a Solana transaction |
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
### Policies
|
|
95
|
+
|
|
96
|
+
| Example | Description |
|
|
97
|
+
| --------------------------------- | ------------------------------- |
|
|
98
|
+
| `policies/createPolicy.ts` | Create a gas sponsorship policy |
|
|
99
|
+
| `policies/getPolicy.ts` | Get a policy by ID |
|
|
100
|
+
| `policies/listPolicies.ts` | List all policies |
|
|
101
|
+
| `policies/updatePolicy.ts` | Update a policy |
|
|
102
|
+
| `policies/disableEnablePolicy.ts` | Enable/disable a policy |
|
|
103
|
+
| `policies/createPolicyRule.ts` | Create a policy rule |
|
|
104
|
+
| `policies/listPolicyRules.ts` | List policy rules |
|
|
105
|
+
|
|
106
|
+
### Contracts
|
|
107
|
+
|
|
108
|
+
| Example | Description |
|
|
109
|
+
| ----------------------------- | ------------------------- |
|
|
110
|
+
| `contracts/createContract.ts` | Register a contract |
|
|
111
|
+
| `contracts/listContracts.ts` | List all contracts |
|
|
112
|
+
| `contracts/readContract.ts` | Read data from a contract |
|
|
113
|
+
|
|
114
|
+
### Transaction Intents
|
|
115
|
+
|
|
116
|
+
| Example | Description |
|
|
117
|
+
| ----------------------------------------- | ------------------------------ |
|
|
118
|
+
| `transactions/createTransactionIntent.ts` | Create a transaction intent |
|
|
119
|
+
| `transactions/getTransactionIntent.ts` | Get a transaction intent |
|
|
120
|
+
| `transactions/listTransactionIntents.ts` | List transaction intents |
|
|
121
|
+
| `transactions/estimateGas.ts` | Estimate gas for a transaction |
|
|
122
|
+
|
|
123
|
+
### Exchange / Swaps
|
|
124
|
+
|
|
125
|
+
| Example | Description |
|
|
126
|
+
| ------------------------ | -------------------------- |
|
|
127
|
+
| `exchange/quoteSwap.ts` | Get a quote for token swap |
|
|
128
|
+
| `exchange/createSwap.ts` | Execute a token swap |
|
|
129
|
+
|
|
130
|
+
### IAM (Identity & Access Management)
|
|
131
|
+
|
|
132
|
+
| Example | Description |
|
|
133
|
+
| --------------------------- | ------------------------------------------------ |
|
|
134
|
+
| `iam/listUsers.ts` | List all authenticated users |
|
|
135
|
+
| `iam/getUser.ts` | Get a user by ID |
|
|
136
|
+
| `iam/deleteUser.ts` | Delete a user |
|
|
137
|
+
| `iam/pregenerateUser.ts` | Pre-generate a user with embedded wallet |
|
|
138
|
+
| `iam/getSession.ts` | Verify auth token server-side |
|
|
139
|
+
|
|
140
|
+
> **Note:** The `pregenerateUser.ts` example requires additional Shield configuration. See `.env.example` for the required environment variables:
|
|
141
|
+
> - `SHIELD_API_KEY`
|
|
142
|
+
> - `SHIELD_API_SECRET`
|
|
143
|
+
> - `SHIELD_ENCRYPTION_SHARE`
|
|
144
|
+
|
|
145
|
+
### Webhooks
|
|
146
|
+
|
|
147
|
+
| Example | Description |
|
|
148
|
+
| ------------------ | --------------------------------------------------- |
|
|
149
|
+
| `webhook/index.ts` | Express server to receive and verify webhook events |
|
|
150
|
+
|
|
151
|
+
## Additional Resources
|
|
152
|
+
|
|
153
|
+
- [Openfort Documentation](https://www.openfort.io/docs)
|
|
154
|
+
- [Dashboard](https://dashboard.openfort.io)
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// Usage: npx tsx contracts/createContract.ts
|
|
2
|
+
|
|
3
|
+
import Openfort from "@openfort/openfort-node";
|
|
4
|
+
import "dotenv/config";
|
|
5
|
+
|
|
6
|
+
const openfort = new Openfort(process.env.OPENFORT_API_KEY!, {
|
|
7
|
+
basePath: process.env.OPENFORT_BASE_URL,
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
const chainId = Number(process.env.CHAIN_ID) || 80002;
|
|
11
|
+
|
|
12
|
+
// Create a contract reference
|
|
13
|
+
const contract = await openfort.contracts.create({
|
|
14
|
+
name: "My NFT Contract",
|
|
15
|
+
chainId,
|
|
16
|
+
address: "0x38090d1636069c0ff1af6bc1737fb996b7f63ac0",
|
|
17
|
+
// Optional: provide ABI for function name validation
|
|
18
|
+
// abi: [...]
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
console.log("Created contract:");
|
|
22
|
+
console.log(" ID:", contract.id);
|
|
23
|
+
console.log(" Name:", contract.name);
|
|
24
|
+
console.log(" Address:", contract.address);
|
|
25
|
+
console.log(" Chain ID:", contract.chainId);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Usage: npx tsx contracts/listContracts.ts
|
|
2
|
+
|
|
3
|
+
import Openfort from "@openfort/openfort-node";
|
|
4
|
+
import "dotenv/config";
|
|
5
|
+
|
|
6
|
+
const openfort = new Openfort(process.env.OPENFORT_API_KEY!, {
|
|
7
|
+
basePath: process.env.OPENFORT_BASE_URL,
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
// List all contracts
|
|
11
|
+
const result = await openfort.contracts.list({ limit: 10 });
|
|
12
|
+
|
|
13
|
+
console.log(`Found ${result.total} contracts:`);
|
|
14
|
+
for (const contract of result.data) {
|
|
15
|
+
console.log(` - ${contract.name} (${contract.id})`);
|
|
16
|
+
console.log(` Address: ${contract.address}`);
|
|
17
|
+
console.log(` Chain: ${contract.chainId}`);
|
|
18
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// Usage: npx tsx contracts/readContract.ts
|
|
2
|
+
|
|
3
|
+
import Openfort from "@openfort/openfort-node";
|
|
4
|
+
import "dotenv/config";
|
|
5
|
+
|
|
6
|
+
const openfort = new Openfort(process.env.OPENFORT_API_KEY!, {
|
|
7
|
+
basePath: process.env.OPENFORT_BASE_URL,
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
const chainId = Number(process.env.CHAIN_ID) || 80002;
|
|
11
|
+
|
|
12
|
+
// Create a contract reference first
|
|
13
|
+
const contract = await openfort.contracts.create({
|
|
14
|
+
name: "ERC20 Token",
|
|
15
|
+
chainId,
|
|
16
|
+
address: "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174", // USDC on Polygon
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
console.log("Contract ID:", contract.id);
|
|
20
|
+
|
|
21
|
+
// Read contract data (e.g., totalSupply)
|
|
22
|
+
const result = await openfort.contracts.read(contract.id, {
|
|
23
|
+
functionName: "totalSupply",
|
|
24
|
+
functionArgs: [],
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
console.log("\nContract read result:");
|
|
28
|
+
console.log(" Function: totalSupply");
|
|
29
|
+
console.log(" Result:", result.result);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Usage: npx tsx evm/accounts/createAccount.ts
|
|
2
|
+
|
|
3
|
+
import Openfort from "@openfort/openfort-node";
|
|
4
|
+
import "dotenv/config";
|
|
5
|
+
|
|
6
|
+
const openfort = new Openfort(process.env.OPENFORT_API_KEY!, {
|
|
7
|
+
basePath: process.env.OPENFORT_BASE_URL,
|
|
8
|
+
walletSecret: process.env.OPENFORT_WALLET_SECRET,
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
// Create an EVM backend account
|
|
12
|
+
const account = await openfort.accounts.evm.backend.create({
|
|
13
|
+
name: `MyWallet-${Date.now()}`,
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
console.log("Created EVM account:");
|
|
17
|
+
console.log(" ID:", account.id);
|
|
18
|
+
console.log(" Address:", account.address);
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// Usage: npx tsx evm/accounts/exportAccount.ts
|
|
2
|
+
|
|
3
|
+
import Openfort from "@openfort/openfort-node";
|
|
4
|
+
import "dotenv/config";
|
|
5
|
+
import { generatePrivateKey, privateKeyToAccount } from "viem/accounts";
|
|
6
|
+
|
|
7
|
+
const openfort = new Openfort(process.env.OPENFORT_API_KEY!, {
|
|
8
|
+
basePath: process.env.OPENFORT_BASE_URL,
|
|
9
|
+
walletSecret: process.env.OPENFORT_WALLET_SECRET,
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
// First import an account so we can export it
|
|
13
|
+
const originalPrivateKey = generatePrivateKey();
|
|
14
|
+
const originalAccount = privateKeyToAccount(originalPrivateKey);
|
|
15
|
+
|
|
16
|
+
console.log("Original private key:", originalPrivateKey);
|
|
17
|
+
console.log("Original address:", originalAccount.address);
|
|
18
|
+
|
|
19
|
+
const account = await openfort.accounts.evm.backend.import({
|
|
20
|
+
privateKey: originalPrivateKey,
|
|
21
|
+
name: "ExportTestWallet",
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
console.log("\nImported account ID:", account.id);
|
|
25
|
+
|
|
26
|
+
// Export the account's private key
|
|
27
|
+
const exportedPrivateKey = await openfort.accounts.evm.backend.export({
|
|
28
|
+
id: account.id,
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
console.log("\nExported private key:", `0x${exportedPrivateKey}`);
|
|
32
|
+
|
|
33
|
+
// Verify the exported key matches
|
|
34
|
+
const derivedAccount = privateKeyToAccount(`0x${exportedPrivateKey}`);
|
|
35
|
+
console.log("Derived address:", derivedAccount.address);
|
|
36
|
+
console.log(
|
|
37
|
+
"Keys match:",
|
|
38
|
+
originalPrivateKey.toLowerCase() === `0x${exportedPrivateKey}`.toLowerCase()
|
|
39
|
+
);
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// Usage: npx tsx evm/accounts/getAccount.ts
|
|
2
|
+
|
|
3
|
+
import Openfort from "@openfort/openfort-node";
|
|
4
|
+
import "dotenv/config";
|
|
5
|
+
|
|
6
|
+
const openfort = new Openfort(process.env.OPENFORT_API_KEY!, {
|
|
7
|
+
basePath: process.env.OPENFORT_BASE_URL,
|
|
8
|
+
walletSecret: process.env.OPENFORT_WALLET_SECRET,
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
// Create a player and account first
|
|
12
|
+
const player = await openfort.players.create({
|
|
13
|
+
name: `Player-${Date.now()}`,
|
|
14
|
+
});
|
|
15
|
+
console.log("Created player:", player.id);
|
|
16
|
+
|
|
17
|
+
const account = await openfort.accounts.evm.backend.create({
|
|
18
|
+
name: `MyWallet-${Date.now()}`,
|
|
19
|
+
});
|
|
20
|
+
console.log("Created account:", account.address);
|
|
21
|
+
|
|
22
|
+
// Retrieve account by ID
|
|
23
|
+
const accountById = await openfort.accounts.evm.backend.get({ id: account.id });
|
|
24
|
+
console.log("Retrieved account by ID:", accountById.address);
|
|
25
|
+
|
|
26
|
+
// Retrieve account by address
|
|
27
|
+
const accountByAddress = await openfort.accounts.evm.backend.get({
|
|
28
|
+
address: account.address,
|
|
29
|
+
});
|
|
30
|
+
console.log("Retrieved account by address:", accountByAddress.address);
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// Usage: npx tsx evm/accounts/importAccount.ts
|
|
2
|
+
|
|
3
|
+
import Openfort from "@openfort/openfort-node";
|
|
4
|
+
import "dotenv/config";
|
|
5
|
+
import { generatePrivateKey, privateKeyToAccount } from "viem/accounts";
|
|
6
|
+
|
|
7
|
+
const openfort = new Openfort(process.env.OPENFORT_API_KEY!, {
|
|
8
|
+
basePath: process.env.OPENFORT_BASE_URL,
|
|
9
|
+
walletSecret: process.env.OPENFORT_WALLET_SECRET,
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
// Generate a random private key for demonstration
|
|
13
|
+
const privateKey = generatePrivateKey();
|
|
14
|
+
const localAccount = privateKeyToAccount(privateKey);
|
|
15
|
+
|
|
16
|
+
console.log("Generated private key:", privateKey);
|
|
17
|
+
console.log("Expected address:", localAccount.address);
|
|
18
|
+
|
|
19
|
+
// Import the account to Openfort
|
|
20
|
+
const account = await openfort.accounts.evm.backend.import({
|
|
21
|
+
privateKey,
|
|
22
|
+
name: "ImportedWallet",
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
console.log("\nImported account:");
|
|
26
|
+
console.log(" ID:", account.id);
|
|
27
|
+
console.log(" Address:", account.address);
|
|
28
|
+
console.log(
|
|
29
|
+
" Addresses match:",
|
|
30
|
+
account.address.toLowerCase() === localAccount.address.toLowerCase()
|
|
31
|
+
);
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// Usage: npx tsx evm/accounts/listAccounts.ts
|
|
2
|
+
|
|
3
|
+
import Openfort from "@openfort/openfort-node";
|
|
4
|
+
import "dotenv/config";
|
|
5
|
+
|
|
6
|
+
const openfort = new Openfort(process.env.OPENFORT_API_KEY!, {
|
|
7
|
+
basePath: process.env.OPENFORT_BASE_URL,
|
|
8
|
+
walletSecret: process.env.OPENFORT_WALLET_SECRET,
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
// List all EVM backend accounts
|
|
12
|
+
const result = await openfort.accounts.evm.backend.list({ limit: 10 });
|
|
13
|
+
|
|
14
|
+
console.log(`Found ${result.total} EVM accounts:`);
|
|
15
|
+
for (const account of result.accounts) {
|
|
16
|
+
console.log(` - ${account.address} (${account.id})`);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Create a few more accounts
|
|
20
|
+
await openfort.accounts.evm.backend.create({ name: `Wallet-${Date.now()}-1` });
|
|
21
|
+
await openfort.accounts.evm.backend.create({ name: `Wallet-${Date.now()}-2` });
|
|
22
|
+
|
|
23
|
+
// List accounts with pagination
|
|
24
|
+
const moreAccounts = await openfort.accounts.evm.backend.list({
|
|
25
|
+
limit: 5,
|
|
26
|
+
skip: 0,
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
console.log(`\nPaginated results (first 5):`);
|
|
30
|
+
for (const account of moreAccounts.accounts) {
|
|
31
|
+
console.log(` - ${account.address}`);
|
|
32
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// Usage: npx tsx evm/signing/signHash.ts
|
|
2
|
+
|
|
3
|
+
import Openfort from "@openfort/openfort-node";
|
|
4
|
+
import "dotenv/config";
|
|
5
|
+
import { keccak256, toBytes } from "viem";
|
|
6
|
+
|
|
7
|
+
const openfort = new Openfort(process.env.OPENFORT_API_KEY!, {
|
|
8
|
+
basePath: process.env.OPENFORT_BASE_URL,
|
|
9
|
+
walletSecret: process.env.OPENFORT_WALLET_SECRET,
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
// Create a backend account
|
|
13
|
+
const account = await openfort.accounts.evm.backend.create({
|
|
14
|
+
name: `Wallet-${Date.now()}`,
|
|
15
|
+
});
|
|
16
|
+
console.log("Created account:", account.address);
|
|
17
|
+
|
|
18
|
+
// Create a hash to sign
|
|
19
|
+
const message = "Hello, Openfort!";
|
|
20
|
+
const hash = keccak256(toBytes(message));
|
|
21
|
+
|
|
22
|
+
console.log("\nOriginal message:", message);
|
|
23
|
+
console.log("Hash:", hash);
|
|
24
|
+
|
|
25
|
+
// Sign the hash directly
|
|
26
|
+
const signature = await account.sign({ hash });
|
|
27
|
+
|
|
28
|
+
console.log("Signature:", signature);
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// Usage: npx tsx evm/signing/signMessage.ts
|
|
2
|
+
|
|
3
|
+
import Openfort from "@openfort/openfort-node";
|
|
4
|
+
import "dotenv/config";
|
|
5
|
+
import { verifyMessage } from "viem";
|
|
6
|
+
|
|
7
|
+
const openfort = new Openfort(process.env.OPENFORT_API_KEY!, {
|
|
8
|
+
basePath: process.env.OPENFORT_BASE_URL,
|
|
9
|
+
walletSecret: process.env.OPENFORT_WALLET_SECRET,
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
// Create a backend account
|
|
13
|
+
const account = await openfort.accounts.evm.backend.create({
|
|
14
|
+
name: `Wallet-${Date.now()}`,
|
|
15
|
+
});
|
|
16
|
+
console.log("Created account:", account.address);
|
|
17
|
+
|
|
18
|
+
// Sign a message
|
|
19
|
+
const message = "Hello, Openfort!";
|
|
20
|
+
const signature = await account.signMessage({ message });
|
|
21
|
+
|
|
22
|
+
console.log("\nMessage:", message);
|
|
23
|
+
console.log("Signature:", signature);
|
|
24
|
+
|
|
25
|
+
// Verify the signature using viem
|
|
26
|
+
const isValid = await verifyMessage({
|
|
27
|
+
address: account.address,
|
|
28
|
+
message,
|
|
29
|
+
signature,
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
console.log("Signature valid:", isValid);
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// Usage: npx tsx evm/signing/signTransaction.ts
|
|
2
|
+
|
|
3
|
+
import Openfort from "@openfort/openfort-node";
|
|
4
|
+
import "dotenv/config";
|
|
5
|
+
import { parseEther, parseTransaction } from "viem";
|
|
6
|
+
|
|
7
|
+
const openfort = new Openfort(process.env.OPENFORT_API_KEY!, {
|
|
8
|
+
basePath: process.env.OPENFORT_BASE_URL,
|
|
9
|
+
walletSecret: process.env.OPENFORT_WALLET_SECRET,
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
// Create a backend account
|
|
13
|
+
const account = await openfort.accounts.evm.backend.create({
|
|
14
|
+
name: `Wallet-${Date.now()}`,
|
|
15
|
+
});
|
|
16
|
+
console.log("Created account:", account.address);
|
|
17
|
+
|
|
18
|
+
// Define a transaction to sign
|
|
19
|
+
const transaction = {
|
|
20
|
+
to: "0x70997970C51812dc3A010C7d01b50e0d17dc79C8" as const,
|
|
21
|
+
value: parseEther("0.001"),
|
|
22
|
+
nonce: 0,
|
|
23
|
+
gas: 21000n,
|
|
24
|
+
maxFeePerGas: 20000000000n,
|
|
25
|
+
maxPriorityFeePerGas: 1000000000n,
|
|
26
|
+
chainId: Number(process.env.CHAIN_ID) || 80002,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
console.log("\nTransaction to sign:");
|
|
30
|
+
console.log(" To:", transaction.to);
|
|
31
|
+
console.log(" Value:", transaction.value.toString(), "wei");
|
|
32
|
+
console.log(" Chain ID:", transaction.chainId);
|
|
33
|
+
|
|
34
|
+
// Sign the transaction
|
|
35
|
+
const signedTransaction = await account.signTransaction(transaction);
|
|
36
|
+
|
|
37
|
+
console.log("\nSigned transaction:", signedTransaction);
|
|
38
|
+
|
|
39
|
+
// Parse and display the signed transaction
|
|
40
|
+
const parsed = parseTransaction(signedTransaction);
|
|
41
|
+
console.log("\nParsed signed transaction:");
|
|
42
|
+
console.log(" To:", parsed.to);
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// Usage: npx tsx evm/signing/signTypedData.ts
|
|
2
|
+
|
|
3
|
+
import Openfort from "@openfort/openfort-node";
|
|
4
|
+
import "dotenv/config";
|
|
5
|
+
import { verifyTypedData } from "viem";
|
|
6
|
+
|
|
7
|
+
const openfort = new Openfort(process.env.OPENFORT_API_KEY!, {
|
|
8
|
+
basePath: process.env.OPENFORT_BASE_URL,
|
|
9
|
+
walletSecret: process.env.OPENFORT_WALLET_SECRET,
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
// Create a backend account
|
|
13
|
+
const account = await openfort.accounts.evm.backend.create({
|
|
14
|
+
name: `Wallet-${Date.now()}`,
|
|
15
|
+
});
|
|
16
|
+
console.log("Created account:", account.address);
|
|
17
|
+
|
|
18
|
+
// Define EIP-712 typed data
|
|
19
|
+
const domain = {
|
|
20
|
+
name: "Example App",
|
|
21
|
+
version: "1",
|
|
22
|
+
chainId: 1,
|
|
23
|
+
verifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC" as const,
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const types = {
|
|
27
|
+
Person: [
|
|
28
|
+
{ name: "name", type: "string" },
|
|
29
|
+
{ name: "wallet", type: "address" },
|
|
30
|
+
],
|
|
31
|
+
Mail: [
|
|
32
|
+
{ name: "from", type: "Person" },
|
|
33
|
+
{ name: "to", type: "Person" },
|
|
34
|
+
{ name: "contents", type: "string" },
|
|
35
|
+
],
|
|
36
|
+
} as const;
|
|
37
|
+
|
|
38
|
+
const message = {
|
|
39
|
+
from: {
|
|
40
|
+
name: "Alice",
|
|
41
|
+
wallet: "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826" as const,
|
|
42
|
+
},
|
|
43
|
+
to: {
|
|
44
|
+
name: "Bob",
|
|
45
|
+
wallet: "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB" as const,
|
|
46
|
+
},
|
|
47
|
+
contents: "Hello, Bob!",
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
// Sign the typed data
|
|
51
|
+
const signature = await account.signTypedData({
|
|
52
|
+
domain,
|
|
53
|
+
types,
|
|
54
|
+
primaryType: "Mail",
|
|
55
|
+
message,
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
console.log("\nTyped data signed successfully!");
|
|
59
|
+
console.log("Signature:", signature);
|
|
60
|
+
|
|
61
|
+
// Verify the signature
|
|
62
|
+
const isValid = await verifyTypedData({
|
|
63
|
+
address: account.address,
|
|
64
|
+
domain,
|
|
65
|
+
types,
|
|
66
|
+
primaryType: "Mail",
|
|
67
|
+
message,
|
|
68
|
+
signature,
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
console.log("Signature valid:", isValid);
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// Usage: npx tsx exchange/createSwap.ts
|
|
2
|
+
|
|
3
|
+
import Openfort from "@openfort/openfort-node";
|
|
4
|
+
import "dotenv/config";
|
|
5
|
+
|
|
6
|
+
const openfort = new Openfort(process.env.OPENFORT_API_KEY!, {
|
|
7
|
+
basePath: process.env.OPENFORT_BASE_URL,
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
const chainId = Number(process.env.CHAIN_ID) || 80002;
|
|
11
|
+
|
|
12
|
+
// Create an account (V1 legacy API)
|
|
13
|
+
const account = await openfort.accounts.v1.create({
|
|
14
|
+
chainId,
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
// Create a policy for the swap
|
|
18
|
+
const policy = await openfort.policies.create({
|
|
19
|
+
name: `SwapPolicy-${Date.now()}`,
|
|
20
|
+
chainId,
|
|
21
|
+
strategy: {
|
|
22
|
+
sponsorSchema: "pay_for_user",
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
await openfort.policyRules.create({
|
|
27
|
+
type: "account_functions",
|
|
28
|
+
policy: policy.id,
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
console.log("Account address:", account.address);
|
|
32
|
+
console.log("Policy ID:", policy.id);
|
|
33
|
+
|
|
34
|
+
// Create a swap transaction
|
|
35
|
+
// Note: Token addresses and amounts will vary by network
|
|
36
|
+
// The account must have sufficient balance for the swap
|
|
37
|
+
const swap = await openfort.exchange.createSwap({
|
|
38
|
+
fromAddress: account.id,
|
|
39
|
+
chainId,
|
|
40
|
+
tokenInAddress: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", // Native token
|
|
41
|
+
tokenOutAddress: "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174", // USDC
|
|
42
|
+
amount: "1000000000000000", // 0.001 native token
|
|
43
|
+
tradeType: "EXACT_INPUT",
|
|
44
|
+
policy: policy.id,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
console.log("\nSwap created:");
|
|
48
|
+
console.log(" Transaction Intent ID:", swap.id);
|
|
49
|
+
console.log(" Status:", swap.response?.status || "pending");
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// Usage: npx tsx exchange/quoteSwap.ts
|
|
2
|
+
|
|
3
|
+
import Openfort from "@openfort/openfort-node";
|
|
4
|
+
import "dotenv/config";
|
|
5
|
+
|
|
6
|
+
const openfort = new Openfort(process.env.OPENFORT_API_KEY!, {
|
|
7
|
+
basePath: process.env.OPENFORT_BASE_URL,
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
const chainId = Number(process.env.CHAIN_ID) || 80002;
|
|
11
|
+
|
|
12
|
+
// Create an account (V1 legacy API)
|
|
13
|
+
const account = await openfort.accounts.v1.create({
|
|
14
|
+
chainId,
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
console.log("Account address:", account.address);
|
|
18
|
+
|
|
19
|
+
// Get a quote for swapping tokens
|
|
20
|
+
// Note: Token addresses and amounts will vary by network
|
|
21
|
+
const quote = await openfort.exchange.quoteSwap({
|
|
22
|
+
fromAddress: account.id,
|
|
23
|
+
chainId,
|
|
24
|
+
tokenInAddress: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE", // Native token (ETH/MATIC)
|
|
25
|
+
tokenOutAddress: "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174", // USDC on Polygon
|
|
26
|
+
amount: "1000000000000000", // 0.001 native token
|
|
27
|
+
tradeType: "EXACT_INPUT",
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
console.log("\nSwap quote:");
|
|
31
|
+
console.log(" Amount:", quote.amount);
|
|
32
|
+
console.log(" Slippage:", quote.slippage);
|
|
33
|
+
console.log(" Estimated Gas Fee:", quote.estimatedTXGasFee);
|
|
34
|
+
console.log(" Estimated Gas Fee (USD):", quote.estimatedTXGasFeeUSD);
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Usage: npx tsx iam/deleteUser.ts <user_id>
|
|
2
|
+
|
|
3
|
+
import Openfort from "@openfort/openfort-node";
|
|
4
|
+
import "dotenv/config";
|
|
5
|
+
|
|
6
|
+
const openfort = new Openfort(process.env.OPENFORT_API_KEY!, {
|
|
7
|
+
basePath: process.env.OPENFORT_BASE_URL,
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
const userId = process.argv[2];
|
|
11
|
+
|
|
12
|
+
if (!userId) {
|
|
13
|
+
console.error("Usage: npx tsx iam/deleteUser.ts <user_id>");
|
|
14
|
+
process.exit(1);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// Delete a user (this will also delete all linked accounts and embedded signers)
|
|
18
|
+
const result = await openfort.iam.users.delete(userId);
|
|
19
|
+
|
|
20
|
+
console.log("User deleted:");
|
|
21
|
+
console.log(" ID:", result.id);
|
|
22
|
+
console.log(" Deleted:", result.deleted);
|