11import { ProviderAPIInterface } from '@/types/provider' ;
22import { BTCRawInfo } from '@/types/activity' ;
33import { ChronikClient } from 'chronik-client' ;
4+ import { WatchOnlyWallet } from 'ecash-wallet' ;
45import { getAddress } from '../types/ecash-network' ;
56import { ECashNetworkInfo , ChronikTx } from '../types/ecash-chronik' ;
67import { Script , Address } from 'ecash-lib' ;
@@ -32,7 +33,7 @@ export class ChronikAPI extends ProviderAPIInterface {
3233 return this . withErrorHandling (
3334 'init' ,
3435 async ( ) => {
35- await this . chronik . blockchainInfo ( ) ;
36+ await this . chronik . chronikInfo ( ) ;
3637 } ,
3738 ( ) => {
3839 throw new Error ( 'Failed to initialize Chronik API' ) ;
@@ -41,7 +42,7 @@ export class ChronikAPI extends ProviderAPIInterface {
4142 }
4243
4344 private ensurePrefix ( address : string ) : string {
44- if ( address . startsWith ( 'ecash:' ) || address . startsWith ( 'ectest :') ) {
45+ if ( address . includes ( ' :') ) {
4546 return address ;
4647 }
4748 return `${ this . networkInfo . cashAddrPrefix } :${ address } ` ;
@@ -50,40 +51,25 @@ export class ChronikAPI extends ProviderAPIInterface {
5051 private async withErrorHandling < T > (
5152 method : string ,
5253 operation : ( ) => Promise < T > ,
53- fallback : ( ) => T ,
54+ fallback ? : ( ) => T | Promise < T > ,
5455 ) : Promise < T > {
5556 try {
5657 return await operation ( ) ;
5758 } catch ( error ) {
58- console . error ( `❌ [${ method } ] Error:` , error ) ;
59- return fallback ( ) ;
59+ console . error ( `[ChronikAPI:${ method } ]` , error ) ;
60+ if ( fallback ) return await fallback ( ) ;
61+ throw error ;
6062 }
6163 }
6264
63- private calculateUTXOBalance ( utxos : any [ ] ) : bigint {
64- return utxos . reduce ( ( total , utxo ) => {
65- if ( ! utxo . token ) {
66- const value = BigInt ( ( utxo as any ) . sats || utxo . value || 0 ) ;
67- return total + value ;
68- }
69- return total ;
70- } , BigInt ( 0 ) ) ;
71- }
72-
7365 async getBalance ( pubkey : string ) : Promise < string > {
7466 return this . withErrorHandling (
7567 'getBalance' ,
7668 async ( ) => {
7769 const address = getAddress ( pubkey ) ;
78-
79- const addressWithPrefix = this . ensurePrefix ( address ) ;
80- const utxoResponse = await this . chronik
81- . address ( addressWithPrefix )
82- . utxos ( ) ;
83-
84- const totalSatoshis = this . calculateUTXOBalance ( utxoResponse . utxos ) ;
85-
86- return totalSatoshis . toString ( ) ;
70+ const wallet = WatchOnlyWallet . fromAddress ( address , this . chronik ) ;
71+ await wallet . sync ( ) ;
72+ return wallet . balanceSats . toString ( ) ;
8773 } ,
8874 ( ) => '0' ,
8975 ) ;
@@ -103,7 +89,7 @@ export class ChronikAPI extends ProviderAPIInterface {
10389 ) ;
10490 }
10591
106- async getTransactionHistory ( address : string ) : Promise < any [ ] > {
92+ async getTransactionHistory ( address : string ) : Promise < ChronikTx [ ] > {
10793 return this . withErrorHandling (
10894 'getTransactionHistory' ,
10995 async ( ) => {
@@ -122,23 +108,19 @@ export class ChronikAPI extends ProviderAPIInterface {
122108 async ( ) => {
123109 const tx = await this . chronik . tx ( hash ) ;
124110
125- if ( ! tx . block ) {
126- return null ; // Transaction is in mempool
127- }
128-
129111 const rawInfo : BTCRawInfo = {
130- blockNumber : tx . block . height ,
112+ blockNumber : tx . block ? .height ?? 0 ,
131113 fee : this . calculateFee ( tx as any ) ,
132114 transactionHash : tx . txid ,
133- timestamp : tx . block . timestamp ,
134- inputs : tx . inputs . map ( ( input : any ) => ( {
135- address : this . scriptToAddress ( input . outputScript || '' ) ,
136- value : input . value || '0' ,
137- pkscript : input . outputScript || '' ,
115+ timestamp : tx . block ? .timestamp ?? Math . floor ( Date . now ( ) / 1000 ) ,
116+ inputs : tx . inputs . map ( input => ( {
117+ address : this . scriptToAddress ( input . outputScript ?? '' ) ,
118+ value : Number ( input . sats ) ,
119+ pkscript : input . outputScript ?? '' ,
138120 } ) ) ,
139- outputs : tx . outputs . map ( ( output : any ) => ( {
121+ outputs : tx . outputs . map ( output => ( {
140122 address : this . scriptToAddress ( output . outputScript ) ,
141- value : output . value ,
123+ value : Number ( output . sats ) ,
142124 pkscript : output . outputScript ,
143125 } ) ) ,
144126 } ;
@@ -151,11 +133,11 @@ export class ChronikAPI extends ProviderAPIInterface {
151133
152134 private calculateFee ( tx : ChronikTx ) : number {
153135 const inputSum = tx . inputs . reduce (
154- ( sum , input ) => sum + BigInt ( input . value || 0 ) ,
136+ ( sum , input ) => sum + input . sats ,
155137 BigInt ( 0 ) ,
156138 ) ;
157139 const outputSum = tx . outputs . reduce (
158- ( sum , output ) => sum + BigInt ( output . value || 0 ) ,
140+ ( sum , output ) => sum + output . sats ,
159141 BigInt ( 0 ) ,
160142 ) ;
161143 return Number ( inputSum - outputSum ) ;
@@ -167,13 +149,15 @@ export class ChronikAPI extends ProviderAPIInterface {
167149 try {
168150 const scriptBytes = Buffer . from ( scriptHex , 'hex' ) ;
169151 const script = new Script ( scriptBytes ) ;
170- const address = Address . fromScript ( script ) ;
171- const fullAddress = address . toString ( ) ;
152+ const fullAddress = Address . fromScript (
153+ script ,
154+ this . networkInfo . cashAddrPrefix ,
155+ ) . toString ( ) ;
172156
173157 return fullAddress . split ( ':' ) [ 1 ] || fullAddress ;
174158 } catch ( error ) {
175159 console . error (
176- '[scriptToAddress] Invalid script:' ,
160+ '[scriptToAddress] Could not derive address from script, only p2pkh and p2sh are supported :' ,
177161 scriptHex . slice ( 0 , 20 ) ,
178162 error ,
179163 ) ;
0 commit comments