Skip to content

Commit

Permalink
feat: add Leather and OneKey wallets (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
chybisov authored Dec 30, 2024
1 parent 736b6d5 commit ac93f66
Show file tree
Hide file tree
Showing 8 changed files with 558 additions and 107 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@
"@commitlint/config-conventional": "^19.6.0",
"@types/events": "^3.0.3",
"@types/node": "^22.10.2",
"@types/react": "^18.3.13",
"@types/react-dom": "^18.3.1",
"@types/react": "^19.0.2",
"@types/react-dom": "^19.0.2",
"cpy-cli": "^5.0.0",
"fs-extra": "^11.2.0",
"husky": "^9.1.7",
"lerna": "8.1.9",
"lint-staged": "^15.2.11",
"lint-staged": "^15.3.0",
"standard-version": "^9.5.0",
"typescript": "^5.7.2"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@
],
"dependencies": {
"@bigmi/core": "workspace:^",
"@wagmi/core": "^2.16.0",
"viem": "^2.21.55"
"@wagmi/core": "^2.16.3",
"viem": "^2.21.59"
},
"devDependencies": {
"cpy-cli": "^5.0.0",
Expand Down
190 changes: 190 additions & 0 deletions packages/client/src/connectors/leather.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
import type {
BtcAccount,
SignPsbtParameters,
UTXOWalletProvider,
} from '@bigmi/core'
import {
type Address,
MethodNotSupportedRpcError,
UserRejectedRequestError,
} from 'viem'
import { ProviderNotFoundError, createConnector } from 'wagmi'
import type { UTXOConnectorParameters } from './types.js'

export type LeatherBitcoinEventMap = {
accountChange(accounts: BtcAccount[]): void
}

export type LeatherBitcoinEvents = {
addListener<TEvent extends keyof LeatherBitcoinEventMap>(
event: TEvent,
listener: LeatherBitcoinEventMap[TEvent]
): void
removeListener?<TEvent extends keyof LeatherBitcoinEventMap>(
event: TEvent,
listener: LeatherBitcoinEventMap[TEvent]
): void
}

type LeatherConnectorProperties = {
getAccounts(): Promise<readonly (BtcAccount | Address)[]>
getInternalProvider(): Promise<LeatherBitcoinProvider>
} & UTXOWalletProvider

type Error = { code: number; message: string }

interface GetAccountsResponse {
result?: { addresses: BtcAccount[] }
error?: Error
}

type LeatherBitcoinProvider = {
request(
method: 'signPsbt',
options: {
psbt: string
allowedSignHash: number
signInputs: Record<string, number[]>
broadcast: boolean
}
): Promise<string>
request(method: 'getAddresses'): Promise<GetAccountsResponse>
} & LeatherBitcoinEvents

leather.type = 'UTXO' as const
export function leather(parameters: UTXOConnectorParameters = {}) {
const { chainId, shimDisconnect = true } = parameters
return createConnector<
UTXOWalletProvider | undefined,
LeatherConnectorProperties
>((config) => ({
id: 'LeatherProvider',
name: 'Leather',
type: leather.type,
icon: 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIxMjgiIGhlaWdodD0iMTI4IiBmaWxsPSJub25lIj4KICAgIDxwYXRoIGZpbGw9IiMxMjEwMEYiIGQ9Ik0wIDBoMTI4djEyOEgweiIvPgogICAgPHBhdGggZmlsbD0iI0Y1RjFFRCIgZD0iTTc0LjkxNyA1Mi43MTFjNy41Ni0xLjE3IDE4LjQ5Mi05LjEzIDE4LjQ5Mi0xNS4zMzUgMC0xLjg3My0xLjUxMi0zLjE2LTMuNzIyLTMuMTYtNC4xODcgMC0xMS4yOCA2LjMyLTE0Ljc3IDE4LjQ5NU0zOS45MTEgODMuNWMtOS44ODUgMC0xMC43IDkuODMzLS44MTQgOS44MzMgNC40MiAwIDkuNzctMS43NTYgMTIuNTYtNC45MTYtNC4wNy0zLjUxMi03LjQ0My00LjkxNy0xMS43NDYtNC45MTdtNjIuOTE4LTQuMjE0Yy41ODEgMTYuNTA2LTcuNzkyIDI1Ljc1NC0yMS45OCAyNS43NTQtOC4zNzQgMC0xMi41Ni0zLjE2MS0yMS41MTYtOS4wMTQtNC42NTIgNS4xNTEtMTMuNDkgOS4wMTQtMjAuODE4IDkuMDE0LTI1LjIzNiAwLTI0LjE5LTMyLjE5MyAxLjUxMi0zMi4xOTMgNS4zNSAwIDkuODg2IDEuNDA1IDE1LjcgNS4wMzRsMy44MzktMTMuNDYyQzQzLjc0OSA2MC4wODYgMzUuODQgNDcuOTEyIDQzLjYzMyAzMC40NjloMTIuNTZjLTYuOTc4IDExLjU5LTIuMjEgMjEuMTg5IDYuNjI5IDIyLjI0MkM2Ny41OSAzNS43MzcgNzcuODI1IDIyLjUxIDkxLjQzMiAyMi41MWM3LjY3NSAwIDEzLjcyMyA1LjAzNCAxMy43MjMgMTQuMTY1IDAgMTQuNjMzLTE5LjA3MyAyNi41NzMtMzMuNDk0IDI3Ljc0NEw2NS43MyA4NS4zNzJjNi43NDUgNy44NDMgMjUuNDY5IDE1LjQ1MiAyNS40NjktNi4wODd6Ii8+Cjwvc3ZnPg==',
async setup() {
//
},
async getInternalProvider() {
if (typeof window === 'undefined') {
return undefined
}
if ('LeatherProvider' in window) {
const anyWindow: any = window
const provider = anyWindow.LeatherProvider
return provider
}
},
async getProvider() {
const internalProvider = await this.getInternalProvider()
if (!internalProvider) {
return
}
const provider = {
request: this.request.bind(internalProvider),
}
return provider
},
async request(
this: LeatherBitcoinProvider | any,
{ method, params }
): Promise<any> {
switch (method) {
case 'signPsbt': {
const { psbt, ...options } = params as SignPsbtParameters
const signedPsbt = await this.request('signPsbt', {
psbt: psbt,
broadcast: options.finalize,
})
if (signedPsbt?.error) {
throw signedPsbt?.error
}
return signedPsbt
}
default:
throw new MethodNotSupportedRpcError(
new Error(MethodNotSupportedRpcError.name),
{
method,
}
)
}
},
async connect() {
const provider = await this.getInternalProvider()
if (!provider) {
throw new ProviderNotFoundError()
}

const accounts = await this.getAccounts()
const chainId = await this.getChainId()

// Remove disconnected shim if it exists
if (shimDisconnect) {
await Promise.all([
config.storage?.setItem(`${this.id}.connected`, true),
config.storage?.removeItem(`${this.id}.disconnected`),
])
}

return { accounts, chainId }
},
async disconnect() {
const provider = await this.getInternalProvider()
if (!provider) {
throw new ProviderNotFoundError()
}

// Add shim signalling connector is disconnected
if (shimDisconnect) {
await Promise.all([
config.storage?.setItem(`${this.id}.disconnected`, true),
config.storage?.removeItem(`${this.id}.connected`),
])
}
},
async getAccounts() {
const provider = await this.getInternalProvider()
if (!provider) {
throw new ProviderNotFoundError()
}
const accounts = await provider.request('getAddresses')
if (!accounts.result) {
throw new UserRejectedRequestError({
name: UserRejectedRequestError.name,
message: accounts.error?.message!,
})
}
return accounts.result.addresses.map(
(account) => account.address as Address
)
},
async getChainId() {
return chainId!
},
async isAuthorized() {
try {
const isConnected =
shimDisconnect &&
// If shim exists in storage, connector is disconnected
Boolean(await config.storage?.getItem(`${this.id}.connected`))
return isConnected
} catch {
return false
}
},
async onAccountsChanged() {
const { accounts } = await this.connect()
config.emitter.emit('change', {
accounts,
})
},
onChainChanged(chain) {
const chainId = Number(chain)
config.emitter.emit('change', { chainId })
},
async onDisconnect(_error) {
config.emitter.emit('disconnect')
},
}))
}
Loading

0 comments on commit ac93f66

Please sign in to comment.