-
Notifications
You must be signed in to change notification settings - Fork 15
add support for parityUSD extension #176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+1,069
−34
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
487059b
add support for parityUSD extension
mr-zwets eaee3fe
improve extension code quiality
mr-zwets 8d2bd47
remove multiline comments, add to CLAUDE.md
mr-zwets 060bc4c
add placeholder text for loading loan data
mr-zwets 1affd85
Merge branch 'main' into parityusd-extension
mr-zwets 299a767
move store logic to storeUtils
mr-zwets 4279398
remove unused param
mr-zwets File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,62 @@ | ||
| import type { ElectrumClient, ElectrumUtxo } from "./extensions/types"; | ||
|
|
||
| /** | ||
| * Minimal provider interface — duck-typed to avoid deep imports from mainnet-js. | ||
| */ | ||
| interface ElectrumProvider { | ||
| getUtxos(cashaddr: string): Promise<{ | ||
| txid: string; | ||
| vout: number; | ||
| satoshis: bigint; | ||
| height?: number; | ||
| token?: { | ||
| category: string; | ||
| amount: bigint; | ||
| nft?: { | ||
| capability: "none" | "mutable" | "minting"; | ||
| commitment: string; | ||
| }; | ||
| }; | ||
| }[]>; | ||
| getRawTransaction(txHash: string): Promise<string>; | ||
| } | ||
|
|
||
| /** | ||
| * Create an ElectrumClient adapter from a mainnet-js ElectrumNetworkProvider. | ||
| * | ||
| * Bridges the mainnet-js Utxo format to the ElectrumUtxo format expected | ||
| * by the extension system. | ||
| */ | ||
| export function createElectrumAdapter(provider: ElectrumProvider): ElectrumClient { | ||
| return { | ||
| async getUTXOs(address: string): Promise<ElectrumUtxo[]> { | ||
| const utxos = await provider.getUtxos(address); | ||
| return utxos.map((utxo) => { | ||
| const result: ElectrumUtxo = { | ||
| tx_hash: utxo.txid, | ||
| tx_pos: utxo.vout, | ||
| value: utxo.satoshis, | ||
| script: "", | ||
| ...(utxo.height !== undefined && { height: utxo.height }), | ||
| }; | ||
| if (utxo.token) { | ||
| result.token_data = { | ||
| category: utxo.token.category, | ||
| amount: utxo.token.amount?.toString(), | ||
| }; | ||
| if (utxo.token.nft?.capability !== undefined || utxo.token.nft?.commitment !== undefined) { | ||
| result.token_data.nft = { | ||
| capability: utxo.token.nft?.capability ?? "none", | ||
| commitment: utxo.token.nft?.commitment ?? "", | ||
| }; | ||
| } | ||
| } | ||
| return result; | ||
| }); | ||
| }, | ||
|
|
||
| async getRawTransaction(txid: string): Promise<string> { | ||
| return provider.getRawTransaction(txid); | ||
| }, | ||
| }; | ||
| } | ||
This file contains hidden or 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,99 @@ | ||
| import type { Output } from "@bitauth/libauth"; | ||
| import type { ExtensionRegistry, ElectrumClient } from "./types"; | ||
| import type { IdentitySnapshot } from "../bcmr-v2.schema"; | ||
|
|
||
| // Import extension handlers | ||
| import { fetchLoanState } from "./parityusd"; | ||
|
|
||
| /** | ||
| * Registry of all available extensions | ||
| */ | ||
| export const extensions: ExtensionRegistry = { | ||
| parityusd: { | ||
| fetchLoanState, | ||
| }, | ||
| }; | ||
|
|
||
| /** | ||
| * Invoke extensions declared in a BCMR identity | ||
| * | ||
| * Extensions are called in order and can modify the UTXO before NFT parsing. | ||
| * Common use case: Fetch on-chain data and transplant into UTXO commitment. | ||
| * | ||
| * @param utxo - The UTXO to process | ||
| * @param identitySnapshot - BCMR identity snapshot containing extensions config | ||
| * @param electrumClient - Electrum client for blockchain data fetching | ||
| * @param networkPrefix - Network prefix ("bitcoincash" or "bchtest") | ||
| * @param extensionsEnabled - Optional map of extension names to enabled status | ||
| * @returns Modified UTXO with extension processing applied | ||
| */ | ||
| export async function invokeExtensions( | ||
| utxo: Output, | ||
| identitySnapshot: IdentitySnapshot, | ||
| electrumClient: ElectrumClient, | ||
| networkPrefix: string, | ||
| extensionsEnabled?: Record<string, boolean>, | ||
| ): Promise<Output> { | ||
| if (!identitySnapshot.extensions) { | ||
| return utxo; | ||
| } | ||
|
|
||
| let modifiedUtxo = utxo; | ||
|
|
||
| // Iterate through all extensions in the identity | ||
| for (const [extensionName, extensionConfig] of Object.entries( | ||
| identitySnapshot.extensions, | ||
| )) { | ||
| // Check if this extension is enabled (default to true if not specified) | ||
| const isEnabled = extensionsEnabled?.[extensionName] ?? true; | ||
| if (!isEnabled) { | ||
| console.log(`Extension ${extensionName} is disabled, skipping`); | ||
| continue; | ||
| } | ||
|
|
||
| const extensionHandlers = extensions[extensionName]; | ||
| if (!extensionHandlers) { | ||
| console.warn(`Unknown extension: ${extensionName}`); | ||
| continue; | ||
| } | ||
|
|
||
| // Iterate through all methods in this extension | ||
| for (const methodName of Object.keys(extensionConfig as object)) { | ||
| const handler = extensionHandlers[methodName]; | ||
| if (!handler) { | ||
| console.warn( | ||
| `Unknown method ${methodName} in extension ${extensionName}`, | ||
| ); | ||
| continue; | ||
| } | ||
|
|
||
| console.log( | ||
| `Invoking extension: ${extensionName}.${methodName}`, | ||
| ); | ||
|
|
||
| try { | ||
| modifiedUtxo = await handler( | ||
| modifiedUtxo, | ||
| identitySnapshot, | ||
| electrumClient, | ||
| networkPrefix, | ||
| ); | ||
| } catch (error) { | ||
| console.error( | ||
| `Error invoking extension ${extensionName}.${methodName}:`, | ||
| error, | ||
| ); | ||
| // Continue with unmodified UTXO on error | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return modifiedUtxo; | ||
| } | ||
|
|
||
| // Re-export types for convenience | ||
| export type { | ||
| ElectrumClient, | ||
| ElectrumUtxo, | ||
| ExtensionHandler, | ||
| } from "./types"; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.