babylonchain/btc-staking-ts

Ecclib not initalised

Opened this issue · 3 comments

I was getting an "Invalid change address" error when I tried to create a stakingTransaction, after some investigation the issue was the following: 'No ECC Library provided. You must call initEccLib() with a valid TinySecp256k1Interface instance', I think it would be nice to show the underlying issue why the address validation failed.

export const isValidBitcoinAddress = (

I fixed the issue by calling the initEccLib() function, but as far as I understand it shouldn't be called from external sources (I couldn't find a call in the simple-staking repo). I'm using dynamic import for the btc-staking-ts not sure if that's might be the issue why the the Ecclib init wasn't called properly

Our tsconfig:

{
    "compilerOptions": {
        "target": "ESNext",
        "module": "Node16",
        "lib": [
            "dom",
            "dom.iterable",
            "esnext"
        ],
        "moduleResolution": "Node16",
        "sourceMap": true,
        "declaration": true,
        "declarationMap": true,
        "strict": true,
        "noUnusedLocals": true,
        "noImplicitReturns": true,
        "noImplicitOverride": true,
        "esModuleInterop": true,
        "skipLibCheck": true,
        "forceConsistentCasingInFileNames": true,
        "importsNotUsedAsValues": "remove",
        "outDir": "dist",
        "rootDir": "src",
        "composite": true,
        "resolveJsonModule": true
    },
    "include": [
        "src/**/*.ts",
    ],
    "exclude": [
        "*.test.ts",
        "lib",
        "node_modules"
    ]
}

hi @DaBors The initBTCCurve function is called in the simple-staking project: https://github.com/babylonchain/simple-staking/blob/dev/src/app/page.tsx#L201.

I don't think this issue is related to your tsconfig setup. The ECC library simply needs to be initialized for it to work. While it would be ideal not to expose the initialization method publicly, bitcoinjs-lib does not provide a straightforward way to check if ECC has already been initialized.

We might need to introduce a workaround if we still want to initialize the BTC curve within the library. Alternatively, we could refactor the code to make it a class with a constructor. However, for now, the best approach is to call the initialization function at the application layer.

Oh missed that initialisation, my bad. I found this method getEccLib in the bitcoinjs-lib that gets you the ECC if it was initialises and otherwise throws an error and it's exported so it should be possible to use that to check if we have the ECC initialised or not.

In any case while the manual initialisation via initBTCCurve is required I think it would be nice to add it to the readme/docs. Also it might make sense to reraise this error on the isValidBitcoinAddress function instead of returning false as it's not that the address is invalid, but the setup is not properly initialised.

make sense to me. but seems the priority is low atm. we will try pick it up later on