Skip to content

Commit 10af99a

Browse files
authored
remove buffer since it's not polifilled in some envs (#43)
* remove buffer since it's not polifilled in some envs * fix format
1 parent b1d4c8b commit 10af99a

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

packages/blinks/src/hooks/solana/useActionSolanaWalletAdapter.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { useWalletModal } from '@solana/wallet-adapter-react-ui';
1212
import { Connection, VersionedTransaction } from '@solana/web3.js';
1313
import bs58 from 'bs58';
1414
import { useMemo } from 'react';
15+
import { decodeBase64 } from '../../utils/base64';
1516
/**
1617
* Hook to create an action adapter using solana's wallet adapter.
1718
*
@@ -64,7 +65,7 @@ export function useActionSolanaWalletAdapter(
6465
signTransaction: async (txData: string) => {
6566
try {
6667
const tx = await wallet.sendTransaction(
67-
VersionedTransaction.deserialize(Buffer.from(txData, 'base64')),
68+
VersionedTransaction.deserialize(decodeBase64(txData)),
6869
finalConnection,
6970
);
7071
return { signature: tx };
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// This approach is written in MDN.
2+
// btoa does not support utf-8 characters. So we need a little bit hack.
3+
export const encodeBase64 = (buf: ArrayBufferLike): string => {
4+
let binary = '';
5+
const bytes = new Uint8Array(buf);
6+
for (let i = 0, len = bytes.length; i < len; i++) {
7+
binary += String.fromCharCode(bytes[i]);
8+
}
9+
return btoa(binary);
10+
};
11+
12+
// atob does not support utf-8 characters. So we need a little bit hack.
13+
export const decodeBase64 = (str: string): Uint8Array => {
14+
const binary = atob(str);
15+
const bytes = new Uint8Array(new ArrayBuffer(binary.length));
16+
const half = binary.length / 2;
17+
for (let i = 0, j = binary.length - 1; i <= half; i++, j--) {
18+
bytes[i] = binary.charCodeAt(i);
19+
bytes[j] = binary.charCodeAt(j);
20+
}
21+
return bytes;
22+
};

0 commit comments

Comments
 (0)