@@ -24,13 +24,12 @@ import {
2424import { createUmi } from '@metaplex-foundation/umi-bundle-defaults'
2525import { createWeb3JsEddsa } from '@metaplex-foundation/umi-eddsa-web3js'
2626import { toWeb3JsInstruction , toWeb3JsPublicKey } from '@metaplex-foundation/umi-web3js-adapters'
27- import { AddressLookupTableAccount , Connection , Keypair } from '@solana/web3.js'
27+ import { AddressLookupTableAccount , Connection } from '@solana/web3.js'
2828import { getSimulationComputeUnits } from '@solana-developers/helpers'
29- import bs58 from 'bs58'
3029import { backOff } from 'exponential-backoff'
3130
3231import { formatEid } from '@layerzerolabs/devtools'
33- import { getPrioritizationFees } from '@layerzerolabs/devtools-solana'
32+ import { getPrioritizationFees , getSolanaKeypair } from '@layerzerolabs/devtools-solana'
3433import { promptToContinue } from '@layerzerolabs/io-devtools'
3534import { EndpointId , endpointIdToNetwork } from '@layerzerolabs/lz-definitions'
3635import { OftPDA } from '@layerzerolabs/oft-v2-solana-sdk'
@@ -42,29 +41,16 @@ const LOOKUP_TABLE_ADDRESS: Partial<Record<EndpointId, PublicKey>> = {
4241 [ EndpointId . SOLANA_V2_TESTNET ] : publicKey ( '9thqPdbR27A1yLWw2spwJLySemiGMXxPnEvfmXVk4KuK' ) ,
4342}
4443
45- const getFromEnv = ( key : string ) : string => {
46- const value = process . env [ key ]
47- if ( ! value ) {
48- throw new Error ( `${ key } is not defined in the environment variables.` )
49- }
50- return value
51- }
52-
53- /**
54- * Extracts the SOLANA_PRIVATE_KEY from the environment. This is purposely not exported for encapsulation purposes.
55- */
56- const getSolanaPrivateKeyFromEnv = ( ) => getFromEnv ( 'SOLANA_PRIVATE_KEY' )
57-
5844/**
5945 * Derive common connection and UMI objects for a given endpoint ID.
6046 * @param eid {EndpointId}
6147 */
6248export const deriveConnection = async ( eid : EndpointId , readOnly = false ) => {
63- const privateKey = readOnly ? bs58 . encode ( Keypair . generate ( ) . secretKey ) : getSolanaPrivateKeyFromEnv ( )
49+ const keypair = await getSolanaKeypair ( readOnly )
6450 const connectionFactory = createSolanaConnectionFactory ( )
6551 const connection = await connectionFactory ( eid )
6652 const umi = createUmi ( connection . rpcEndpoint ) . use ( mplToolbox ( ) )
67- const umiWalletKeyPair = umi . eddsa . createKeypairFromSecretKey ( bs58 . decode ( privateKey ) )
53+ const umiWalletKeyPair = umi . eddsa . createKeypairFromSecretKey ( keypair . secretKey )
6854 const umiWalletSigner = createSignerFromKeypair ( umi , umiWalletKeyPair )
6955 umi . use ( signerIdentity ( umiWalletSigner ) )
7056 return {
@@ -75,10 +61,8 @@ export const deriveConnection = async (eid: EndpointId, readOnly = false) => {
7561 }
7662}
7763
78- export const useWeb3Js = ( ) => {
79- const privateKey = getSolanaPrivateKeyFromEnv ( )
80- const secretKeyBytes = bs58 . decode ( privateKey )
81- const keypair = Keypair . fromSecretKey ( secretKeyBytes )
64+ export const useWeb3Js = async ( ) => {
65+ const keypair = await getSolanaKeypair ( )
8266 return {
8367 web3JsKeypair : keypair ,
8468 }
0 commit comments