-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: address monitor and btc ntx notifications
- Loading branch information
Showing
17 changed files
with
530 additions
and
174 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
78 changes: 78 additions & 0 deletions
78
src/app/features/address-monitor/use-monitorable-addresses.ts
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,78 @@ | ||
import { useMemo } from 'react'; | ||
|
||
import type { HDKey } from '@scure/bip32'; | ||
import type { P2Ret } from '@scure/btc-signer/payment'; | ||
|
||
import { | ||
type SupportedPaymentType, | ||
deriveAddressIndexZeroFromAccount, | ||
getNativeSegwitPaymentFromAddressIndex, | ||
getTaprootPaymentFromAddressIndex, | ||
} from '@leather.io/bitcoin'; | ||
import type { BitcoinNetworkModes } from '@leather.io/models'; | ||
import { createNullArrayOfLength, isDefined } from '@leather.io/utils'; | ||
|
||
import { useCurrentAccountIndex } from '@app/store/accounts/account'; | ||
import { useGenerateNativeSegwitAccount } from '@app/store/accounts/blockchain/bitcoin/native-segwit-account.hooks'; | ||
import { useGenerateTaprootAccount } from '@app/store/accounts/blockchain/bitcoin/taproot-account.hooks'; | ||
import { useStacksAccounts } from '@app/store/accounts/blockchain/stacks/stacks-account.hooks'; | ||
import { useCurrentNetworkId } from '@app/store/networks/networks.selectors'; | ||
import type { MonitoredAddress } from '@background/monitors/address-monitor'; | ||
|
||
const paymentFnMap: Record< | ||
SupportedPaymentType, | ||
(keychain: HDKey, network: BitcoinNetworkModes) => P2Ret | ||
> = { | ||
p2tr: getTaprootPaymentFromAddressIndex, | ||
p2wpkh: getNativeSegwitPaymentFromAddressIndex, | ||
}; | ||
|
||
export function useMonitorableAddresses() { | ||
const currentAccountIndex = useCurrentAccountIndex(); | ||
const currentNetworkId = useCurrentNetworkId(); | ||
const createNativeSegwitAccount = useGenerateNativeSegwitAccount(); | ||
const createTaprootAccount = useGenerateTaprootAccount(); | ||
|
||
const stacksAccounts = useStacksAccounts(); | ||
|
||
return useMemo(() => { | ||
if (!stacksAccounts || !currentNetworkId) return; | ||
|
||
const stacksAddresses = stacksAccounts.map( | ||
account => | ||
({ | ||
accountIndex: account.index, | ||
address: account.address, | ||
chain: 'stacks', | ||
isCurrent: account.index === currentAccountIndex, | ||
}) as MonitoredAddress | ||
); | ||
const btcAddresses = createNullArrayOfLength(stacksAccounts.length).flatMap((_, index) => | ||
[createNativeSegwitAccount(index), createTaprootAccount(index)] | ||
.filter(isDefined) | ||
.map(account => { | ||
const addressIndexKeychain = deriveAddressIndexZeroFromAccount(account.keychain); | ||
if (account.type !== 'p2tr' && account.type !== 'p2wpkh') return undefined; | ||
const payment = paymentFnMap[account.type](addressIndexKeychain, 'mainnet'); | ||
return { | ||
accountIndex: index, | ||
address: payment.address, | ||
chain: 'bitcoin', | ||
isCurrent: index === currentAccountIndex, | ||
} as MonitoredAddress; | ||
}) | ||
.filter(isDefined) | ||
); | ||
// if one address array is empty and the other not, we're in an intermediate state | ||
return (stacksAddresses.length === 0 && btcAddresses.length > 0) || | ||
(btcAddresses.length === 0 && stacksAddresses.length > 0) | ||
? undefined | ||
: [...stacksAddresses, ...btcAddresses]; | ||
}, [ | ||
createNativeSegwitAccount, | ||
createTaprootAccount, | ||
stacksAccounts, | ||
currentNetworkId, | ||
currentAccountIndex, | ||
]); | ||
} |
29 changes: 29 additions & 0 deletions
29
src/app/features/address-monitor/use-sync-address-monitor.ts
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,29 @@ | ||
import { useEffect, useRef } from 'react'; | ||
|
||
import isEqual from 'lodash.isequal'; | ||
|
||
import { logger } from '@shared/logger'; | ||
import { InternalMethods } from '@shared/message-types'; | ||
import { sendMessage } from '@shared/messages'; | ||
|
||
import { useMonitorableAddresses } from '@app/features/address-monitor/use-monitorable-addresses'; | ||
import type { MonitoredAddress } from '@background/monitors/address-monitor'; | ||
|
||
export function useSyncAddressMonitor() { | ||
const addresses = useMonitorableAddresses(); | ||
const prevAddresses = useRef<MonitoredAddress[]>([]); | ||
|
||
useEffect(() => { | ||
if (addresses && !isEqual(addresses, prevAddresses.current)) { | ||
prevAddresses.current = addresses; | ||
|
||
logger.debug('Syncing Monitored Addresses: ', addresses); | ||
sendMessage({ | ||
method: InternalMethods.AddressMonitorUpdated, | ||
payload: { | ||
addresses, | ||
}, | ||
}); | ||
} | ||
}, [addresses]); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
import { useSelector } from 'react-redux'; | ||
|
||
import { RootState } from '@app/store'; | ||
|
||
export const selectStacksChain = (state: RootState) => state.chains.stx; | ||
|
||
export function useStacksChain() { | ||
return useSelector(selectStacksChain); | ||
} |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* eslint-disable no-console */ | ||
import { z } from 'zod'; | ||
|
||
import { BitcoinTransactionMonitor } from './address-monitors/bitcoin-transaction-monitor'; | ||
|
||
export const monitoredAddressSchema = z.object({ | ||
chain: z.enum(['bitcoin', 'stacks']), | ||
accountIndex: z.number(), | ||
isCurrent: z.boolean(), | ||
address: z.string(), | ||
}); | ||
|
||
export type MonitoredAddress = z.infer<typeof monitoredAddressSchema>; | ||
|
||
export interface AddressMonitor { | ||
syncAddresses(addresses: MonitoredAddress[]): void; | ||
} | ||
|
||
export class AddressMonitorContainer { | ||
private _monitors: AddressMonitor[] = []; | ||
|
||
init(addresses: MonitoredAddress[]) { | ||
this._monitors = [new BitcoinTransactionMonitor(addresses)]; | ||
} | ||
sync(addresses: MonitoredAddress[]) { | ||
this._monitors.forEach(monitor => monitor.syncAddresses(addresses)); | ||
} | ||
} | ||
|
||
const monitorContainer = new AddressMonitorContainer(); | ||
|
||
export async function initAddressMonitor() { | ||
const addresses = await readMonitoredAddressStore(); | ||
monitorContainer.init(addresses); | ||
} | ||
|
||
export async function syncAddressMonitor(addresses: MonitoredAddress[]) { | ||
await writeMonitoredAddressStore(addresses); | ||
monitorContainer.sync(addresses); | ||
} | ||
|
||
const ADDRESS_MONITOR_STORE = 'addressMonitorStore'; | ||
|
||
export async function readMonitoredAddressStore() { | ||
const result = await chrome.storage.local.get(ADDRESS_MONITOR_STORE); | ||
const addresses = result[ADDRESS_MONITOR_STORE] || []; | ||
return addresses; | ||
} | ||
|
||
export async function writeMonitoredAddressStore(addresses: MonitoredAddress[]) { | ||
await chrome.storage.local.set({ | ||
[ADDRESS_MONITOR_STORE]: addresses, | ||
}); | ||
} |
Oops, something went wrong.