Skip to content

Commit

Permalink
types and name improves
Browse files Browse the repository at this point in the history
  • Loading branch information
0xBeycan committed May 24, 2024
1 parent 3b8d04a commit 3184f91
Show file tree
Hide file tree
Showing 35 changed files with 152 additions and 151 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ test*.ts
test*.js
test*.cjs
.env
index.html

# Editor directories and files
.vscode/*
Expand Down
4 changes: 2 additions & 2 deletions packages/networks/bitcoin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@multiplechain/bitcoin",
"version": "0.4.1",
"version": "0.4.2",
"type": "module",
"main": "dist/index.cjs",
"module": "dist/index.es.js",
Expand Down Expand Up @@ -72,7 +72,7 @@
"url": "https://github.com/MultipleChain/js/issues"
},
"dependencies": {
"@multiplechain/types": "^0.1.63",
"@multiplechain/types": "^0.1.64",
"@multiplechain/utils": "^0.1.21",
"axios": "^1.6.8",
"bitcore-lib": "^10.0.28",
Expand Down
8 changes: 4 additions & 4 deletions packages/networks/bitcoin/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions packages/networks/bitcoin/src/browser/Wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import { Provider } from '../services/Provider.ts'
import type { TransactionSigner } from '../services/TransactionSigner.ts'

export interface BitcoinWalletAdapter {
export interface WalletProvider {
getAddress: () => Promise<string>
signMessage: (message: string) => Promise<string>
sendBitcoin: (to: string, amount: number) => Promise<string>
Expand All @@ -33,12 +33,12 @@ const rejectMap = (error: any, reject: (a: any) => any): any => {
return reject(error)
}

type WalletAdapter = WalletAdapterInterface<Provider, BitcoinWalletAdapter>
type WalletAdapter = WalletAdapterInterface<Provider, WalletProvider>

export class Wallet implements WalletInterface<Provider, BitcoinWalletAdapter, TransactionSigner> {
export class Wallet implements WalletInterface<Provider, WalletProvider, TransactionSigner> {
adapter: WalletAdapter

walletProvider: BitcoinWalletAdapter
walletProvider: WalletProvider

networkProvider: Provider

Expand Down
23 changes: 12 additions & 11 deletions packages/networks/bitcoin/src/browser/adapters/Leather.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import icons from './icons.ts'
import type { WalletProvider } from '../Wallet.ts'
import { WalletPlatformEnum } from '@multiplechain/types'
import type { WalletAdapterInterface } from '@multiplechain/types'
import type { BitcoinWalletAdapter } from '../Wallet.ts'
import type { Provider } from '../../services/Provider.ts'
import type { WalletAdapterInterface } from '@multiplechain/types'

declare global {
interface Window {
Expand All @@ -16,23 +16,24 @@ declare global {
}

let connected = false
let walletProvider: WalletProvider | undefined

const Leather: WalletAdapterInterface<Provider, BitcoinWalletAdapter> = {
const Leather: WalletAdapterInterface<Provider, WalletProvider> = {
id: 'leather',
name: 'Leather',
icon: icons.Leather,
provider: window.LeatherProvider,
provider: walletProvider,
platforms: [WalletPlatformEnum.BROWSER],
downloadLink: 'https://leather.io/install-extension',
isDetected: () => Boolean(window.LeatherProvider),
isConnected: async () => connected,
connect: async (provider?: Provider): Promise<BitcoinWalletAdapter> => {
connect: async (provider?: Provider): Promise<WalletProvider> => {
return await new Promise((resolve, reject) => {
const leather = window.LeatherProvider

const network = provider !== undefined && provider?.isTestnet() ? 'testnet' : 'mainnet'

const walletAdapter: BitcoinWalletAdapter = {
const _walletProvider: WalletProvider = {
on: (event, callback) => {
if (window.btc?.listen !== undefined) {
window.btc.listen(event, callback)
Expand Down Expand Up @@ -71,7 +72,7 @@ const Leather: WalletAdapterInterface<Provider, BitcoinWalletAdapter> = {
getAddress: async () => ''
}

const connect = async (): Promise<BitcoinWalletAdapter> => {
const connect = async (): Promise<WalletProvider> => {
const addresses = (
await leather.request('getAddresses', {
network
Expand All @@ -83,18 +84,18 @@ const Leather: WalletAdapterInterface<Provider, BitcoinWalletAdapter> = {
// for ordinals & BRC-20 integrations
// const ordinals = addresses.find(address => address.type == 'p2tr');

walletAdapter.getAddress = async () => {
_walletProvider.getAddress = async () => {
return bitcoin.address
}

return walletAdapter
return _walletProvider
}

try {
connect()
.then((walletAdapter) => {
.then(() => {
connected = true
resolve(walletAdapter)
resolve((walletProvider = _walletProvider))
})
.catch(reject)
} catch (error) {
Expand Down
14 changes: 8 additions & 6 deletions packages/networks/bitcoin/src/browser/adapters/UniSat.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import icons from './icons.ts'
import type { WalletProvider } from '../Wallet.ts'
import { WalletPlatformEnum } from '@multiplechain/types'
import type { BitcoinWalletAdapter } from '../Wallet.ts'
import type { Provider } from '../../services/Provider.ts'
import type { WalletAdapterInterface } from '@multiplechain/types'

Expand All @@ -18,20 +18,22 @@ declare global {
}
}

const UniSat: WalletAdapterInterface<Provider, BitcoinWalletAdapter> = {
let walletProvider: WalletProvider | undefined

const UniSat: WalletAdapterInterface<Provider, WalletProvider> = {
id: 'unisat',
name: 'UniSat',
icon: icons.UniSat,
provider: window.unisat,
provider: walletProvider,
platforms: [WalletPlatformEnum.BROWSER],
downloadLink: 'https://unisat.io/download',
isDetected: () => Boolean(window.unisat?.requestAccounts),
isConnected: async () => window?.unisat?._isConnected ?? false,
connect: async (provider?: Provider): Promise<BitcoinWalletAdapter> => {
connect: async (provider?: Provider): Promise<WalletProvider> => {
return await new Promise((resolve, reject) => {
const network = provider !== undefined && provider?.isTestnet() ? 'testnet' : 'livenet'

const walletAdapter: BitcoinWalletAdapter = {
const _walletProvider: WalletProvider = {
on: window.unisat.on,
signMessage: window.unisat.signMessage,
sendBitcoin: window.unisat.sendBitcoin,
Expand All @@ -47,7 +49,7 @@ const UniSat: WalletAdapterInterface<Provider, BitcoinWalletAdapter> = {
window.unisat
.switchNetwork(network)
.then(() => {
resolve(walletAdapter)
resolve((walletProvider = _walletProvider))
})
.catch(reject)
})
Expand Down
27 changes: 14 additions & 13 deletions packages/networks/bitcoin/src/browser/adapters/Xverse.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import icons from './icons.ts'
import type { BitcoinWalletAdapter } from '../Wallet.ts'
import type { WalletProvider } from '../Wallet.ts'
import type { Provider } from '../../services/Provider.ts'
import {
ErrorTypeEnum,
WalletPlatformEnum,
Expand All @@ -12,30 +13,30 @@ import {
signMessage,
AddressPurpose
} from 'sats-connect'
import type { Provider } from '../../services/Provider.ts'

let connected = false
let walletProvider: WalletProvider | undefined

const Xverse: WalletAdapterInterface<Provider, BitcoinWalletAdapter> = {
const Xverse: WalletAdapterInterface<Provider, WalletProvider> = {
id: 'xverse',
name: 'Xverse',
icon: icons.Xverse,
provider: window.XverseProviders?.BitcoinProvider,
provider: walletProvider,
platforms: [WalletPlatformEnum.BROWSER, WalletPlatformEnum.MOBILE],
downloadLink: 'https://www.xverse.app/download',
isDetected: () => Boolean(window.XverseProviders?.BitcoinProvider),
isConnected: async () => connected,
connect: async (provider?: Provider): Promise<BitcoinWalletAdapter> => {
connect: async (provider?: Provider): Promise<WalletProvider> => {
return await new Promise((resolve, reject) => {
const type =
provider !== undefined && provider?.isTestnet()
? BitcoinNetworkType.Testnet
: BitcoinNetworkType.Mainnet

const walletAdapter: BitcoinWalletAdapter = {
const _walletProvider: WalletProvider = {
on: (_event: string, _callback: (data: any) => void) => {},
signMessage: async (message: string) => {
const address = await walletAdapter.getAddress()
const address = await _walletProvider.getAddress()
return await new Promise((resolve, reject) => {
signMessage({
payload: {
Expand All @@ -55,7 +56,7 @@ const Xverse: WalletAdapterInterface<Provider, BitcoinWalletAdapter> = {
})
},
sendBitcoin: async (to: string, amount: number) => {
const senderAddress = await walletAdapter.getAddress()
const senderAddress = await _walletProvider.getAddress()
return await new Promise((resolve, reject) => {
sendBtcTransaction({
payload: {
Expand All @@ -82,7 +83,7 @@ const Xverse: WalletAdapterInterface<Provider, BitcoinWalletAdapter> = {
getAddress: async () => ''
}

const connect = async (): Promise<BitcoinWalletAdapter> => {
const connect = async (): Promise<WalletProvider> => {
return await new Promise((resolve, reject) => {
try {
getAddress({
Expand All @@ -107,11 +108,11 @@ const Xverse: WalletAdapterInterface<Provider, BitcoinWalletAdapter> = {
return
}

walletAdapter.getAddress = async () => {
_walletProvider.getAddress = async () => {
return bitcoin.address
}

resolve(walletAdapter)
resolve(_walletProvider)
},
onCancel: () => {
reject(ErrorTypeEnum.WALLET_REQUEST_REJECTED)
Expand All @@ -125,9 +126,9 @@ const Xverse: WalletAdapterInterface<Provider, BitcoinWalletAdapter> = {

try {
connect()
.then((walletAdapter) => {
.then(() => {
connected = true
resolve(walletAdapter)
resolve((walletProvider = _walletProvider))
})
.catch(reject)
} catch (error) {
Expand Down
8 changes: 4 additions & 4 deletions packages/networks/bitcoin/src/browser/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import type { Provider } from 'sats-connect'
import { Wallet, type BitcoinWalletAdapter } from './Wallet.ts'
import { Wallet, type WalletProvider } from './Wallet.ts'
import * as adapterList from './adapters/index.ts'
import type {
WalletAdapterListType,
WalletAdapterInterface,
RegisterWalletAdapterType
} from '@multiplechain/types'

const adapters: WalletAdapterListType<Provider, BitcoinWalletAdapter> = {}
const adapters: WalletAdapterListType<Provider, WalletProvider> = {}

const registerAdapter: RegisterWalletAdapterType<Provider, BitcoinWalletAdapter> = (
adapter: WalletAdapterInterface<Provider, BitcoinWalletAdapter>
const registerAdapter: RegisterWalletAdapterType<Provider, WalletProvider> = (
adapter: WalletAdapterInterface<Provider, WalletProvider>
): void => {
if (Object.values(adapters).find((a) => a.id === adapter.id) !== undefined) {
throw new Error(`Adapter with id ${adapter.id} already exists`)
Expand Down
2 changes: 1 addition & 1 deletion packages/networks/boilerplate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"url": "https://github.com/MultipleChain/js/issues"
},
"dependencies": {
"@multiplechain/types": "^0.1.63",
"@multiplechain/types": "^0.1.64",
"@multiplechain/utils": "^0.1.21"
}
}
8 changes: 4 additions & 4 deletions packages/networks/boilerplate/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/networks/evm-chains/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@multiplechain/evm-chains",
"version": "0.4.0",
"version": "0.4.1",
"type": "module",
"main": "dist/index.cjs",
"module": "dist/index.es.js",
Expand Down Expand Up @@ -75,7 +75,7 @@
"url": "https://github.com/MultipleChain/js/issues"
},
"dependencies": {
"@multiplechain/types": "^0.1.63",
"@multiplechain/types": "^0.1.64",
"@multiplechain/utils": "^0.1.21",
"@wagmi/chains": "^1.8.0",
"@walletconnect/ethereum-provider": "^2.12.2",
Expand Down
8 changes: 4 additions & 4 deletions packages/networks/evm-chains/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const MetaMask: WalletAdapterInterface<Provider, EIP1193Provider> = {
id: 'metamask',
name: 'MetaMask',
icon: icons.metaMask,
provider: window.ethereum,
downloadLink: 'https://metamask.io/download/',
provider: window.ethereum as unknown as WindowEthereum,
platforms: [WalletPlatformEnum.BROWSER, WalletPlatformEnum.MOBILE],
isDetected: () => Boolean((window?.ethereum as unknown as WindowEthereum).isMetaMask),
createDeepLink: (url: string): string => `https://metamask.app.link/dapp/${url}`,
Expand Down
4 changes: 2 additions & 2 deletions packages/networks/solana/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@multiplechain/solana",
"version": "0.4.0",
"version": "0.4.1",
"type": "module",
"main": "dist/index.cjs",
"module": "dist/index.es.js",
Expand Down Expand Up @@ -74,7 +74,7 @@
"dependencies": {
"@metaplex-foundation/js": "^0.20.1",
"@multiplechain/solana-walletconnect": "^0.1.1",
"@multiplechain/types": "^0.1.63",
"@multiplechain/types": "^0.1.64",
"@multiplechain/utils": "^0.1.21",
"@solana/spl-token": "^0.4.6",
"@solana/spl-token-metadata": "^0.1.4",
Expand Down
Loading

0 comments on commit 3184f91

Please sign in to comment.