Skip to content

Commit

Permalink
feat: support okx on testnet
Browse files Browse the repository at this point in the history
Signed-off-by: Gregory Hill <[email protected]>
  • Loading branch information
gregdhill committed Sep 6, 2024
1 parent ae876d9 commit 6c8405f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 33 deletions.
65 changes: 36 additions & 29 deletions packages/sats-wagmi/src/connectors/okx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import { PsbtInputAccounts, SatsConnector } from './base';

type WalletNetwork = 'livenet' | 'testnet';

const getLibNetwork = (network: WalletNetwork): Network => {
switch (network) {
case 'livenet':
return Network.mainnet;
case 'testnet':
return Network.testnet;
}
};
// const getLibNetwork = (network: WalletNetwork): Network => {
// switch (network) {
// case 'livenet':
// return Network.mainnet;
// case 'testnet':
// return Network.testnet;
// }
// };

type AccountChangeEventParams = { address: string; publicKey: string; compressedPublicKey: string };

Expand Down Expand Up @@ -74,10 +74,15 @@ type OKXWallet = {
) => Promise<string>;
};

type OKXWalletTestnet = Pick<OKXWallet, 'connect' | 'signMessage' | 'signPsbt'>;

declare global {
interface Window {
okxwallet: {
bitcoin: OKXWallet;
// NOTE: this doesn't work right now because it defaults to p2tr
// but we have public key so could transform?
bitcoinTestnet: OKXWalletTestnet;
};
}
}
Expand All @@ -87,37 +92,38 @@ class OKXConnector extends SatsConnector {
super(network, 'OKX Wallet', 'OKX Wallet', 'https://www.okx.com/web3', okxLogo);
}

private getSource() {
return this.network === Network.mainnet ? window?.okxwallet?.bitcoin : window?.okxwallet?.bitcoinTestnet;
}

async connect(): Promise<void> {
const network = await window.okxwallet.bitcoin.getNetwork();
const mappedNetwork = getLibNetwork(network);
// const network = await window.okxwallet.bitcoin.getNetwork();
// const mappedNetwork = getLibNetwork(network);

if (mappedNetwork !== this.network) {
throw new Error(`Invalid Network. Please switch to Bitcoin ${this.network}.`);
}
// if (mappedNetwork !== this.network) {
// throw new Error(`Invalid Network. Please switch to Bitcoin ${this.network}.`);
// }

const [accounts, publickKey] = await Promise.all([
window.okxwallet.bitcoin.requestAccounts(),
window.okxwallet.bitcoin.getPublicKey()
]);
const { address, publicKey } = await this.getSource().connect();

this.paymentAddress = accounts[0];
this.ordinalsAddress = accounts[0];
this.publicKey = publickKey;
this.paymentAddress = address;
this.publicKey = publicKey;

window.okxwallet.bitcoin.on('accountChanged', this.changeAccount);
// window.okxwallet.bitcoin.on('accountChanged', this.changeAccount);
}

signMessage(message: string) {
return window.okxwallet.bitcoin.signMessage(message);
return this.getSource().signMessage(message);
}

on(callback: (account: string) => void): void {
window.okxwallet.bitcoin.on('accountChanged', ({ address, publicKey, compressedPublicKey }) => {
callback(address);
/** @dev only for mainnet */
// on(callback: (account: string) => void): void {
// window.okxwallet.bitcoin.on('accountChanged', ({ address, publicKey, compressedPublicKey }) => {
// callback(address);

this.changeAccount({ address, publicKey, compressedPublicKey });
});
}
// this.changeAccount({ address, publicKey, compressedPublicKey });
// });
// }

removeListener(): void {}

Expand All @@ -132,6 +138,7 @@ class OKXConnector extends SatsConnector {
return this.ready;
}

/** @dev only for mainnet */
async sendToAddress(toAddress: string, amount: number): Promise<string> {
return window.okxwallet.bitcoin.sendBitcoin(toAddress, amount);
}
Expand Down Expand Up @@ -161,7 +168,7 @@ class OKXConnector extends SatsConnector {
};
});

const signedPsbtHex = await window.okxwallet.bitcoin.signPsbt(psbtHex, {
const signedPsbtHex = await this.getSource().signPsbt(psbtHex, {
autoFinalized: false,
toSignInputs: toSignInputs
});
Expand Down
6 changes: 2 additions & 4 deletions packages/sats-wagmi/src/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,9 @@ const SatsWagmiConfig: FC<SatsWagmiConfigProps> = ({ children, queryClient, netw
const init = () => {
const readyConnectors: SatsConnector[] = [];

if (network === 'mainnet') {
const okx = new OKXConnector(network);
const okx = new OKXConnector(network);

readyConnectors.push(okx);
}
readyConnectors.push(okx);

const xverse = new XverseConnector(network);

Expand Down

0 comments on commit 6c8405f

Please sign in to comment.