Search code examples
angulartypescripttypescript-typingsbitcoinlib

Can't resolve all parameters for ECPair: (?, ?, ?)


I try to implement bitcoinjs-lib in a new angular 4 app What i did :

npm install bitcoinjs-lib --save
npm install @types/bitcoinjs-lib --save

My app.component.ts :

import { Inject } from '@angular/core';
import { Component } from '@angular/core';
import { HDNode, Transaction, ECPair } from 'bitcoinjs-lib'

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';

  constructor(private ecPair: ECPair){
    console.log(this.ecPair.getAddress())
  }
}

Compiled successfully but i get in my browser :

Uncaught Error: Can't resolve all parameters for ECPair: (?, ?, ?).
    at syntaxError (compiler.js:466)

Here is ECPair in the node_modules :

export class ECPair {
    constructor(d: BigInteger, Q?: null, options?: { compressed?: boolean, network?: Network });

    constructor(d: null | undefined, Q: any, options?: { compressed?: boolean, network?: Network }); // Q should be ECPoint, but not sure how to define such type

    d: BigInteger;

    getAddress(): string;
...
}

I understand that he doesn't know how to instantiate it because of the differents parameters type, how can i fix it ? I tried with @Inject but can't resolved too.

Thanks


Solution

  • You have to provide it properly. In your @NgModule you should use something like this:

    @NgModule({
      ...
      providers: [
        ...
        { ECPair, useFactory:()=>new ECPair(d,Q,options) }
      ]
    })
    

    Specify appropriate arguments for d Q and options.