130 lines
3.7 KiB
TypeScript
130 lines
3.7 KiB
TypeScript
import {
|
|
BlockchainElectrum,
|
|
encodeExtendedJson,
|
|
Transaction,
|
|
} from '@xocash/stack';
|
|
import { ref } from 'vue';
|
|
|
|
export const electrumClient = {
|
|
from: [
|
|
{
|
|
label: 'From Servers',
|
|
inputs: ['Servers'],
|
|
models: [
|
|
'bch.imaginary.cash, cashnode.bch.ninja, electrum.imaginary.cash',
|
|
],
|
|
callback: async (args: [string]) => {
|
|
const electrum = await BlockchainElectrum.from({
|
|
opts: {
|
|
servers: args[0].replaceAll(' ', '').split(','),
|
|
application: 'XO-tools',
|
|
},
|
|
});
|
|
await electrum.start();
|
|
|
|
return electrum;
|
|
},
|
|
},
|
|
],
|
|
to: [],
|
|
methods: [
|
|
{
|
|
label: 'Request',
|
|
inputs: ['Method', 'Parameters'],
|
|
dynamicInputs: true,
|
|
models: ['', ''],
|
|
callback: async (
|
|
electrum: BlockchainElectrum,
|
|
args: [string, string],
|
|
) => {
|
|
const [method, ...parameters] = args;
|
|
|
|
// If the first parameter is empty, don't include it in the request
|
|
// This parameter can not be removed in the UI so we handle a blank parameter gracefully
|
|
// Since other parameters can be removed, we assume that blank parameters are intentional and so we include them as part of the request
|
|
if (!parameters[0] && parameters.length === 1) {
|
|
parameters.shift();
|
|
}
|
|
|
|
const response = await electrum.request(method, ...parameters);
|
|
return encodeExtendedJson(response, 2);
|
|
},
|
|
},
|
|
{
|
|
label: 'Fetch Unspents',
|
|
inputs: ['Address'],
|
|
models: [''],
|
|
callback: async (electrum: BlockchainElectrum, args: [string]) => {
|
|
const unspents = await electrum.fetchUnspents(args[0]);
|
|
return encodeExtendedJson(unspents.toArray(), 2);
|
|
},
|
|
},
|
|
{
|
|
label: 'Fetch Transaction',
|
|
inputs: ['Transaction Hash'],
|
|
models: [''],
|
|
callback: async (electrum: BlockchainElectrum, args: [string]) => {
|
|
const transaction = await electrum.fetchTransaction(args[0]);
|
|
return encodeExtendedJson(transaction, 2);
|
|
},
|
|
},
|
|
{
|
|
label: 'Fetch Address History',
|
|
inputs: ['Address'],
|
|
models: [''],
|
|
callback: async (electrum: BlockchainElectrum, args: [string]) => {
|
|
const transactions = await electrum.fetchAddressHistory(args[0]);
|
|
return encodeExtendedJson(transactions.toArray(), 2);
|
|
},
|
|
},
|
|
{
|
|
label: 'Broadcast Transaction',
|
|
inputs: ['Transaction'],
|
|
models: [''],
|
|
callback: async (electrum: BlockchainElectrum, args: [string]) => {
|
|
const tx = Transaction.from(args[0]);
|
|
return await electrum.broadcastTransaction(tx);
|
|
},
|
|
},
|
|
{
|
|
label: 'Subscribe Address',
|
|
inputs: ['Address'],
|
|
models: [''],
|
|
callback: async (electrum: BlockchainElectrum, args: [string]) => {
|
|
// Use a vue ref so we can update the UI dynamically.
|
|
const addressEvents = ref<string>('');
|
|
|
|
await electrum.subscribeAddress(args[0], (status) => {
|
|
// Don't update if its null
|
|
if (!status) {
|
|
return;
|
|
}
|
|
|
|
addressEvents.value += `${status}\n`;
|
|
});
|
|
|
|
return addressEvents;
|
|
},
|
|
},
|
|
{
|
|
label: 'Subscribe Transaction',
|
|
inputs: ['Transaction Hash'],
|
|
models: [''],
|
|
callback: async (electrum: BlockchainElectrum, args: [string]) => {
|
|
const transactionEvents = ref<string>('');
|
|
|
|
await electrum.subscribeTransaction(args[0], (transaction) => {
|
|
// Don't update if its null
|
|
if (!transaction) {
|
|
return;
|
|
}
|
|
|
|
transactionEvents.value += `${transaction}\n`;
|
|
});
|
|
|
|
return transactionEvents;
|
|
},
|
|
},
|
|
],
|
|
};
|