All files / src wallet.ts

100% Statements 15/15
100% Branches 0/0
100% Functions 5/5
100% Lines 14/14
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 461x 1x         1x               608x           1x 608x 608x 608x 608x                   1x 907x           1x 207x   1x  
import {api as sodium} from 'sodium';
import {toSha256} from './utils/sha256';
// tslint:disable-next-line no-var-requires
/**
 * Generic Abstract Wallet.
 */
export abstract class GenericWallet {
  // tslint:disable variable-name
  protected _privKey: string;
  protected _publicKey: string;
  protected _address: string;
  // tslint:enable variable-name
 
  constructor(secret: string) {
    this.createKeyPair(secret);
  }
  /**
   * Creates key pair from secret string
   * @param {string} secret
   */
  protected createKeyPair(secret: string) {
    const hash      = toSha256(secret);
    const keypair   = sodium.crypto_sign_seed_keypair(hash);
    this._privKey   = keypair.secretKey.toString('hex');
    this._publicKey = keypair.publicKey.toString('hex');
  }
 
  abstract get address();
 
  protected abstract deriveAddress();
 
  /**
   * @returns {string} privateKey
   */
  get privKey(): string {
    return this._privKey;
  }
 
  /**
   * @returns {string} publicKey
   */
  get publicKey(): string {
    return this._publicKey;
  }
}