All files / src/services Services.ts

73.86% Statements 147/199
52.83% Branches 28/53
72.22% Functions 26/36
73.36% Lines 135/184

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 36357x 57x 57x   57x 57x 57x 57x 57x   57x   71x                             71x   71x   71x       71x     71x     71x     71x     71x           21x   21x                 2x   2x   2x 2x   2x                   2x 2x     2x   2x 2x 2x 2x 2x 2x 2x       2x 2x     2x                         3x 3x 3x     3x                                                 12x     12x   12x 12x 12x 12x 8x   8x   8x 8x 8x 8x         4x   4x   4x   12x       9x   9x 9x 9x 8x   1x 1x 1x             4x 4x 3x   3x   4x       2x 2x   2x       3x 3x 3x   3x   3x         5x     5x   5x 5x 5x 5x   3x 3x 3x 3x 3x 2x   1x   2x   5x     71x       2x 2x 2x   2x 2x         2x       2x 2x 2x 2x 6x 2x 2x                 2x           2x       6x 6x       6x     6x   6x       6x 7x 6x                                   57x 2x 2x           2x             2x 2x       2x                     57x 3x 3x 3x 3x 3x 3x 3x 3x 3x    
import { Transaction as BsvTransaction, Beef, ChainTracker, Utils } from '@bsv/sdk'
import { asArray, asString, doubleSha256BE, sdk, sha256Hash, wait } from '../index.client'
import { ServiceCollection } from './ServiceCollection'
 
import { createDefaultWalletServicesOptions } from './createDefaultWalletServicesOptions'
import { ChaintracksChainTracker } from './chaintracker'
import { getTaalArcServiceConfig, makePostBeefToTaalARC, makePostTxsToTaalARC } from './providers/arcServices'
import { getMerklePathFromWhatsOnChainTsc, getRawTxFromWhatsOnChain, getUtxoStatusFromWhatsOnChain, updateBsvExchangeRate } from './providers/whatsonchain'
import { updateChaintracksFiatExchangeRates, updateExchangeratesapi } from './providers/echangeRates'
 
export class Services implements sdk.WalletServices {
    static createDefaultOptions(chain: sdk.Chain): sdk.WalletServicesOptions {
        return createDefaultWalletServicesOptions(chain)
    }
 
    options: sdk.WalletServicesOptions
 
    getMerklePathServices: ServiceCollection<sdk.GetMerklePathService>
    getRawTxServices: ServiceCollection<sdk.GetRawTxService>
    postTxsServices: ServiceCollection<sdk.PostTxsService>
    postBeefServices: ServiceCollection<sdk.PostBeefService>
    getUtxoStatusServices: ServiceCollection<sdk.GetUtxoStatusService>
    updateFiatExchangeRateServices: ServiceCollection<sdk.UpdateFiatExchangeRateService>
 
    chain: sdk.Chain
 
    constructor(optionsOrChain: sdk.Chain | sdk.WalletServicesOptions) {
        this.chain = (typeof optionsOrChain === 'string') ? optionsOrChain : optionsOrChain.chain
 
        this.options = (typeof optionsOrChain === 'string') ? Services.createDefaultOptions(this.chain) : optionsOrChain
 
        this.getMerklePathServices = new ServiceCollection<sdk.GetMerklePathService>()
            .add({ name: 'WhatsOnChainTsc', service: getMerklePathFromWhatsOnChainTsc })
        //.add({ name: 'Taal', service: makeGetMerklePathFromTaalARC(getTaalArcServiceConfig(this.chain, this.options.taalApiKey!)) })
 
        this.getRawTxServices = new ServiceCollection<sdk.GetRawTxService>()
            .add({ name: 'WhatsOnChain', service: getRawTxFromWhatsOnChain })
 
        this.postTxsServices = new ServiceCollection<sdk.PostTxsService>()
            .add({ name: 'TaalArcTxs', service: makePostTxsToTaalARC(getTaalArcServiceConfig(this.chain, this.options.taalApiKey!)) })
 
        this.postBeefServices = new ServiceCollection<sdk.PostBeefService>()
            .add({ name: 'TaalArcBeef', service: makePostBeefToTaalARC(getTaalArcServiceConfig(this.chain, this.options.taalApiKey!)) })
 
        this.getUtxoStatusServices = new ServiceCollection<sdk.GetUtxoStatusService>()
            .add({ name: 'WhatsOnChain', service: getUtxoStatusFromWhatsOnChain })
 
        this.updateFiatExchangeRateServices = new ServiceCollection<sdk.UpdateFiatExchangeRateService>()
            .add({ name: 'ChaintracksService', service: updateChaintracksFiatExchangeRates })
            .add({ name: 'exchangeratesapi', service: updateExchangeratesapi })
    }
 
    async getChainTracker(): Promise<ChainTracker> {
        Iif (!this.options.chaintracks)
            throw new sdk.WERR_INVALID_PARAMETER('options.chaintracks', `valid to enable 'getChainTracker' service.`)
        return new ChaintracksChainTracker(this.chain, this.options.chaintracks)
    }
 
    async getBsvExchangeRate(): Promise<number> {
        this.options.bsvExchangeRate = await updateBsvExchangeRate(this.options.bsvExchangeRate, this.options.bsvUpdateMsecs)
        return this.options.bsvExchangeRate.rate
    }
 
    async getFiatExchangeRate(currency: "USD" | "GBP" | "EUR", base?: "USD" | "GBP" | "EUR"): Promise<number> {
        const rates = await this.updateFiatExchangeRates(this.options.fiatExchangeRates, this.options.fiatUpdateMsecs)
 
        this.options.fiatExchangeRates = rates
 
        base ||= 'USD'
        const rate = rates.rates[currency] / rates.rates[base]
 
        return rate
    }
 
    get getProofsCount() { return this.getMerklePathServices.count }
    get getRawTxsCount() { return this.getRawTxServices.count }
    get postTxsServicesCount() { return this.postTxsServices.count }
    get postBeefServicesCount() { return this.postBeefServices.count }
    get getUtxoStatsCount() { return this.getUtxoStatusServices.count }
 
    async getUtxoStatus(output: string, outputFormat?: sdk.GetUtxoStatusOutputFormat, useNext?: boolean): Promise<sdk.GetUtxoStatusResult> {
        const services = this.getUtxoStatusServices
        Iif (useNext)
            services.next()
 
        let r0: sdk.GetUtxoStatusResult = { name: "<noservices>", status: "error", error: new sdk.WERR_INTERNAL('No services available.'), details: [] }
 
        for (let retry = 0; retry < 2; retry++) {
            for (let tries = 0; tries < services.count; tries++) {
                const service = services.service
                const r = await service(output, this.chain, outputFormat)
                if (r.status === 'success') {
                    r0 = r
                    break
                }
                services.next()
            }
            if (r0.status === 'success')
                break;
            await wait(2000)
        }
        return r0
    }
 
    /**
     * The beef must contain at least each rawTx for each txid.
     * Some services may require input transactions as well.
     * These will be fetched if missing, greatly extending the service response time.
     * @param beef 
     * @param txids
     * @returns
     */
    async postTxs(beef: Beef, txids: string[]): Promise<sdk.PostTxsResult[]> {
 
        const rs = await Promise.all(this.postTxsServices.allServices.map(async service => {
            const r = await service(beef, txids, this)
            return r
        }))
 
        return rs
    }
 
    /**
     * 
     * @param beef 
     * @param chain 
     * @returns
     */
    async postBeef(beef: Beef, txids: string[]): Promise<sdk.PostBeefResult[]> {
 
        let rs = await Promise.all(this.postBeefServices.allServices.map(async service => {
            const r = await service(beef, txids, this)
            return r
        }))
 
        Iif (rs.every(r => r.status !== 'success')) {
            rs = await this.postTxs(beef, txids)
        }
 
        return rs
    }
 
    async getRawTx(txid: string, useNext?: boolean): Promise<sdk.GetRawTxResult> {
 
        Iif (useNext)
            this.getRawTxServices.next()
 
        const r0: sdk.GetRawTxResult = { txid }
 
        for (let tries = 0; tries < this.getRawTxServices.count; tries++) {
            const service = this.getRawTxServices.service
            const r = await service(txid, this.chain)
            if (r.rawTx) {
                const hash = asString(doubleSha256BE(r.rawTx!))
                // Confirm transaction hash matches txid
                if (hash === asString(txid)) {
                    // If we have a match, call it done.
                    r0.rawTx = r.rawTx
                    r0.name = r.name
                    r0.error = undefined
                    break
                }
                r.error = new sdk.WERR_INTERNAL(`computed txid ${hash} doesn't match requested value ${txid}`)
                r.rawTx = undefined
            }
            if (r.error && !r0.error && !r0.rawTx)
                // If we have an error and didn't before...
                r0.error = r.error
 
            this.getRawTxServices.next()
        }
        return r0
    }
 
    async invokeChaintracksWithRetry<R>(method: () => Promise<R>): Promise<R> {
        Iif (!this.options.chaintracks)
            throw new sdk.WERR_INVALID_PARAMETER('options.chaintracks', 'valid for this service operation.');
        for (let retry = 0; retry < 3; retry++) {
            try {
                const r: R = await method()
                return r
            } catch (eu: unknown) {
                const e = sdk.WalletError.fromUnknown(eu)
                if (e.code != 'ECONNRESET')
                    throw eu
            }
        }
        throw new sdk.WERR_INVALID_OPERATION('hashToHeader service unavailable')
    }
 
    async getHeaderForHeight(height: number): Promise<number[]> {
        const method = async () => {
            const header = await this.options.chaintracks!.findHeaderForHeight(height)
            Iif (!header)
                throw new sdk.WERR_INVALID_PARAMETER('hash', `valid height '${height}' on mined chain ${this.chain}`);
            return toBinaryBaseBlockHeader(header)
        }
        return this.invokeChaintracksWithRetry(method)
    }
 
    async getHeight(): Promise<number> {
        const method = async () => {
            return await this.options.chaintracks!.currentHeight()
        }
        return this.invokeChaintracksWithRetry(method)
    }
 
    async hashToHeader(hash: string): Promise<sdk.BlockHeader> {
        const method = async () => {
            const header = await this.options.chaintracks!.findHeaderForBlockHash(hash)
            Iif (!header)
                throw new sdk.WERR_INVALID_PARAMETER('hash', `valid blockhash '${hash}' on mined chain ${this.chain}`);
            return header
        }
        return this.invokeChaintracksWithRetry(method)
    }
 
    async getMerklePath(txid: string, useNext?: boolean): Promise<sdk.GetMerklePathResult> {
 
        Iif (useNext)
            this.getMerklePathServices.next()
 
        const r0: sdk.GetMerklePathResult = {}
 
        for (let tries = 0; tries < this.getMerklePathServices.count; tries++) {
            const service = this.getMerklePathServices.service
            const r = await service(txid, this.chain, this)
            if (r.merklePath) {
                // If we have a proof, call it done.
                r0.merklePath = r.merklePath
                r0.header = r.header
                r0.name = r.name
                r0.error = undefined
                break
            } else if (r.error && !r0.error)
                // If we have an error and didn't before...
                r0.error = r.error
 
            this.getMerklePathServices.next()
        }
        return r0
    }
 
    targetCurrencies = ['USD', 'GBP', 'EUR']
 
    async updateFiatExchangeRates(rates?: sdk.FiatExchangeRates, updateMsecs?: number): Promise<sdk.FiatExchangeRates> {
 
        updateMsecs ||= 1000 * 60 * 15
        const freshnessDate = new Date(Date.now() - updateMsecs)
        if (rates) {
            // Check if the rate we know is stale enough to update.
            updateMsecs ||= 1000 * 60 * 15
            Iif (rates.timestamp > freshnessDate)
                return rates
        }
 
        // Make sure we always start with the first service listed (chaintracks aggregator)
        const services = this.updateFiatExchangeRateServices.clone()
 
        let r0: sdk.FiatExchangeRates | undefined
 
        for (let tries = 0; tries < services.count; tries++) {
            const service = services.service
            try {
                const r = await service(this.targetCurrencies, this.options)
                if (this.targetCurrencies.every(c => typeof r.rates[c] === 'number')) {
                    r0 = r
                    break
                }
            } catch (eu: unknown) {
                const e = sdk.WalletError.fromUnknown(eu)
                console.error(`updateFiatExchangeRates servcice name ${service.name} error ${e.message}`)
            }
            services.next()
        }
 
        Iif (!r0) {
            console.error('Failed to update fiat exchange rates.')
            Iif (!rates) throw new sdk.WERR_INTERNAL()
            return rates
        }
 
        return r0
    }
 
    async nLockTimeIsFinal(tx: string | number[] | BsvTransaction | number): Promise<boolean> {
        const MAXINT = 0xffffffff
        const BLOCK_LIMIT = 500000000
 
        let nLockTime: number
 
        Iif (typeof tx === 'number')
            nLockTime = tx
        else {
            Iif (typeof tx === 'string') {
                tx = BsvTransaction.fromHex(tx)
            } else Iif (Array.isArray(tx)) {
                tx = BsvTransaction.fromBinary(tx)
            }
 
            if (tx instanceof BsvTransaction) {
                if (tx.inputs.every(i => i.sequence === MAXINT)) {
                    return true
                }
                nLockTime = tx.lockTime
            } else E{
                throw new sdk.WERR_INTERNAL('Should be either @bsv/sdk Transaction or babbage-bsv Transaction')
            }
        }
 
        Iif (nLockTime >= BLOCK_LIMIT) {
            const limit = Math.floor(Date.now() / 1000)
            return nLockTime < limit
        }
 
        const height = await this.getHeight()
        return nLockTime < height
    }
}
 
export function validateScriptHash(output: string, outputFormat?: sdk.GetUtxoStatusOutputFormat): string {
    let b = asArray(output)
    Iif (!outputFormat) {
        if (b.length === 32)
            outputFormat = 'hashLE'
        else
            outputFormat = 'script'
    }
    switch (outputFormat) {
        case 'hashBE':
            break;
        case 'hashLE':
            b = b.reverse()
            break;
        case 'script':
            b = sha256Hash(b).reverse()
            break;
        default:
            throw new sdk.WERR_INVALID_PARAMETER('outputFormat', `not be ${outputFormat}`)
    }
    return asString(b)
}
 
/**
 * Serializes a block header as an 80 byte array.
 * The exact serialized format is defined in the Bitcoin White Paper
 * such that computing a double sha256 hash of the array computes
 * the block hash for the header.
 * @returns 80 byte array
 * @publicbody
 */
export function toBinaryBaseBlockHeader(header: sdk.BaseBlockHeader): number[] {
    const writer = new Utils.Writer()
    writer.writeUInt32BE(header.version)
    writer.writeReverse(asArray(header.previousHash))
    writer.writeReverse(asArray(header.merkleRoot))
    writer.writeUInt32BE(header.time)
    writer.writeUInt32BE(header.bits)
    writer.writeUInt32BE(header.nonce)
    const r = writer.toArray()
    return r
}