From 00100cc93cf0f1d642a1b2c83403a0f937b03690 Mon Sep 17 00:00:00 2001 From: Omkar Shanbhag Date: Wed, 11 Dec 2024 16:00:01 -0800 Subject: [PATCH] add ata derivation to solana package --- examples/with-solana/src/utils/deriveAta.ts | 14 ------ packages/solana/package.json | 1 + packages/solana/src/index.ts | 19 ++++++++ pnpm-lock.yaml | 50 +++++++++++++++++++++ 4 files changed, 70 insertions(+), 14 deletions(-) delete mode 100644 examples/with-solana/src/utils/deriveAta.ts diff --git a/examples/with-solana/src/utils/deriveAta.ts b/examples/with-solana/src/utils/deriveAta.ts deleted file mode 100644 index c84440029..000000000 --- a/examples/with-solana/src/utils/deriveAta.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { PublicKey } from "@solana/web3.js"; -import { getAssociatedTokenAddressSync } from "@solana/spl-token"; - -export function getAssociatedTokenAddress(mintAddress: string, walletAddress:string) { - const mintPublicKey = new PublicKey(mintAddress); - const walletPublicKey = new PublicKey(walletAddress); - - const associatedTokenAddress = getAssociatedTokenAddressSync( - mintPublicKey, - walletPublicKey - ); - - return associatedTokenAddress.toString(); -} \ No newline at end of file diff --git a/packages/solana/package.json b/packages/solana/package.json index 93544a10f..b0cdaae7d 100644 --- a/packages/solana/package.json +++ b/packages/solana/package.json @@ -48,6 +48,7 @@ "typecheck": "tsc -p tsconfig.typecheck.json" }, "dependencies": { + "@solana/spl-token": "^0.4.8", "@solana/web3.js": "^1.88.1", "@turnkey/http": "workspace:*", "@turnkey/sdk-browser": "workspace:*", diff --git a/packages/solana/src/index.ts b/packages/solana/src/index.ts index 17f451022..7e08c251a 100644 --- a/packages/solana/src/index.ts +++ b/packages/solana/src/index.ts @@ -1,4 +1,5 @@ import { PublicKey, Transaction, VersionedTransaction } from "@solana/web3.js"; +import { getAssociatedTokenAddressSync } from "@solana/spl-token"; import { assertNonNull, assertActivityCompleted, @@ -131,6 +132,24 @@ export class TurnkeySigner { return recoveredTransaction; } + /** + * This function derives a Solana associated token address for a given Solana wallet address, related to a particular token mint address + * + * @param walletAddress string representation of the Solana wallet address (base58 encoded) + * @param mintAddress string representation of the token mint address (base58 encoded) + */ + public getAssociatedTokenAddress(mintAddress: string, walletAddress:string) { + const mintPublicKey = new PublicKey(mintAddress); + const walletPublicKey = new PublicKey(walletAddress); + + const associatedTokenAddress = getAssociatedTokenAddressSync( + mintPublicKey, + walletPublicKey + ); + + return associatedTokenAddress.toString(); +} + private async signTransactionImpl( unsignedTransaction: string, signWith: string, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index fd397ad93..761105c01 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1764,6 +1764,9 @@ importers: packages/solana: dependencies: + '@solana/spl-token': + specifier: ^0.4.8 + version: 0.4.8(@solana/web3.js@1.88.1)(fastestsmallesttextencoderdecoder@1.0.22)(typescript@5.1.5) '@solana/web3.js': specifier: ^1.88.1 version: 1.88.1 @@ -10375,6 +10378,20 @@ packages: - fastestsmallesttextencoderdecoder dev: false + /@solana/spl-token-group@0.0.5(@solana/web3.js@1.88.1)(fastestsmallesttextencoderdecoder@1.0.22)(typescript@5.1.5): + resolution: {integrity: sha512-CLJnWEcdoUBpQJfx9WEbX3h6nTdNiUzswfFdkABUik7HVwSNA98u5AYvBVK2H93d9PGMOHAak2lHW9xr+zAJGQ==} + engines: {node: '>=16'} + peerDependencies: + '@solana/web3.js': ^1.94.0 + dependencies: + '@solana/codecs': 2.0.0-preview.4(fastestsmallesttextencoderdecoder@1.0.22)(typescript@5.1.5) + '@solana/spl-type-length-value': 0.1.0 + '@solana/web3.js': 1.88.1 + transitivePeerDependencies: + - fastestsmallesttextencoderdecoder + - typescript + dev: false + /@solana/spl-token-group@0.0.5(@solana/web3.js@1.95.1)(fastestsmallesttextencoderdecoder@1.0.22)(typescript@5.1.5): resolution: {integrity: sha512-CLJnWEcdoUBpQJfx9WEbX3h6nTdNiUzswfFdkABUik7HVwSNA98u5AYvBVK2H93d9PGMOHAak2lHW9xr+zAJGQ==} engines: {node: '>=16'} @@ -10389,6 +10406,19 @@ packages: - typescript dev: false + /@solana/spl-token-metadata@0.1.4(@solana/web3.js@1.88.1)(fastestsmallesttextencoderdecoder@1.0.22): + resolution: {integrity: sha512-N3gZ8DlW6NWDV28+vCCDJoTqaCZiF/jDUnk3o8GRkAFzHObiR60Bs1gXHBa8zCPdvOwiG6Z3dg5pg7+RW6XNsQ==} + engines: {node: '>=16'} + peerDependencies: + '@solana/web3.js': ^1.91.6 + dependencies: + '@solana/codecs': 2.0.0-preview.2(fastestsmallesttextencoderdecoder@1.0.22) + '@solana/spl-type-length-value': 0.1.0 + '@solana/web3.js': 1.88.1 + transitivePeerDependencies: + - fastestsmallesttextencoderdecoder + dev: false + /@solana/spl-token-metadata@0.1.4(@solana/web3.js@1.95.1)(fastestsmallesttextencoderdecoder@1.0.22): resolution: {integrity: sha512-N3gZ8DlW6NWDV28+vCCDJoTqaCZiF/jDUnk3o8GRkAFzHObiR60Bs1gXHBa8zCPdvOwiG6Z3dg5pg7+RW6XNsQ==} engines: {node: '>=16'} @@ -10402,6 +10432,26 @@ packages: - fastestsmallesttextencoderdecoder dev: false + /@solana/spl-token@0.4.8(@solana/web3.js@1.88.1)(fastestsmallesttextencoderdecoder@1.0.22)(typescript@5.1.5): + resolution: {integrity: sha512-RO0JD9vPRi4LsAbMUdNbDJ5/cv2z11MGhtAvFeRzT4+hAGE/FUzRi0tkkWtuCfSIU3twC6CtmAihRp/+XXjWsA==} + engines: {node: '>=16'} + peerDependencies: + '@solana/web3.js': ^1.94.0 + dependencies: + '@solana/buffer-layout': 4.0.1 + '@solana/buffer-layout-utils': 0.2.0 + '@solana/spl-token-group': 0.0.5(@solana/web3.js@1.88.1)(fastestsmallesttextencoderdecoder@1.0.22)(typescript@5.1.5) + '@solana/spl-token-metadata': 0.1.4(@solana/web3.js@1.88.1)(fastestsmallesttextencoderdecoder@1.0.22) + '@solana/web3.js': 1.88.1 + buffer: 6.0.3 + transitivePeerDependencies: + - bufferutil + - encoding + - fastestsmallesttextencoderdecoder + - typescript + - utf-8-validate + dev: false + /@solana/spl-token@0.4.8(@solana/web3.js@1.95.1)(fastestsmallesttextencoderdecoder@1.0.22)(typescript@5.1.5): resolution: {integrity: sha512-RO0JD9vPRi4LsAbMUdNbDJ5/cv2z11MGhtAvFeRzT4+hAGE/FUzRi0tkkWtuCfSIU3twC6CtmAihRp/+XXjWsA==} engines: {node: '>=16'}