So I have a option on my site to "send" xy amount of custom solana token with connection to phantom wallet. This exact same function works in localhost but not when I upload it to my server and go to the website through the ip adress. How is this even possible when this code is executed client-side in the same browser and the code is the same but when using the website hosted on the server I get: TypeError: Cannot read properties of undefined (reading 'digest') In function myToken.getOrCreateAssociatedAccountInfo Using
<script src="https://unpkg.com/@solana/web3.js@latest/lib/index.iife.min.js"></script>
<script src="https://unpkg.com/@solana/spl-token@latest/lib/index.iife.min.js"></script>
In the headers
Here is the code:
function sendCOIN(amount){
return new Promise(async(resolve, reject) => {
const publicKey = window.solana._publicKey
const toWallet = new web3.PublicKey("myWalletPublicKey")
const myMint = new web3.PublicKey("myMintAdress");
const myToken = new splToken.Token(
connection,
myMint,
splToken.TOKEN_PROGRAM_ID,
publicKey
);
const createTransferTransaction = async () => {
const fromTokenAccount = await myToken.getOrCreateAssociatedAccountInfo(
publicKey
)
const toTokenAccount = await myToken.getOrCreateAssociatedAccountInfo(
toWallet
)
let transaction = new web3.Transaction().add(
splToken.Token.createTransferInstruction(
splToken.TOKEN_PROGRAM_ID,
fromTokenAccount.address,
toTokenAccount.address,
publicKey,
[],
web3.LAMPORTS_PER_SOL*amount
)
);
transaction.feePayer = publicKey;
console.log("Getting recent blockhash");
const anyTransaction = transaction;
anyTransaction.recentBlockhash = (
await connection.getRecentBlockhash()
).blockhash;
return transaction;
};
const sendTransaction = async () => {
try {
const transaction = await createTransferTransaction();
if (!transaction) return;
console.log("Sending transaction!")
const { signature } = await window.solana.signAndSendTransaction(transaction);
console.log("Submitted transaction " + signature + ", awaiting confirmation");
await connection.confirmTransaction(signature);
console.log("Transaction " + signature + " confirmed");
return resolve({signature, publicKey: publicKey.toString()})
} catch (err) {
console.warn(err);
console.log("[error] sendTransaction: " + JSON.stringify(err));
return reject(500)
}
};
sendTransaction()
});
}