Skip to content

Commit

Permalink
working implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
omkarshanbhag committed Dec 12, 2024
1 parent 2922413 commit 761d40c
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 45 deletions.
16 changes: 7 additions & 9 deletions examples/with-solana/src/allowTokenTransferPolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,14 @@ import {
createNewSolanaWallet,
createToken,
createTokenAccount,
createTokenTransfer,
createTokenTransferSignTransaction,
solanaNetwork,
TURNKEY_WAR_CHEST,
} from "./utils";

import keys from "./keys";

import {
createUser,
createPolicy,
} from "./requests";
import { createUser, createPolicy } from "./requests";

async function main() {
const turnkeyWarchest = new PublicKey(TURNKEY_WAR_CHEST);
Expand Down Expand Up @@ -132,8 +129,8 @@ async function main() {
tokenAccount.address,
mintAuthority.publicKey
);
// Create non-root user

// Create non-root user
let nonRootUserID = await createUser(
turnkeyClient.apiClient(),
"Non Root User",
Expand Down Expand Up @@ -163,8 +160,9 @@ async function main() {
`solana.tx.spl_transfers.any(transfer, transfer.to == '${tokenAccountWarchest.address}')`
);

// Transfer token from primary to Warchest
await createTokenTransfer(
// Transfer token from primary to Warchest associated token account\
// This call uses Turnkey's sign transaction endpoint which passes the transaction through the policy engine
await createTokenTransferSignTransaction(
nonRootUserSigner,
connection,
solAddress,
Expand Down
19 changes: 9 additions & 10 deletions examples/with-solana/src/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
// NOTE: Only used for demo purposes

const keys: { [key: string]: { [key: string]: string } } = {
nonRootUser: {
publicKey:
"03178b1cb727bd1ab23c3c5725d633f9ad3c7c0502efda2371e4e4bd88aea8a94d",
privateKey:
"26e53957146d5d5b5612f09518b8bb40deddcf28d378270101227cb0a231969e",
},
};

export default keys;

nonRootUser: {
publicKey:
"03178b1cb727bd1ab23c3c5725d633f9ad3c7c0502efda2371e4e4bd88aea8a94d",
privateKey:
"26e53957146d5d5b5612f09518b8bb40deddcf28d378270101227cb0a231969e",
},
};

export default keys;
5 changes: 2 additions & 3 deletions examples/with-solana/src/requests/createPolicy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
type TurnkeyServerClient,
TurnkeyActivityError,
type TurnkeyServerClient,
TurnkeyActivityError,
} from "@turnkey/sdk-server";

import { refineNonNull } from "./utils";
Expand Down Expand Up @@ -49,4 +49,3 @@ export default async function createPolicy(
});
}
}

2 changes: 1 addition & 1 deletion examples/with-solana/src/requests/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { default as createUser } from "./createUser";
export { default as createPolicy } from "./createPolicy";
export { default as createPolicy } from "./createPolicy";
17 changes: 8 additions & 9 deletions examples/with-solana/src/requests/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
export function refineNonNull<T>(
input: T | null | undefined,
errorMessage?: string
): T {
if (input == null) {
throw new Error(errorMessage ?? `Unexpected ${JSON.stringify(input)}`);
}

return input;
input: T | null | undefined,
errorMessage?: string
): T {
if (input == null) {
throw new Error(errorMessage ?? `Unexpected ${JSON.stringify(input)}`);
}


return input;
}
5 changes: 1 addition & 4 deletions examples/with-solana/src/tokenTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ import { getAccount, getAssociatedTokenAddress } from "@solana/spl-token";
import { Turnkey } from "@turnkey/sdk-server";
import { TurnkeySigner } from "@turnkey/solana";

import {
createUser,
createPolicy,
} from "./requests";
import { createUser, createPolicy } from "./requests";

import {
createMint,
Expand Down
42 changes: 34 additions & 8 deletions examples/with-solana/src/utils/createTokenTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,43 @@ export async function createTokenTransfer(
// Set the signer
transferTx.feePayer = fromKey;

// const serializedTransaction = transferTx.serialize({
// requireAllSignatures: false,
// verifySignatures: false
// });

// const hexString = serializedTransaction.toString('hex');
// console.log("Transaction hex:", hexString);

await turnkeySigner.addSignature(transferTx, solAddress);

console.log("Broadcasting token transfer transaction...");

await solanaNetwork.broadcast(connection, transferTx);
}

export async function createTokenTransferSignTransaction(
turnkeySigner: TurnkeySigner,
connection: Connection,
solAddress: string,
tokenAccountPubkey: PublicKey,
mintAuthority: PublicKey,
ataRecipient: PublicKey
): Promise<any> {
const fromKey = new PublicKey(solAddress);

let transferTx = new Transaction().add(
createTransferCheckedInstruction(
tokenAccountPubkey, // from (should be a token account)
mintAuthority, // mint
ataRecipient, // to (should be a token account)
fromKey, // from's owner
1e4, // amount, if your deciamls is 8, send 10^8 for 1 token
8 // decimals
)
);

// Get a recent block hash
transferTx.recentBlockhash = await solanaNetwork.recentBlockhash();
// Set the signer
transferTx.feePayer = fromKey;

// Use Turnkey's sign transaction endpoint that passes the created transaction through the policy engine
let signedTx = await turnkeySigner.signTransaction(transferTx, solAddress);

console.log("Broadcasting token transfer transaction...");

await solanaNetwork.broadcast(connection, signedTx);
}
6 changes: 5 additions & 1 deletion examples/with-solana/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import { print } from "./print";
import { createMint } from "./createMint";
import { createToken } from "./createToken";
import { createTokenAccount } from "./createTokenAccount";
import { createTokenTransfer } from "./createTokenTransfer";
import {
createTokenTransfer,
createTokenTransferSignTransaction,
} from "./createTokenTransfer";
import { transactionSenderAndConfirmationWaiter } from "./retrySender";
import { handleActivityError } from "./handleActivityError";

Expand All @@ -18,6 +21,7 @@ export {
createToken,
createTokenAccount,
createTokenTransfer,
createTokenTransferSignTransaction,
createTransfer,
handleActivityError,
print,
Expand Down

0 comments on commit 761d40c

Please sign in to comment.