|
|
|
@@ -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(
|
|
|
|
|