Skip to content

Commit

Permalink
updated electrs client
Browse files Browse the repository at this point in the history
  • Loading branch information
cf committed Jun 30, 2024
1 parent 6852f09 commit 7bc8113
Show file tree
Hide file tree
Showing 10 changed files with 661 additions and 81 deletions.
10 changes: 5 additions & 5 deletions docs/ThirdPartyWallets.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ interface IDogeWalletProvider {
For example, here is a reference implementation for a Ledger hardware wallet:
```typescript
import {
DogeLinkRPC,
IDogeLinkRPC,
IDogeTransactionSigner,
IDogeWalletProvider,
ISignatureResult,
Expand All @@ -54,10 +54,10 @@ class LedgerHardwareWalletSigner implements IDogeTransactionSigner {
walletPath: string;
ledgerInstance: LedgerBitcoinApp;
cachedPublicKey: string = "";
rpc: DogeLinkRPC;
rpc: IDogeLinkRPC;
constructor(
walletPath: string,
rpc: DogeLinkRPC,
rpc: IDogeLinkRPC,
ledgerInstance: LedgerBitcoinApp
) {
this.walletPath = walletPath;
Expand Down Expand Up @@ -197,10 +197,10 @@ class LedgerHardwareWalletProvider implements IDogeWalletProvider {
numberOfWallets: number;
ledgerInstance: LedgerBitcoinApp;
signers: LedgerHardwareWalletSigner[] = [];
rpc: DogeLinkRPC;
rpc: IDogeLinkRPC;

constructor(
rpc: DogeLinkRPC,
rpc: IDogeLinkRPC,
ledgerInstance: LedgerBitcoinApp,
numberOfWallets = 8
) {
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/p2pkh/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { decodeAddress, getP2PKHOutputScript, getAddressFromP2PKHOutputScript } from "../../address";
import { hashHex } from "../../hash";
import { DogeLinkRPC } from "../../rpc";
import { IDogeLinkRPC } from "../../rpc/types";
import { TransactionBuilder } from "../../transaction/builder";
import { normailzeTransactionOutput } from "../../transaction/normalize";
import { ITransactionOutputUser } from "../../transaction/types";
Expand Down Expand Up @@ -42,7 +42,7 @@ async function createP2PKHTransactionAsync(signer: IDogeTransactionSigner, param
return createP2PKHTransactionInternal(signer, publicKeyHash, params.inputs, params.outputs);
}
}
async function resolveP2PKHParams(rpc: DogeLinkRPC, signer: IDogeTransactionSigner, params: ICreateP2PKHRPCParams): Promise<ICreateP2PKHParams> {
async function resolveP2PKHParams(rpc: IDogeLinkRPC, signer: IDogeTransactionSigner, params: ICreateP2PKHRPCParams): Promise<ICreateP2PKHParams> {
const resolver = new P2PKHTransactionInfoResolver(signer, params.inputs, params.address, rpc);
const {inputs, lastOutputScript} = await resolver.resolveInputs();
const address = getAddressFromP2PKHOutputScript(lastOutputScript, rpc.getNetwork().networkId);
Expand Down
6 changes: 3 additions & 3 deletions src/helpers/p2pkh/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { decodeAddress, getP2PKHOutputScript, getAddressFromP2PKHOutputScript } from "../../address";
import { hashHex } from "../../hash";
import { DogeLinkRPC } from "../../rpc";
import { IDogeLinkRPC } from "../../rpc/types";
import { Transaction } from "../../transaction";
import { seq } from "../../utils/misc";
import { IDogeTransactionSigner } from "../../wallet/types";
Expand All @@ -12,15 +12,15 @@ function isNormalizedFundingUTXO(input: IP2PKHFundingUTXOInput): input is IP2PKH

class P2PKHTransactionInfoResolver {
inputs: IP2PKHFundingUTXOInput[];
rpc?: DogeLinkRPC;
rpc?: IDogeLinkRPC;
signer: IDogeTransactionSigner;
unresolvedInputIndexes: number[];
resolvedTransactions: Record<number, Transaction> = {};
neededTransactions: number[] = [];
resolvedInputIndexes: number[] = [];
address: string | undefined;
lastOutputScript?: Uint8Array;
constructor(signer: IDogeTransactionSigner, inputs: IP2PKHFundingUTXOInput[], address?: string | undefined, rpc?: DogeLinkRPC) {
constructor(signer: IDogeTransactionSigner, inputs: IP2PKHFundingUTXOInput[], address?: string | undefined, rpc?: IDogeLinkRPC) {
this.inputs = inputs;
this.rpc = rpc;
this.signer = signer;
Expand Down
19 changes: 19 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,28 @@ export type {
IUTXO,
IBaseUTXO,
IDogeLinkRPCInfo,
IDogeLinkRPC,
} from './rpc/types';
export * from './rpc';

export type {
ITxVout,
ITxVin,
ITxConfirmedStatusFalse,
ITxConfirmedStatusTrue,
ITXConfirmedStatus,
IGetTXResponse,
IBasicBlock,
IBlockStatus,
IAddressStats,
IAddressStatsResponse,
IScriptHashStatsResponse,
IMempoolStatus,
IMempoolRecentTransaction,
ITransactionOutSpend,
IDogeLinkElectrsRPC,
} from './rpc/electrsTypes';


export type {
ISpendP2PKHParams,
Expand Down
147 changes: 147 additions & 0 deletions src/rpc/electrsTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import { IDogeLinkRPC, IUTXO } from "./types";

interface ITxVout {
scriptpubkey: string;
scriptpubkey_asm: string;
scriptpubkey_type: string;
scriptpubkey_address: string;
value: number;
}
interface ITxVin {
txid: string;
vout: number;
prevout: ITxVout;
scriptsig: string;
scriptsig_asm: string;
is_coinbase: boolean;
sequence: number;
}
interface ITxConfirmedStatusFalse {
confirmed: false;
block_height?: number;
block_hash?: string;
block_time?: number;
}
interface ITxConfirmedStatusTrue {
confirmed: true;
block_height: number;
block_hash: string;
block_time: number;
}
type ITXConfirmedStatus = ITxConfirmedStatusFalse | ITxConfirmedStatusTrue;

interface IGetTXResponse {
txid: string;
vin: ITxVin[];
vout: ITxVout[];
version: number;
locktime: number;
size: number;
weight: number;
fee: number;
status: ITXConfirmedStatus;
}
interface IBasicBlock {
id: string;
height: number;
version: number;
timestamp: number;
tx_count: number;
size: number;
weight: number;
merkle_root: string;
previousblockhash: string;
mediantime: number;
nonce: number;
bits: number;
difficulty: number;
}
interface IBlockStatus {
in_best_chain: boolean;
height: number;
next_best: string;
}

interface IAddressStats {
funded_txo_count: number;
funded_txo_sum: number;
spent_txo_count: number;
spent_txo_sum: number;
tx_count: number;
}
interface IAddressStatsResponse {
address: string;
chain_stats: IAddressStats;
mempool_stats: IAddressStats;
}
interface IScriptHashStatsResponse {
scripthash: string;
chain_stats: IAddressStats;
mempool_stats: IAddressStats;
}
interface IMempoolStatus {
count: number;
vsize: number;
total_fee: number;
fee_histogram: [number, number][];
}
interface IMempoolRecentTransaction {
txid: string;
fee: number;
vsize: number;
value: number;
}

interface ITransactionOutSpendBase {
spent: boolean;
txid?: string;
vin?: number;
status?: ITXConfirmedStatus;

}
interface ITransactionOutSpendSpent extends ITransactionOutSpendBase {
spent: true;
txid: string;
vin: number;
status: ITXConfirmedStatus;
}
interface ITransactionOutSpendUnspent extends ITransactionOutSpendBase {
spent: false;
}
type ITransactionOutSpend = ITransactionOutSpendSpent | ITransactionOutSpendUnspent;

interface IDogeLinkElectrsRPC extends IDogeLinkRPC {
getBlockStatus(hash: string): Promise<IBlockStatus>;
getBlockGroup(start?: number): Promise<IBasicBlock[]>;
getBlockBasic(hash: string): Promise<IBasicBlock>;
getBalance(address: string): Promise<number>;
getTransactionsFor(addressOrScriptHash: string, confirmed?: boolean, afterTxid?: string): Promise<IGetTXResponse[]>;
getStatsFor(addressOrScriptHash: string): Promise<IAddressStatsResponse | IScriptHashStatsResponse>;
getBlockHeight(): Promise<number>;
getTransactionElectrs(txId: string): Promise<IGetTXResponse>;
getTransactionElectrs(txId: string, rawHex: true): Promise<string>;
getTransactionElectrs(txId: string, rawHex: false): Promise<IGetTXResponse>;
getTransactionElectrs(txId: string, rawHex: undefined): Promise<IGetTXResponse>;
getUTXOs(addressOrScriptHash: string): Promise<IUTXO[]>;
waitUntilUTXO(address: string, pollInterval?: number, maxAttempts?: number): Promise<IUTXO[]>;
getMempoolStatus(): Promise<IMempoolStatus>;
getMempoolRecentTransactions(): Promise<IMempoolRecentTransaction[]>;
getTransactionOutSpends(txid: string): Promise<ITransactionOutSpend[]>;
}
export type {
ITxVout,
ITxVin,
ITxConfirmedStatusFalse,
ITxConfirmedStatusTrue,
ITXConfirmedStatus,
IGetTXResponse,
IBasicBlock,
IBlockStatus,
IAddressStats,
IAddressStatsResponse,
IScriptHashStatsResponse,
IMempoolStatus,
IMempoolRecentTransaction,
ITransactionOutSpend,
IDogeLinkElectrsRPC,
}
3 changes: 2 additions & 1 deletion src/rpc/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './linkRPC';
export * from './linkElectrs';
export * from './linkElectrs';
export * from './linkElectrsCombo';
Loading

0 comments on commit 7bc8113

Please sign in to comment.