Skip to content

Commit

Permalink
Merge pull-request #339
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewkmin committed Aug 26, 2024
2 parents d8de1ba + b6e99be commit 7368b92
Show file tree
Hide file tree
Showing 12 changed files with 423 additions and 435 deletions.
1 change: 1 addition & 0 deletions examples/with-solana/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@solana/web3.js": "^1.88.1",
"@turnkey/api-key-stamper": "workspace:*",
"@turnkey/http": "workspace:*",
"@turnkey/sdk-server": "workspace:*",
"@turnkey/solana": "workspace:*",
"bs58": "^5.0.0",
"dotenv": "^16.0.3",
Expand Down
20 changes: 9 additions & 11 deletions examples/with-solana/src/advanced.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import {
TransactionMessage,
} from "@solana/web3.js";

import { TurnkeyClient } from "@turnkey/http";
import { ApiKeyStamper } from "@turnkey/api-key-stamper";
import { Turnkey } from "@turnkey/sdk-server";
import { TurnkeySigner } from "@turnkey/solana";

import { createNewSolanaWallet, solanaNetwork } from "./utils";
Expand All @@ -27,22 +26,21 @@ async function main() {

const connection = solanaNetwork.connect();

const turnkeyClient = new TurnkeyClient(
{ baseUrl: process.env.BASE_URL! },
new ApiKeyStamper({
apiPublicKey: process.env.API_PUBLIC_KEY!,
apiPrivateKey: process.env.API_PRIVATE_KEY!,
})
);
const turnkeyClient = new Turnkey({
apiBaseUrl: process.env.BASE_URL!,
apiPublicKey: process.env.API_PUBLIC_KEY!,
apiPrivateKey: process.env.API_PRIVATE_KEY!,
defaultOrganizationId: organizationId,
});

const turnkeySigner = new TurnkeySigner({
organizationId,
client: turnkeyClient,
client: turnkeyClient.apiClient(),
});

let solAddress = process.env.SOLANA_ADDRESS!;
if (!solAddress) {
solAddress = await createNewSolanaWallet(turnkeyClient, organizationId);
solAddress = await createNewSolanaWallet(turnkeyClient.apiClient());
console.log(`\nYour new Solana address: "${solAddress}"`);
} else {
console.log(`\nUsing existing Solana address from ENV: "${solAddress}"`);
Expand Down
20 changes: 9 additions & 11 deletions examples/with-solana/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import bs58 from "bs58";
import { input, confirm } from "@inquirer/prompts";
import type { Transaction } from "@solana/web3.js";

import { TurnkeyClient } from "@turnkey/http";
import { ApiKeyStamper } from "@turnkey/api-key-stamper";
import { Turnkey } from "@turnkey/sdk-server";
import { TurnkeySigner } from "@turnkey/solana";

const TURNKEY_WAR_CHEST = "tkhqC9QX2gkqJtUFk2QKhBmQfFyyqZXSpr73VFRi35C";
Expand All @@ -27,22 +26,21 @@ async function main() {

const connection = solanaNetwork.connect();

const turnkeyClient = new TurnkeyClient(
{ baseUrl: process.env.BASE_URL! },
new ApiKeyStamper({
apiPublicKey: process.env.API_PUBLIC_KEY!,
apiPrivateKey: process.env.API_PRIVATE_KEY!,
})
);
const turnkeyClient = new Turnkey({
apiBaseUrl: process.env.BASE_URL!,
apiPublicKey: process.env.API_PUBLIC_KEY!,
apiPrivateKey: process.env.API_PRIVATE_KEY!,
defaultOrganizationId: organizationId,
});

const turnkeySigner = new TurnkeySigner({
organizationId,
client: turnkeyClient,
client: turnkeyClient.apiClient(),
});

let solAddress = process.env.SOLANA_ADDRESS!;
if (!solAddress) {
solAddress = await createNewSolanaWallet(turnkeyClient, organizationId);
solAddress = await createNewSolanaWallet(turnkeyClient.apiClient());
console.log(`\nYour new Solana address: "${solAddress}"`);
} else {
console.log(`\nUsing existing Solana address from ENV: "${solAddress}"`);
Expand Down
21 changes: 10 additions & 11 deletions examples/with-solana/src/tokenTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import { confirm } from "@inquirer/prompts";
import { PublicKey } from "@solana/web3.js";
import { getAccount, getAssociatedTokenAddress } from "@solana/spl-token";

import { TurnkeyClient } from "@turnkey/http";
import { ApiKeyStamper } from "@turnkey/api-key-stamper";
import { Turnkey } from "@turnkey/sdk-server";
import { TurnkeySigner } from "@turnkey/solana";

import {
Expand All @@ -26,22 +25,22 @@ async function main() {
const turnkeyWarchest = new PublicKey(TURNKEY_WAR_CHEST);
const organizationId = process.env.ORGANIZATION_ID!;
const connection = solanaNetwork.connect();
const turnkeyClient = new TurnkeyClient(
{ baseUrl: process.env.BASE_URL! },
new ApiKeyStamper({
apiPublicKey: process.env.API_PUBLIC_KEY!,
apiPrivateKey: process.env.API_PRIVATE_KEY!,
})
);

const turnkeyClient = new Turnkey({
apiBaseUrl: process.env.BASE_URL!,
apiPublicKey: process.env.API_PUBLIC_KEY!,
apiPrivateKey: process.env.API_PRIVATE_KEY!,
defaultOrganizationId: organizationId,
});

const turnkeySigner = new TurnkeySigner({
organizationId,
client: turnkeyClient,
client: turnkeyClient.apiClient(),
});

let solAddress = process.env.SOLANA_ADDRESS!;
if (!solAddress) {
solAddress = await createNewSolanaWallet(turnkeyClient, organizationId);
solAddress = await createNewSolanaWallet(turnkeyClient.apiClient());
console.log(`\nYour new Solana address: "${solAddress}"`);
} else {
console.log(`\nUsing existing Solana address from ENV: "${solAddress}"`);
Expand Down
58 changes: 18 additions & 40 deletions examples/with-solana/src/utils/createSolanaWallet.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,35 @@
import {
TurnkeyActivityError,
TurnkeyClient,
createActivityPoller,
} from "@turnkey/http";
import { TurnkeyActivityError } from "@turnkey/http";
import type { TurnkeyApiClient } from "@turnkey/sdk-server";
import * as crypto from "crypto";

export async function createNewSolanaWallet(
client: TurnkeyClient,
turnkeyOrganizationId: string
) {
export async function createNewSolanaWallet(client: TurnkeyApiClient) {
console.log("creating a new Solana wallet in your Turnkey organization...\n");

const walletName = `Solana Wallet ${crypto.randomBytes(2).toString("hex")}`;

try {
const activityPoller = createActivityPoller({
client: client,
requestFn: client.createWallet,
const response = await client.createWallet({
walletName,
accounts: [
{
pathFormat: "PATH_FORMAT_BIP32",
// https://github.com/satoshilabs/slips/blob/master/slip-0044.md
path: "m/44'/501'/0'/0'",
curve: "CURVE_ED25519",
addressFormat: "ADDRESS_FORMAT_SOLANA",
},
],
});

const completedActivity = await activityPoller({
type: "ACTIVITY_TYPE_CREATE_WALLET",
organizationId: turnkeyOrganizationId,
parameters: {
walletName,
accounts: [
{
pathFormat: "PATH_FORMAT_BIP32",
// https://github.com/satoshilabs/slips/blob/master/slip-0044.md
path: "m/44'/501'/0'/0'",
curve: "CURVE_ED25519",
addressFormat: "ADDRESS_FORMAT_SOLANA",
},
],
},
timestampMs: String(Date.now()), // millisecond timestamp
});

const walletId = completedActivity.result.createWalletResult?.walletId;
const walletId = response.walletId;
if (!walletId) {
console.error(
"activity doesn't contain a valid wallet ID",
completedActivity
);
console.error("response doesn't contain a valid wallet ID");
process.exit(1);
}

const address = completedActivity.result.createWalletResult?.addresses[0];
const address = response.addresses[0];
if (!address) {
console.error(
"activity result doesn't contain a valid address",
completedActivity
);
console.error("response doesn't contain a valid address");
process.exit(1);
}

Expand Down
7 changes: 7 additions & 0 deletions examples/with-wallet-stamper/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# with-wallet-stamper

## 0.1.5

### Patch Changes

- Updated dependencies [c342954]
- @turnkey/solana@0.4.0

## 0.1.4

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion examples/with-wallet-stamper/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "with-wallet-stamper",
"version": "0.1.4",
"version": "0.1.5",
"private": true,
"type": "module",
"scripts": {
Expand Down
6 changes: 6 additions & 0 deletions packages/solana/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @turnkey/solana

## 0.4.0

### Minor Changes

- c342954: Add compatibility with @turnkey/sdk-server and @turnkey/sdk-browser

## 0.3.10

### Patch Changes
Expand Down
6 changes: 4 additions & 2 deletions packages/solana/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@turnkey/solana",
"version": "0.3.10",
"version": "0.4.0",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
"exports": {
Expand Down Expand Up @@ -49,7 +49,9 @@
},
"dependencies": {
"@solana/web3.js": "^1.88.1",
"@turnkey/http": "workspace:*"
"@turnkey/http": "workspace:*",
"@turnkey/sdk-browser": "workspace:*",
"@turnkey/sdk-server": "workspace:*"
},
"devDependencies": {
"@turnkey/api-key-stamper": "workspace:*",
Expand Down
Loading

0 comments on commit 7368b92

Please sign in to comment.