Skip to content

Commit

Permalink
Merge pull request #320 from omnisat/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
hathbanger authored Feb 10, 2025
2 parents 7210059 + 08eda78 commit c888f1a
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 76 deletions.
2 changes: 1 addition & 1 deletion packages/lasereyes-core/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@omnisat/lasereyes-core",
"private": false,
"version": "0.0.60",
"version": "0.0.61-rc.3",
"type": "module",
"main": "./dist/index.umd.cjs",
"module": "./dist/index.js",
Expand Down
47 changes: 33 additions & 14 deletions packages/lasereyes-core/src/client/providers/leather.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@ import {
LeatherRequestSignResponse,
SignPsbtRequestParams,
} from '../../types'
import { getBTCBalance } from '../../lib/helpers'
import { getBTCBalance, isMainnetNetwork } from '../../lib/helpers'
import { LEATHER, P2TR, P2WPKH } from '../../constants/wallets'
import { listenKeys, MapStore } from 'nanostores'
import { persistentMap } from '@nanostores/persistent'
import { LaserEyesStoreType } from '../types'
import { SIGNET, TESTNET, TESTNET4 } from '../../constants'
import { RpcErrorCode } from 'sats-connect'
import { keysToPersist, PersistedKey } from '../utils'
import {
handleStateChangePersistence,
keysToPersist,
PersistedKey,
} from '../utils'

const LEATHER_WALLET_PERSISTENCE_KEY = 'LEATHER_CONNECTED_WALLET_STATE'

Expand Down Expand Up @@ -46,25 +50,28 @@ export default class LeatherProvider extends WalletProvider {
restorePersistedValues() {
const vals = this.$valueStore.get()
for (const key of keysToPersist) {
if (key === 'balance') {
this.$store.setKey(key, BigInt(vals[key]))
}
this.$store.setKey(key, vals[key])
}
this.$store.setKey(
'accounts',
[vals.address, vals.paymentAddress].filter(Boolean)
)
}

watchStateChange(
newState: LaserEyesStoreType,
_: LaserEyesStoreType | undefined,
changedKey: keyof LaserEyesStoreType | undefined
) {
if (changedKey && newState.provider === LEATHER) {
if (changedKey === 'balance') {
this.$valueStore.setKey('balance', newState.balance?.toString() ?? '')
} else if ((keysToPersist as readonly string[]).includes(changedKey)) {
this.$valueStore.setKey(
changedKey as PersistedKey,
newState[changedKey]?.toString() ?? ''
)
}
}
handleStateChangePersistence(
LEATHER,
newState,
changedKey,
this.$valueStore
)
}

initialize() {
Expand Down Expand Up @@ -107,6 +114,20 @@ export default class LeatherProvider extends WalletProvider {
}

async connect(_: ProviderType): Promise<void> {
const { address, paymentAddress } = this.$valueStore!.get()

if (address) {
if (address.startsWith('tb1') && isMainnetNetwork(this.network)) {
this.disconnect()
} else {
this.restorePersistedValues()
getBTCBalance(paymentAddress, this.network).then((totalBalance) => {
this.$store.setKey('balance', totalBalance)
})
return
}
}

if (!this.library) throw new Error("Leather isn't installed")
const getAddressesResponse: LeatherRPCResponse =
await this.library.request('getAddresses')
Expand Down Expand Up @@ -144,8 +165,6 @@ export default class LeatherProvider extends WalletProvider {
this.$store.setKey('paymentAddress', segwitAddress.address)
this.$store.setKey('publicKey', taprootAddress.publicKey)
this.$store.setKey('paymentPublicKey', segwitAddress.publicKey)
this.$store.setKey('provider', LEATHER)
this.$store.setKey('connected', true)
}

async getNetwork() {
Expand Down
37 changes: 24 additions & 13 deletions packages/lasereyes-core/src/client/providers/okx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
} from '../..'
import { listenKeys, MapStore } from 'nanostores'
import { persistentMap } from '@nanostores/persistent'
import { keysToPersist, PersistedKey } from '../utils'
import { handleStateChangePersistence, keysToPersist, PersistedKey } from '../utils'
import { getBTCBalance, isMainnetNetwork } from '../../lib/helpers'

const OKX_WALLET_PERSISTENCE_KEY = 'OKX_CONNECTED_WALLET_STATE'

Expand Down Expand Up @@ -56,25 +57,23 @@ export default class OkxProvider extends WalletProvider {
restorePersistedValues() {
const vals = this.$valueStore.get()
for (const key of keysToPersist) {
if (key === 'balance') {
this.$store.setKey(key, BigInt(vals[key]))
}
this.$store.setKey(key, vals[key])
}
this.$store.setKey(
'accounts',
[vals.address, vals.paymentAddress].filter(Boolean)
)
}

watchStateChange(
newState: LaserEyesStoreType,
_: LaserEyesStoreType | undefined,
changedKey: keyof LaserEyesStoreType | undefined
) {
if (changedKey && newState.provider === OKX) {
if (changedKey === 'balance') {
this.$valueStore.setKey('balance', newState.balance?.toString() ?? '')
} else if ((keysToPersist as readonly string[]).includes(changedKey)) {
this.$valueStore.setKey(
changedKey as PersistedKey,
newState[changedKey]?.toString() ?? ''
)
}
}
handleStateChangePersistence(OKX, newState, changedKey, this.$valueStore)
}

initialize(): void {
Expand Down Expand Up @@ -118,6 +117,20 @@ export default class OkxProvider extends WalletProvider {
}

async connect(_: ProviderType): Promise<void> {
const { address, paymentAddress } = this.$valueStore!.get()

if (address) {
if (address.startsWith('tb1') && isMainnetNetwork(this.network)) {
this.disconnect()
} else {
this.restorePersistedValues()
getBTCBalance(paymentAddress, this.network).then((totalBalance) => {
this.$store.setKey('balance', totalBalance)
})
return
}
}

try {
const okxAccounts = await this.library.connect()
if (!okxAccounts) throw new Error('No accounts found')
Expand All @@ -127,8 +140,6 @@ export default class OkxProvider extends WalletProvider {
this.$store.setKey('publicKey', okxAccounts.publicKey)
this.$store.setKey('paymentPublicKey', okxAccounts.publicKey)
this.$store.setKey('accounts', [okxAccounts])
this.$store.setKey('provider', OKX)
this.$store.setKey('connected', true)
} catch (e) {
throw e
}
Expand Down
25 changes: 14 additions & 11 deletions packages/lasereyes-core/src/client/providers/orange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
import { MapStore, listenKeys } from 'nanostores'
import { persistentMap } from '@nanostores/persistent'

import { keysToPersist, PersistedKey } from '../utils'
import { handleStateChangePersistence, keysToPersist, PersistedKey } from '../utils'

const { signMessage: signMessageOrange, sendBtcTransaction: sendBtcTxOrange } =
orange
Expand Down Expand Up @@ -64,25 +64,28 @@ export default class OrangeProvider extends WalletProvider {
restorePersistedValues() {
const vals = this.$valueStore.get()
for (const key of keysToPersist) {
if (key === 'balance') {
this.$store.setKey(key, BigInt(vals[key]))
}
this.$store.setKey(key, vals[key])
}
this.$store.setKey(
'accounts',
[vals.address, vals.paymentAddress].filter(Boolean)
)
}

watchStateChange(
newState: LaserEyesStoreType,
_: LaserEyesStoreType | undefined,
changedKey: keyof LaserEyesStoreType | undefined
) {
if (changedKey && newState.provider === ORANGE) {
if (changedKey === 'balance') {
this.$valueStore.setKey('balance', newState.balance?.toString() ?? '')
} else if ((keysToPersist as readonly string[]).includes(changedKey)) {
this.$valueStore.setKey(
changedKey as PersistedKey,
newState[changedKey]?.toString() ?? ''
)
}
}
handleStateChangePersistence(
ORANGE,
newState,
changedKey,
this.$valueStore
)
}

initialize(): void {
Expand Down
55 changes: 39 additions & 16 deletions packages/lasereyes-core/src/client/providers/oyl.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import * as bitcoin from 'bitcoinjs-lib'
import { UNSUPPORTED_PROVIDER_METHOD_ERROR, WalletProvider } from '.'
import { ProviderType, NetworkType } from '../../types'
import { createSendBtcPsbt, isTestnetNetwork } from '../../lib/helpers'
import {
createSendBtcPsbt,
getBTCBalance,
isMainnetNetwork,
isTestnetNetwork,
} from '../../lib/helpers'
import { OYL } from '../../constants/wallets'
import { listenKeys, MapStore } from 'nanostores'
import { persistentMap } from '@nanostores/persistent'
import { LaserEyesStoreType } from '../types'
import { keysToPersist, PersistedKey } from '../utils'
import {
handleStateChangePersistence,
keysToPersist,
PersistedKey,
} from '../utils'

const OYL_WALLET_PERSISTENCE_KEY = 'OYL_CONNECTED_WALLET_STATE'

Expand Down Expand Up @@ -36,25 +45,23 @@ export default class OylProvider extends WalletProvider {
restorePersistedValues() {
const vals = this.$valueStore.get()
for (const key of keysToPersist) {
if (key === 'balance') {
this.$store.setKey(key, BigInt(vals[key]))
}
this.$store.setKey(key, vals[key])
}
this.$store.setKey(
'accounts',
[vals.address, vals.paymentAddress].filter(Boolean)
)
}

watchStateChange(
newState: LaserEyesStoreType,
_: LaserEyesStoreType | undefined,
changedKey: keyof LaserEyesStoreType | undefined
) {
if (changedKey && newState.provider === OYL) {
if (changedKey === 'balance') {
this.$valueStore.setKey('balance', newState.balance?.toString() ?? '')
} else if ((keysToPersist as readonly string[]).includes(changedKey)) {
this.$valueStore.setKey(
changedKey as PersistedKey,
newState[changedKey]?.toString() ?? ''
)
}
}
handleStateChangePersistence(OYL, newState, changedKey, this.$valueStore)
}

initialize() {
Expand Down Expand Up @@ -97,6 +104,20 @@ export default class OylProvider extends WalletProvider {
}

async connect(_: ProviderType): Promise<void> {
const { address, paymentAddress } = this.$valueStore!.get()

if (address) {
if (address.startsWith('tb1') && isMainnetNetwork(this.network)) {
this.disconnect()
} else {
this.restorePersistedValues()
getBTCBalance(paymentAddress, this.network).then((totalBalance) => {
this.$store.setKey('balance', totalBalance)
})
return
}
}

if (!this.library) throw new Error("Oyl isn't installed")
if (isTestnetNetwork(this.network)) {
throw new Error(`${this.network} is not supported by Oyl`)
Expand All @@ -108,8 +129,6 @@ export default class OylProvider extends WalletProvider {
this.$store.setKey('paymentAddress', nativeSegwit.address)
this.$store.setKey('publicKey', taproot.publicKey)
this.$store.setKey('paymentPublicKey', nativeSegwit.publicKey)
this.$store.setKey('provider', OYL)
this.$store.setKey('connected', true)
}

async getNetwork() {
Expand Down Expand Up @@ -171,7 +190,11 @@ export default class OylProvider extends WalletProvider {
}

async getPublicKey() {
return await this.library?.getPublicKey()
const { nativeSegwit, taproot } = await this.library.getAddresses()
if (!nativeSegwit || !taproot) throw new Error('No accounts found')
this.$store.setKey('publicKey', taproot.publicKey)
this.$store.setKey('paymentPublicKey', nativeSegwit.publicKey)
return taproot.publicKey
}
async getBalance() {
const { total } = await this.library.getBalance()
Expand All @@ -186,7 +209,7 @@ export default class OylProvider extends WalletProvider {
}

async requestAccounts(): Promise<string[]> {
return await this.library.requestAccounts()
return [this.$store.get().address, this.$store.get().paymentAddress]
}

async switchNetwork(): Promise<void> {
Expand Down
Loading

0 comments on commit c888f1a

Please sign in to comment.