All files / src networks.ts

100% Statements 16/16
100% Branches 6/6
100% Functions 1/1
100% Lines 12/12

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                                3x                                                                 3x                                                                                                                                                                                                                                         30x 30x   3x 11x   11x 10x     10x   10x 1x     9x          
import { Network } from "@bithighlander/bitcoin-cash-js-lib";
 
type BIP32 = {
  bip32: {
    public: number;
    private: number;
  };
};
 
type BIP32ByScriptType = {
  p2sh: BIP32;
  p2pkh: BIP32;
  "p2sh-p2wpkh"?: BIP32;
  p2wpkh?: BIP32;
};
 
const bip32BTC: BIP32ByScriptType = {
  p2sh: {
    bip32: {
      public: 0x0488b21e,
      private: 0x0488ade4,
    },
  },
  p2pkh: {
    bip32: {
      public: 0x0488b21e,
      private: 0x0488ade4,
    },
  },
  "p2sh-p2wpkh": {
    bip32: {
      public: 0x049d7cb2,
      private: 0x049d7878,
    },
  },
  p2wpkh: {
    bip32: {
      public: 0x04b24746,
      private: 0x04b2430c,
    },
  },
};
 
type NetworkDescription = {
  base: Omit<Network, "bip32">;
} & BIP32ByScriptType;
 
type Networks = Record<string, NetworkDescription>;
 
const networks: Networks = {
  bitcoin: {
    base: {
      messagePrefix: "\x18Bitcoin Signed Message:\n",
      bech32: "bc",
      pubKeyHash: 0x00,
      scriptHash: 0x05,
      wif: 0x80,
    },
    ...bip32BTC,
  },
  dash: {
    base: {
      messagePrefix: "unused",
      bech32: "",
      pubKeyHash: 0x4c,
      scriptHash: 0x10,
      wif: 0xcc,
    },
    p2sh: bip32BTC.p2sh,
    p2pkh: bip32BTC.p2pkh,
  },
  digibyte: {
    base: {
      messagePrefix: "\x19Digibyte Signed Message:\n",
      bech32: "dgb",
      pubKeyHash: 0x1e,
      scriptHash: 0x3f,
      wif: 0x80,
    },
    ...bip32BTC,
  },
  dogecoin: {
    base: {
      messagePrefix: "\x19Dogecoin Signed Message:\n",
      bech32: "",
      pubKeyHash: 0x1e,
      scriptHash: 0x16,
      wif: 0x9e,
    },
    p2sh: {
      bip32: {
        public: 0x02facafd,
        private: 0x02fac398,
      },
    },
    p2pkh: {
      bip32: {
        public: 0x02facafd,
        private: 0x02fac398,
      },
    },
  },
  litecoin: {
    base: {
      messagePrefix: "\x19Litecoin Signed Message:\n",
      bech32: "ltc",
      pubKeyHash: 0x30,
      scriptHash: 0x32,
      wif: 0xb0,
    },
    p2sh: {
      bip32: {
        public: 0x019da462,
        private: 0x019d9cfe,
      },
    },
    p2pkh: {
      bip32: {
        public: 0x019da462,
        private: 0x019d9cfe,
      },
    },
    "p2sh-p2wpkh": {
      bip32: {
        public: 0x01b26ef6,
        private: 0x01b26792,
      },
    },
    p2wpkh: bip32BTC.p2wpkh,
  },
  testnet: {
    base: {
      messagePrefix: "\x18Bitcoin Signed Message:\n",
      bech32: "tb",
      pubKeyHash: 0x6f,
      scriptHash: 0xc4,
      wif: 0xef,
    },
    p2sh: {
      bip32: {
        public: 0x043587cf,
        private: 0x04358394,
      },
    },
    p2pkh: {
      bip32: {
        public: 0x043587cf,
        private: 0x04358394,
      },
    },
    "p2sh-p2wpkh": {
      bip32: {
        public: 0x044a5262,
        private: 0x044a4e28,
      },
    },
    p2wpkh: {
      bip32: {
        public: 0x045f1cf6,
        private: 0x045f18bc,
      },
    },
  },
};
 
//TODO: all below are missing network data
for (const coin of ["bitcoincash", "thorchain", "secret", "terra", "kava", "cardano", "cosmos","osmosis", "binance", "ethereum"])
  networks[coin] = networks.bitcoin;
 
export function getNetwork(coin: string, scriptType?: string): Network {
  coin = coin.toLowerCase();
 
  if (!(coin in networks)) throw new Error(`${coin} network not supported`);
  let network = networks[coin];
 
  // @ts-ignore
  const bip32 = network[scriptType || "p2sh"];
 
  if (!bip32) {
    throw new Error(`${scriptType} not supported for ${coin} network`);
  }
 
  return {
    ...network.base,
    ...bip32,
  };
}