All files factory.ts

87.64% Statements 78/89
66.67% Branches 42/63
84% Functions 21/25
87.18% Lines 68/78

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 2361x 1x   1x 62x 62x       62x   62x           2x     6x           2x                 4x           6x 6x 6x     6x                 6x             4x 4x 4x 4x             18x 18x 18x 2x 2x   2x             2x         16x         20x       18x 18x   16x   2x                       161x 161x 33x   33x   31x 2x     2x         128x 128x                 18x 2x 2x               20x 4x               4x 4x       18x 2x                     30x 14x                 30x 24x 8x   8x                   24x 8x 8x                   28x 22x 6x   6x                   21x 5x 5x         1x  
import { funcGetChain } from "@swtc/common"
import { Factory as KeypairFactory } from "@swtc/keypairs"
 
const Factory = (token_or_chain = "jingtum") => {
  const chain = funcGetChain(token_or_chain)
  Iif (!chain) {
    throw Error("token or chain not supported")
  }
 
  const KeyPair = KeypairFactory(chain.code)
 
  return class Wallet {
    public static token = chain.currency.toUpperCase()
    public static chain = chain.code.toLowerCase()
    public static KeyPair = KeyPair
    public static config = chain
    public static getCurrency() {
      return Wallet.token || "SWT"
    }
    public static getCurrencies() {
      return Wallet.config.CURRENCIES || {}
    }
    public static getChain() {
      return Wallet.chain || "jingtum"
    }
    public static getFee() {
      return Wallet.config.fee || 10000
    }
    public static getAccountZero() {
      return Wallet.config.ACCOUNT_ZERO || "jjjjjjjjjjjjjjjjjjjjjhoLvTp"
    }
    public static getAccountOne() {
      return Wallet.config.ACCOUNT_ONE || "jjjjjjjjjjjjjjjjjjjjBZbvri"
    }
    public static getIssuer() {
      return Wallet.config.issuer || "jGa9J9TkqtBcUoHe2zqhVFFbgUVED6o9or"
    }
    public static makeCurrency(
      currency = Wallet.token,
      issuer = Wallet.getIssuer()
    ) {
      const CURRENCIES = Wallet.getCurrencies()
      currency = currency.toUpperCase()
      currency = CURRENCIES.hasOwnProperty(currency)
        ? CURRENCIES[currency]
        : currency
      return currency === Wallet.token
        ? { currency, issuer: "" }
        : { currency, issuer }
    }
    public static makeAmount(
      value = 1,
      currency = Wallet.token,
      issuer = Wallet.getIssuer()
    ) {
      return typeof currency === "object"
        ? Object.assign({}, currency, { value: Number(value) })
        : Object.assign({}, this.makeCurrency(currency, issuer), {
            value: Number(value)
          })
    }
    public static generate(options: any = {}) {
      const secret = KeyPair.generateSeed(options)
      const keypair = KeyPair.deriveKeyPair(secret)
      const address = KeyPair.deriveAddress(keypair.publicKey)
      return {
        secret,
        address
      }
    }
 
    public static fromSecret(secret_or_private_key, algorithm = "sec256k1") {
      try {
        let secret = secret_or_private_key
        const keypair = KeyPair.deriveKeyPair(secret_or_private_key, algorithm)
        const address = KeyPair.deriveAddress(keypair.publicKey)
        Eif (/^s/.test(secret_or_private_key)) {
          // secret starts with s
          secret = secret_or_private_key
        } else if (secret_or_private_key.length >= 64) {
          // private key for hdwallet
          secret = "privatekey"
        } else {
          throw new Error("use secret or private key to get wallet")
        }
        return {
          secret,
          address
        }
      } catch (err) {
        return null
      }
    }
 
    public static isValidAddress(address) {
      return KeyPair.isValidAddress(address)
    }
 
    public static isValidSecret(secret) {
      try {
        KeyPair.deriveKeyPair(secret)
      } catch (err) {
        return false
      }
      return true
    }
 
    public static checkTx(message, signature, publicKey) {
      return KeyPair.verifyTx(message, signature, publicKey)
      // return ec.verify(message, signature, hexToBytes(publicKey))
    }
 
    public _keypairs
    public _secret
    constructor(secret_or_private_key: any, algorithm = "sec256k1") {
      // extend for hdwallet, take secret or privatekey, plugs algorithm for raw privateKey
      try {
        this._keypairs = KeyPair.deriveKeyPair(secret_or_private_key, algorithm)
        Iif (typeof secret_or_private_key !== "string") {
          throw new Error("use secret or private key to instantiate wallet")
        } else if (/^s/.test(secret_or_private_key)) {
          // secret starts with s
          this._secret = secret_or_private_key
        } else Eif (secret_or_private_key.length >= 64) {
          // private key for hdwallet
          // this._secret = secret_or_private_key
          this._secret = "privatekey"
        } else {
          throw new Error("use secret or private key to instantiate wallet")
        }
      } catch (err) {
        this._keypairs = null
        this._secret = null
      }
    }
 
    /**
     * get wallet address
     * @returns {*}
     */
    public address() {
      if (!this._keypairs) return null
      const address = KeyPair.deriveAddress(this._keypairs.publicKey)
      return address
    }
 
    /**
     * get wallet secret
     * @returns {*}
     */
    public secret() {
      if (!this._keypairs) return null
      return this._secret
    }
 
    /*
     * Get the public key from key pair
     * used for local signing operation.
     */
    public isEd25519() {
      Iif (!this._keypairs) return false
      return this._keypairs.privateKey.slice(0, 2).toUpperCase() === "ED"
    }
 
    public toJson() {
      if (!this._keypairs) return null
      return {
        secret: this.secret(),
        address: this.address()
      }
    }
 
    /*
     * Get the public key from key pair
     * used for local signing operation.
     */
    public getPublicKey() {
      if (!this._keypairs) return null
      return this._keypairs.publicKey
    }
 
    /**
     * sign message with wallet privatekey
     * @param message
     * @returns {*}
     */
    public sign(message) {
      if (!message) return null
      if (!this._keypairs) return null
      const privateKey = this._keypairs.privateKey
      // Export DER encoded signature in Array
      return KeyPair.sign(message, privateKey)
    }
 
    /**
     * verify signature with wallet publickey
     * @param message
     * @param signature
     * @returns {*}
     */
    public verify(message, signature) {
      if (!this._keypairs) return null
      const publicKey = this.getPublicKey()
      return KeyPair.verify(message, signature, publicKey)
    }
 
    /**
     * sign message with wallet privatekey
     * Export DER encoded signature in Array
     * @param message
     * @returns {*}
     */
    public signTx(message) {
      if (!message) return null
      if (!this._keypairs) return null
      const privateKey = this._keypairs.privateKey
      // Export DER encoded signature in Array
      return KeyPair.signTx(message, privateKey)
    }
 
    /**
     * verify signature with wallet publickey
     * @param message
     * @param signature
     * @returns {*}
     */
    public verifyTx(message, signature) {
      if (!this._keypairs) return null
      const publicKey = this.getPublicKey()
      return KeyPair.verifyTx(message, signature, publicKey)
    }
  }
}
 
export { Factory }