All files / src/services/providers arcServices.ts

27.89% Statements 53/190
15.06% Branches 11/73
28.57% Functions 6/21
29.05% Lines 52/179

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 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 46357x 57x   57x 57x     57x 57x                         57x 142x                   57x 71x 3x       57x 71x         57x                                                                                                                       57x                                                   57x 3x   3x 3x             3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x   3x 3x   3x   3x         3x   3x 3x 3x 3x 3x     3x 3x     3x 3x 3x     3x   3x         3x                                                                                             57x                                                                         57x                                                                                                                                                                       57x                                                                                                                                                     57x                                                              
import { Transaction as BsvTransaction, ARC, ArcConfig, Beef, defaultHttpClient, HttpClient, HttpClientRequestOptions, MerklePath } from '@bsv/sdk'
import { asArray, randomBytesHex, sdk } from "../../index.client";
 
import axios from 'axios'
import { Readable } from 'stream'
import { PostTxResultForTxid } from '../../sdk';
 
const BEEF_V1 = 4022206465 // 0100BEEF in LE order
const BEEF_V2 = 4022206466 // 0200BEEF in LE order
 
// Documentation:
// https://docs.taal.com/
// https://docs.taal.com/core-products/transaction-processing/arc-endpoints
// https://bitcoin-sv.github.io/arc/api.html
 
export interface ArcServiceConfig {
    name: string,
    url: string,
    arcConfig: ArcConfig
}
 
export function getTaalArcServiceConfig(chain: sdk.Chain, apiKey: string): ArcServiceConfig {
    return {
        name: 'TaalArc',
        url: chain === 'main' ? 'https://api.taal.com/arc' : 'https://arc-test.taal.com',
        arcConfig: {
            apiKey,
            deploymentId: `WalletServices-${randomBytesHex(16)}`
        },
    }
}
 
export function makePostTxsToTaalARC(config: ArcServiceConfig): sdk.PostTxsService {
    return (beef: Beef, txids: string[], services: sdk.WalletServices) => {
        return postTxsToTaalArcMiner(beef, txids, config, services)
    }
}
 
export function makePostBeefToTaalARC(config: ArcServiceConfig) : sdk.PostBeefService {
    return (beef: Beef, txids: string[], services: sdk.WalletServices) => {
        return postBeefToTaalArcMiner(beef, txids, config, services)
    }
}
 
export function makeGetMerklePathFromTaalARC(config: ArcServiceConfig) : sdk.GetMerklePathService {
    return (txid: string, chain: sdk.Chain, services: sdk.WalletServices) => {
        return getMerklePathFromTaalARC(txid, config, services)
    }
}
 
class ArcServices {
    readonly URL: string
    readonly apiKey: string | undefined
    readonly deploymentId: string
    readonly callbackUrl: string | undefined
    readonly callbackToken: string | undefined
    readonly headers: Record<string, string> | undefined
    private readonly httpClient: HttpClient
 
    /**
     * Constructs an instance of the ARC broadcaster.
     *
     * @param {string} URL - The URL endpoint for the ARC API.
     * @param {ArcConfig} config - Configuration options for the ARC broadcaster.
     */
    constructor(URL: string, config: ArcConfig) {
        this.URL = URL
        const { apiKey, deploymentId, httpClient, callbackToken, callbackUrl, headers } = config
        this.apiKey = apiKey
        this.httpClient = httpClient ?? defaultHttpClient()
        this.deploymentId = deploymentId || `WalletServices-${randomBytesHex(16)}`
        this.callbackToken = callbackToken
        this.callbackUrl = callbackUrl
        this.headers = headers
    }
 
    /**
     * Unfortunately this seems to only work for recently submitted txids...
     * @param txid 
     * @returns 
     */
    async getTxStatus(txid: string): Promise<ArcMinerGetTxData> {
 
        const requestOptions: HttpClientRequestOptions = {
            method: 'GET',
            headers: this.requestHeaders(),
        }
 
        const response = await this.httpClient.request<ArcMinerGetTxData>(`${this.URL}/v1/tx/${txid}`, requestOptions)
 
        return response.data
    }
 
    requestHeaders() {
        const headers: Record<string, string> = { 'Content-Type': 'application/json', 'XDeployment-ID': this.deploymentId }
        Iif (this.apiKey) { headers.Authorization = `Bearer ${this.apiKey}` }
        Iif (this.callbackUrl) { headers['X-CallbackUrl'] = this.callbackUrl }
        Iif (this.callbackToken) { headers['X-CallbackToken'] = this.callbackToken }
        Iif (this.headers) { for (const key in this.headers) { headers[key] = this.headers[key] } }
        return headers
    }
 
}
 
export async function getMerklePathFromTaalARC(txid: string, config: ArcServiceConfig, services: sdk.WalletServices): Promise<sdk.GetMerklePathResult>
{
    const r: sdk.GetMerklePathResult = { name: config.name }
 
    try {
        const arc = new ArcServices(config.url, config.arcConfig)
 
        const rr = await arc.getTxStatus(txid)
 
        Iif (rr.status === 200 && rr.merklePath) {
            const mp = MerklePath.fromHex(rr.merklePath)
            r.merklePath = mp
            r.header = await services.hashToHeader(rr.blockHash)
        }
 
    } catch (eu: unknown) {
        r.error = sdk.WalletError.fromUnknown(eu)
    }
    return r
}
 
/**
 * 
 * @param txs All transactions must have source transactions. Will just source locking scripts and satoshis do?? toHexEF() is used.
 * @param config 
 */
export async function postTxsToTaalArcMiner(beef: Beef, txids: string[], config: ArcServiceConfig, services: sdk.WalletServices): Promise<sdk.PostTxsResult> {
    const r: sdk.PostTxsResult = { name: config.name, status: 'error', txidResults: [] }
 
    try {
        const arc = new ARC(config.url, config.arcConfig)
 
        /**
         * This service requires an array of EF serialized transactions:
         * Pull the transactions matching txids array out of the Beef and fill in input sourceTransations,
         * either from Beef or by external service lookup.
         */
        const txs: BsvTransaction[] = []
        for (const txid of txids) {
            const btx = beef.findTxid(txid)
            if (btx) {
                const tx = btx.tx
                for (const input of tx.inputs) {
                    Iif (!input.sourceTXID || input.sourceTXID === '0'.repeat(64)) continue; // all zero txid is a coinbase input.
                    let itx = beef.findTxid(input.sourceTXID!)
                    if (!itx) {
                        const rawTx = await services.getRawTx(input.sourceTXID!)
                        Iif (!rawTx || !rawTx.rawTx)
                            throw new sdk.WERR_INVALID_PARAMETER('sourceTXID', `contained in beef or found on chain. ${input.sourceTXID}`);
                        beef.mergeRawTx(rawTx.rawTx!)
                        itx = beef.findTxid(input.sourceTXID!)
                    }
                    input.sourceTransaction = itx!.tx
                }
                txs.push(tx)
            } else
                Ethrow new sdk.WERR_INVALID_PARAMETER('beef', `merged with rawTxs matching txids. Missing ${txid}`)
        }
 
        const rrs = await arc.broadcastMany(txs) as ArcMinerPostTxsData[]
 
        r.status = 'success'
        for (const txid of txids) {
            const txr: PostTxResultForTxid = { txid, status: 'success' }
            const rr = rrs.find(r => r.txid === txid)
            Iif (!rr) {
                r.status = txr.status = 'error'
            } else {
                txr.data = rr
                Iif (rr.status !== 200)
                    r.status = txr.status = 'error';
                else {
                    txr.blockHash = rr.blockHash
                    txr.blockHeight = rr.blockHeight
                    txr.merklePath = !rr.merklePath ? undefined : MerklePath.fromHex(rr.merklePath)
                }
            }
            r.txidResults.push(txr)
        }
        r.data = rrs
 
    } catch (eu: unknown) {
        r.error = sdk.WalletError.fromUnknown(eu)
    }
    return r
}
 
export interface ArcMinerGetTxData {
    status: number // 200
    title: string // OK
    blockHash: string
    blockHeight: number
    competingTxs: null | string[]
    extraInfo: string
    merklePath: string
    timestamp: string // ISO Z
    txid: string
    txStatus: string // 'SEEN_IN_ORPHAN_MEMPOOL'
}
 
export interface ArcMinerPostTxsData {
    status: number // 200
    title: string // OK
    blockHash: string
    blockHeight: number
    competingTxs: null | string[]
    extraInfo: string
    merklePath: string
    timestamp: string // ISO Z
    txid: string
    txStatus: string // 'SEEN_IN_ORPHAN_MEMPOOL'
}
 
export interface ArcMinerPostBeefDataApi {
    status: number, // 200
    title: string, // "OK",
    blockHash?: string, // ""
    blockHeight?: number, // 0
    competingTxs?: null,
    extraInfo: string, // ""
    merklePath?: string, // ""
    timestamp?: string, // "2024-08-23T12:55:26.229904Z",
    txid?: string, // "272b5cdca9a0aa51846df9be29ee366ff85902691d38210e8c4be2fead3823a5",
    txStatus?: string, // "SEEN_ON_NETWORK",
 
 
    type?: string, // url
    detail?: string,
    instance?: string,
}
 
export async function postBeefToTaalArcMiner(
    beef: Beef,
    txids: string[],
    config: ArcServiceConfig,
    services: sdk.WalletServices
)
: Promise<sdk.PostBeefResult>
{
    const r1 = await postBeefToArcMiner(beef, txids, config)
    return r1
 
    Iif (r1.status === 'success') return r1
 
    const datas: object = { r1: r1.data }
 
    const r2 = await services.postTxs(beef, txids)
    
 
    const r3: sdk.PostBeefResult = {
        name: config.name,
        status: 'success',
        data: {},
        txidResults: []
    }
 
    for (const txid of txids) {
        const rawTx = beef.findTxid(txid)!.rawTx!
        const rt = await postBeefToArcMiner(rawTx, [txid], config)
        Iif (rt.status === 'error') r3.status = 'error'
        r3.data![txid] = rt.data
        r3.txidResults.push(rt.txidResults[0])
    }
    datas['r3'] = r3.data
    r3.data = datas
    return r3
}
 
export async function postBeefToArcMiner(
    beef: Beef | number[],
    txids: string[],
    config: ArcServiceConfig
)
: Promise<sdk.PostBeefResult>
{
    const m = {...config}
 
    let url = ''
 
    let r: sdk.PostBeefResult | undefined = undefined
 
    // HACK to resolve ARC error when row has zero leaves.
    // beef.addComputedLeaves()
 
    let beefBinary: number[]
 
    if (Array.isArray(beef))
        beefBinary = beef
    else {
        // HACK to resolve ARC not handling V2 Beef after change Beef class to always serialize as V2 if default constructed:
        beef.version = BEEF_V1
        beefBinary = beef.toBinary()
    }
 
    try {
        const length = beefBinary.length
 
        const makeRequestHeaders = () => {
            const headers: Record<string, string> = {
                'Content-Type': 'application/octet-stream',
                'Content-Length': length.toString(),
                'XDeployment-ID': m.arcConfig.deploymentId || `WalletServices-${randomBytesHex(16)}`,
            }
 
            Iif (m.arcConfig.apiKey) {
                headers['Authorization'] = `Bearer ${m.arcConfig.apiKey}`
            }
 
            return headers
        }
 
        const headers = makeRequestHeaders()
 
        const stream = new Readable({
            read() {
                this.push(Buffer.from(beefBinary as number[]))
                this.push(null)
            }
        })
 
        url = `${config.url}/v1/tx`
 
        const data = await axios.post(
            url,
            stream,
            {
                headers,
                maxBodyLength: Infinity,
                validateStatus: () => true,
            }
        )
        
        Iif (!data || !data.data)
            throw new sdk.WERR_BAD_REQUEST('no response data')
        Iif (data.data === 'No Authorization' || data.status === 403 || data.statusText === 'Forbiden')
            throw new sdk.WERR_BAD_REQUEST('No Authorization')
        Iif (typeof data.data !== 'object')
            throw new sdk.WERR_BAD_REQUEST('no response data object')
 
        const dd = data.data as ArcMinerPostBeefDataApi
 
        r = makePostBeefResult(dd, config, beefBinary, txids)
 
    } catch (err: unknown) {
        console.error(err)
        const error = new sdk.WERR_INTERNAL(`service: ${url}, error: ${JSON.stringify(sdk.WalletError.fromUnknown(err))}`)
        r = makeErrorResult(error, config, beefBinary, txids)
    }
 
    return r
}
 
export function makePostBeefResult(dd: ArcMinerPostBeefDataApi, miner: ArcServiceConfig, beef: number[], txids: string[]) : sdk.PostBeefResult {
    let r: sdk.PostBeefResult
    switch (dd.status) {
        case 200: // Success
            r = makeSuccessResult(dd, miner, beef, txids)
            break
        case 400: // Bad Request
            r = makeErrorResult(new sdk.WERR_BAD_REQUEST(), miner, beef, txids, dd)
            break
        case 401: // Security Failed
            r = makeErrorResult(new sdk.WERR_BAD_REQUEST(`Security Failed (401)`), miner, beef, txids, dd)
            break
        case 409: // Generic Error
        case 422: // RFC 7807 Error
        case 460: // Not Extended Format
        case 467: // Mined Ancestor Missing
        case 468: // Invalid BUMPs
            r = makeErrorResult(new sdk.WERR_BAD_REQUEST(`status ${dd.status}, title ${dd.title}`), miner, beef, txids, dd)
            break
        case 461: // Malformed Transaction
        case 463: // Malformed Transaction
        case 464: // Invalid Outputs
            r = makeErrorResult(new sdk.WERR_BAD_REQUEST(`status ${dd.status}, title ${dd.title}`), miner, beef, txids, dd)
            break
        case 462: // Invalid Inputs
            if (dd.txid)
                r = makeErrorResult(new sdk.WERR_BAD_REQUEST(`status ${dd.status}, title ${dd.title}`), miner, beef, txids, dd)
            else
                r = makeErrorResult(new sdk.WERR_BAD_REQUEST(`status ${dd.status}, title ${dd.title}`), miner, beef, txids, dd)
            break
        case 465: // Fee Too Low
        case 473: // Cumulative Fee Validation Failed
            r = makeErrorResult(new sdk.WERR_BAD_REQUEST(`status ${dd.status}, title ${dd.title}`), miner, beef, txids, dd)
            break
        case 469: // Invalid Merkle Root
            r = makeErrorResult(new sdk.WERR_BAD_REQUEST(`status ${dd.status}, title ${dd.title}`), miner, beef, txids, dd)
            break
        default:
            r = makeErrorResult(new sdk.WERR_BAD_REQUEST(`status ${dd.status}, title ${dd.title}`), miner, beef, txids, dd)
            break
    }
    return r
}
 
function makeSuccessResult(
    dd: ArcMinerPostBeefDataApi,
    miner: ArcServiceConfig,
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    beef: number[],
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    txids: string[]
): sdk.PostBeefResult {
    const r: sdk.PostBeefResult = {
        status: 'success',
        name: miner.name,
        data: dd,
        txidResults: []
    }
    for (let i = 0; i < txids.length; i++) {
        const rt: sdk.PostTxResultForTxid = {
            txid: txids[i],
            status: 'success'
        }
        Iif (dd.txid === txids[i]) {
            rt.alreadyKnown = !!dd.txStatus && ['SEEN_ON_NETWORK', 'MINED'].indexOf(dd.txStatus) >= 0
            rt.txid = dd.txid
            rt.blockHash = dd.blockHash
            rt.blockHeight = dd.blockHeight
            rt.merklePath = MerklePath.fromBinary(asArray(dd.merklePath!))
        }
        r.txidResults.push(rt)
    }
    return r
}
 
export function makeErrorResult(
    error: sdk.WalletError,
    miner: ArcServiceConfig,
    // eslint-disable-next-line @typescript-eslint/no-unused-vars
    beef: number[],
    txids: string[],
    dd?: ArcMinerPostBeefDataApi
): sdk.PostBeefResult {
    const r: sdk.PostBeefResult = {
        status: 'error',
        name: miner.name,
        error,
        data: dd,
        txidResults: []
    }
    for (let i = 0; i < txids.length; i++) {
        const rt: sdk.PostTxResultForTxid = {
            txid: txids[i],
            status: 'error'
        }
        Iif (dd?.txid === txids[i]) {
            rt.alreadyKnown = !!dd.txStatus && ['SEEN_ON_NETWORK', 'MINED'].indexOf(dd.txStatus) >= 0
            rt.txid = dd.txid
            rt.blockHash = dd.blockHash
            rt.blockHeight = dd.blockHeight
            rt.merklePath = MerklePath.fromBinary(asArray(dd.merklePath!))
        }
        r.txidResults.push(rt)
    }
    return r
}