-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bitcoin): Add phantom wallet adapter (#1267)
* feat(bitcoin): Add phantom wallet adapter * changeset * type def * Update packages/bitcoin/src/adapter/wallets/phantom.ts Co-authored-by: thinkasany <[email protected]> * doc * fix eslint * x * build error * get balance * doc * impl getInscriptions * ignore test coverage * update demo * doc * add NotImplementError * update lock * fix dep version * chore: update lock --------- Co-authored-by: thinkasany <[email protected]>
- Loading branch information
1 parent
9a6dc16
commit 80f0dc2
Showing
15 changed files
with
276 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@ant-design/web3-bitcoin': minor | ||
--- | ||
|
||
feat(bitcoin): Add phantom wallet adapter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './unisat'; | ||
export * from './xverse'; | ||
export * from './okx'; | ||
export * from './phantom'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* v8 ignore start */ | ||
import type { Account } from '@ant-design/web3-common'; | ||
import { fromHex, fromUtf8, toBase64, toHex } from 'uint8array-tools'; | ||
|
||
import { NoAddressError, NoProviderError, NotImplementedError } from '../../error'; | ||
import { getBalanceByMempool, getInscriptionsByAddress } from '../../helpers'; | ||
import type { SignPsbtParams, TransferParams } from '../../types'; | ||
import type { BitcoinWallet } from '../useBitcoinWallet'; | ||
|
||
type AccountType = { | ||
address: string; | ||
addressType: string; | ||
publicKey: string; | ||
purpose: string; | ||
}; | ||
|
||
/** | ||
* @link https://docs.phantom.app/bitcoin/provider-api-reference#options-parameters | ||
*/ | ||
type PhantomSignPsbtOptions = { | ||
sigHash?: number; | ||
address: string; | ||
signingIndexes: number[]; | ||
}[]; | ||
|
||
export class PhantomBitcoinWallet implements BitcoinWallet { | ||
name: string; | ||
provider?: any; | ||
account?: Account; | ||
payment?: string; | ||
|
||
constructor(name: string) { | ||
this.name = name; | ||
this.provider = window.phantom?.bitcoin; | ||
this.account = undefined; | ||
} | ||
|
||
connect = async () => { | ||
if (!this.provider) { | ||
throw new NoProviderError(); | ||
} | ||
|
||
try { | ||
const accounts: AccountType[] = await this.provider.requestAccounts(); | ||
const ordinals = accounts.find((acc) => acc.purpose === 'ordinals'); | ||
const payment = accounts.find((acc) => acc.purpose === 'payment'); | ||
|
||
this.account = ordinals ? { address: ordinals.address } : undefined; | ||
this.payment = payment?.address; | ||
} catch (error) { | ||
throw error; | ||
} | ||
}; | ||
|
||
getBalance = async () => { | ||
if (!this.payment) { | ||
throw new NoAddressError(); | ||
} | ||
|
||
const balance = await getBalanceByMempool(this.payment); | ||
return balance; | ||
}; | ||
|
||
signMessage = async (message: string) => { | ||
if (!this.provider) { | ||
throw new NoProviderError(); | ||
} | ||
|
||
if (!this.account?.address) { | ||
throw new NoAddressError(); | ||
} | ||
|
||
const { signature } = await this.provider.signMessage(this.account.address, fromUtf8(message)); | ||
|
||
return toBase64(signature); | ||
}; | ||
|
||
signPsbt = async ({ psbt, options = {} }: SignPsbtParams) => { | ||
if (!this.provider) { | ||
throw new NoProviderError(); | ||
} | ||
|
||
if (!this.account?.address) { | ||
throw new NoAddressError(); | ||
} | ||
|
||
const serializedPsbt = fromHex(psbt); | ||
|
||
const { | ||
// `broadcast` not supported | ||
// broadcast, | ||
signInputs, | ||
signHash, | ||
} = options; | ||
const inputsToSign: PhantomSignPsbtOptions = []; | ||
|
||
if (signInputs) { | ||
for (const address in signInputs) { | ||
inputsToSign.push({ | ||
sigHash: signHash, | ||
address, | ||
signingIndexes: signInputs[address], | ||
}); | ||
} | ||
} | ||
|
||
const signedPsbt = await this.provider.signPSBT(serializedPsbt, { inputsToSign }); | ||
const hexPsbt = toHex(signedPsbt); | ||
|
||
return { | ||
psbt: hexPsbt, | ||
}; | ||
}; | ||
|
||
sendTransfer = async (params: TransferParams) => { | ||
throw new NotImplementedError(); | ||
}; | ||
|
||
getInscriptions = async (offset = 0, limit = 20) => { | ||
if (!this.account?.address) { | ||
throw new NoAddressError(); | ||
} | ||
|
||
const inscriptions = await getInscriptionsByAddress({ | ||
address: this.account.address, | ||
offset, | ||
limit, | ||
}); | ||
return inscriptions; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './provider'; | ||
export * from './wallets'; | ||
export * from './types'; | ||
export * from './error'; | ||
export { useBitcoinWallet } from './adapter/useBitcoinWallet'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,20 @@ | ||
/* v8 ignore start */ | ||
import { metadata_OkxWallet, metadata_Unisat, metadata_Xverse } from '@ant-design/web3-assets'; | ||
import { | ||
metadata_OkxWallet, | ||
metadata_Phantom, | ||
metadata_Unisat, | ||
metadata_Xverse, | ||
} from '@ant-design/web3-assets'; | ||
|
||
import { OkxBitcoinWallet, UnisatBitcoinWallet, XverseBitcoinWallet } from '../adapter'; | ||
import { | ||
OkxBitcoinWallet, | ||
PhantomBitcoinWallet, | ||
UnisatBitcoinWallet, | ||
XverseBitcoinWallet, | ||
} from '../adapter'; | ||
import { WalletFactory } from './factory'; | ||
|
||
export const UnisatWallet = () => WalletFactory(UnisatBitcoinWallet, metadata_Unisat); | ||
export const XverseWallet = () => WalletFactory(XverseBitcoinWallet, metadata_Xverse); | ||
export const OkxWallet = () => WalletFactory(OkxBitcoinWallet, metadata_OkxWallet); | ||
export const PhantomWallet = () => WalletFactory(PhantomBitcoinWallet, metadata_Phantom); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.