From ce688ce0a436c67efdc8c7320c195fdeba56cb42 Mon Sep 17 00:00:00 2001 From: James Zuccon Date: Fri, 6 Sep 2024 12:10:09 +1000 Subject: [PATCH] WIP Signed-off-by: James Zuccon --- docs/.vitepress/theme/Layout.vue | 4 +- docs/.vitepress/theme/style.css | 92 ++++++++++++++++---- packages/primitives/src/transaction-graph.ts | 2 +- packages/primitives/src/transaction.ts | 8 ++ packages/wallet/src/wallet/blockchain.ts | 46 ++++++++-- 5 files changed, 124 insertions(+), 28 deletions(-) diff --git a/docs/.vitepress/theme/Layout.vue b/docs/.vitepress/theme/Layout.vue index 5970ec5..66dd769 100644 --- a/docs/.vitepress/theme/Layout.vue +++ b/docs/.vitepress/theme/Layout.vue @@ -8,7 +8,7 @@ const { Layout } = DefaultTheme \ No newline at end of file + diff --git a/docs/.vitepress/theme/style.css b/docs/.vitepress/theme/style.css index 0ac631e..240016f 100644 --- a/docs/.vitepress/theme/style.css +++ b/docs/.vitepress/theme/style.css @@ -43,7 +43,7 @@ * in custom container, badges, etc. * -------------------------------------------------------------------------- */ - :root { +:root { --vp-c-default-1: var(--vp-c-gray-1); --vp-c-default-2: var(--vp-c-gray-2); --vp-c-default-3: var(--vp-c-gray-3); @@ -70,22 +70,6 @@ --vp-c-danger-soft: var(--vp-c-red-soft); } -.VPSidebarItem.level-2.is-active > .item > .indicator, -.VPSidebarItem.level-3.is-active > .item > .indicator, -.VPSidebarItem.level-4.is-active > .item > .indicator, -.VPSidebarItem.level-5.is-active > .item > .indicator { - background-color: #b2fb54 !important; -} - -.VPSidebarItem.level-0.is-active > .item .link > .text, -.VPSidebarItem.level-1.is-active > .item .link > .text, -.VPSidebarItem.level-2.is-active > .item .link > .text, -.VPSidebarItem.level-3.is-active > .item .link > .text, -.VPSidebarItem.level-4.is-active > .item .link > .text, -.VPSidebarItem.level-5.is-active > .item .link > .text { - color: #b2fb54 !important; -} - /** * Component: Button * -------------------------------------------------------------------------- */ @@ -153,3 +137,77 @@ --docsearch-primary-color: var(--vp-c-brand-1) !important; } +/** + * Overrides + */ + +:root { + --primary: #2c7d00; + --vp-nav-logo-height: 48px; +} + +html.dark { + --primary: #b2fb54; +} + +body { + font-size: 14px; +} + +.vp-nav .title { + font-size: 24px !important; +} + +.vp-doc h1 { + font-size: 24px; +} + +.vp-doc h2 { + font-size: 20px; +} + +.vp-doc h3 { + font-size: 16px; +} + +.vp-doc h4 { + font-size: 14px; + opacity: 0.75; +} + +.VPSidebarItem .text { + font-size: 12px; +} + +.VPSidebarItem.level-2.is-active > .item > .indicator, +.VPSidebarItem.level-3.is-active > .item > .indicator, +.VPSidebarItem.level-4.is-active > .item > .indicator, +.VPSidebarItem.level-5.is-active > .item > .indicator { + background-color: var(--primary) !important; +} + +.VPSidebarItem.level-0.is-active > .item .link > .text, +.VPSidebarItem.level-1.is-active > .item .link > .text, +.VPSidebarItem.level-2.is-active > .item .link > .text, +.VPSidebarItem.level-3.is-active > .item .link > .text, +.VPSidebarItem.level-4.is-active > .item .link > .text, +.VPSidebarItem.level-5.is-active > .item .link > .text { + color: var(--primary) !important; +} + +.VPSidebarItem.level-0.is-link > .item > .link:hover .text, +.VPSidebarItem.level-1.is-link > .item > .link:hover .text, +.VPSidebarItem.level-2.is-link > .item > .link:hover .text, +.VPSidebarItem.level-3.is-link > .item > .link:hover .text, +.VPSidebarItem.level-4.is-link > .item > .link:hover .text, +.VPSidebarItem.level-5.is-link > .item > .link:hover .text { + color: var(--primary) !important; +} + +.outline-title { + font-size: 12px !important; +} + +.outline-link { + font-size: 12px !important; +} diff --git a/packages/primitives/src/transaction-graph.ts b/packages/primitives/src/transaction-graph.ts index 7e3a182..65b867b 100644 --- a/packages/primitives/src/transaction-graph.ts +++ b/packages/primitives/src/transaction-graph.ts @@ -1,4 +1,4 @@ -import { Transaction } from '@xo/primitives'; +import { Transaction } from './transaction.js'; import { binToHex, Output } from '@bitauth/libauth'; diff --git a/packages/primitives/src/transaction.ts b/packages/primitives/src/transaction.ts index f9c5be7..c2449e8 100644 --- a/packages/primitives/src/transaction.ts +++ b/packages/primitives/src/transaction.ts @@ -59,6 +59,14 @@ export class Transaction { return Transaction.fromBytes(bytes); } + public static fromAny(bytesOrHex: Uint8Array | string) { + if (typeof bytesOrHex === 'string') { + return Transaction.fromHex(bytesOrHex); + } + + return Transaction.fromBytes(bytesOrHex); + } + public toBytes(): Uint8Array { const bytes = encodeTransaction(this.transaction); diff --git a/packages/wallet/src/wallet/blockchain.ts b/packages/wallet/src/wallet/blockchain.ts index 91ea11d..4bebdf7 100644 --- a/packages/wallet/src/wallet/blockchain.ts +++ b/packages/wallet/src/wallet/blockchain.ts @@ -1,4 +1,4 @@ -import { Transaction } from '@xo/primitives'; +import { Transaction, TransactionGraph } from '@xo/primitives'; import { AddressNotification, ElectrumRequest } from './blockchain-types.js'; import { ElectrumClient } from '@electrum-cash/network'; @@ -9,6 +9,8 @@ export type AddressCallback = (status: string | null) => void; // For local state, we'll want to track a tree of dependencies locally. // Might be able to copy whatever structure a proper node uses (DAG?). export class Blockchain { + pendingTransactions: TransactionGraph = new TransactionGraph(); + electrumClient!: ElectrumClient; // Address subscriptions. @@ -76,21 +78,49 @@ export class Blockchain { return response; } - // TODO: async commit() method - // This won't actually broadcast, this will just commit the transaction to our graph. - // The reason we want this is because, for protos like BIP70, we want service to verify the tx and broadcast it. - // And we just want to be able to monitor whether service did the needful (broadcasted it). - // This should accept options: { waitForSeen: boolean } + /** + * This won't actually broadcast, this will just commit the transaction to our graph. + * The reason we want this is because, for protos like BIP70, we want service to verify the tx and broadcast it. + * And we just want to be able to monitor whether service did the needful (broadcasted it). + * This should accept options: { waitForSeen: boolean } + */ async commitTransaction( - transaction: Transaction, + transaction: Uint8Array | Transaction | string, opts: { waitUntilSeen: boolean } = { waitUntilSeen: true } ) { - console.log(transaction); + if (typeof transaction === 'string') { + transaction = Transaction.fromHex(transaction); + } + if (transaction instanceof Uint8Array) { + transaction = Transaction.fromBytes(transaction); + } + + this.pendingTransactions.addTransaction(transaction); + + // TODO: Wait for transaction to be seen on network. console.log(opts); } // TODO: async broadcast() method // This should accept options: { waitForSeen: boolean } + async broadcastTransaction( + transaction: Uint8Array | Transaction | string, + opts: { waitUntilSeen: boolean } = { waitUntilSeen: true } + ) { + if (typeof transaction === 'string') { + transaction = Transaction.fromHex(transaction); + } + if (transaction instanceof Uint8Array) { + transaction = Transaction.fromBytes(transaction); + } + + this.pendingTransactions.addTransaction(transaction); + + // TODO: Broadcast the transaction. + + // TODO: Wait for transaction to be seen on network. + console.log(opts); + } async subscribeAddress(address: string, callback: AddressCallback) { await this.electrumClient.subscribe(