All files Address.ts

100% Statements 41/41
100% Branches 31/31
100% Functions 4/4
100% Lines 40/40

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 1161x 1x 1x 1x 1x 1x         1x                     13x 1x   12x 12x 12x 12x                       5x 4x 4x 4x 1x   3x 5x 2x     1x                     9x 9x                       31x 28x 28x 2x     26x 2x     24x 3x     21x   19x 2x               17x 3x               14x 22x     2x      
import { BufferWriter } from "@node-lightning/bufio";
import { Base58Check } from "./Base58Check";
import { Bech32, Bech32Version } from "./Bech32";
import { BitcoinError } from "./BitcoinError";
import { BitcoinErrorCode } from "./BitcoinErrorCode";
import { Network } from "./Network";
 
/**
 * Address encoding/decoding utility for legacy and segwit addresses.
 */
export class Address {
    /**
     * Encodes a P2PKH or P2SH address using Base58Check in the format
     * - 1-byte: prefix
     * - 20-byte: hash160 of data
     * - 4-byte: checksum from hash256 of data
     * @param prefix - 1-byte prefix from a Network config (either p2)
     * @param hash
     * @returns
     */
    public static encodeBase58(prefix: number, hash: Buffer): string {
        if (hash.length !== 20) {
            throw new BitcoinError(BitcoinErrorCode.Hash160Invalid, { hash });
        }
        const w = new BufferWriter(Buffer.alloc(21));
        w.writeUInt8(prefix);
        w.writeBytes(hash);
        return Base58Check.encode(w.toBuffer());
    }
 
    /**
     * Decodes a Base58Check encoded legacy address and determines the
     * network. This function returns the hash160 and the prefix.
     * @param encoded base58check encoded string
     * @returns
     */
    public static decodeBase58(
        encoded: string,
    ): { network: Network; prefix: number; hash: Buffer } {
        const data = Base58Check.decode(encoded);
        const prefix = data[0];
        const hash = data.slice(1);
        if (hash.length !== 20) {
            throw new BitcoinError(BitcoinErrorCode.Hash160Invalid, { hash });
        }
        for (const network of Network.all) {
            if (network.p2pkhPrefix === prefix || network.p2shPrefix === prefix) {
                return { network, prefix, hash };
            }
        }
        throw new BitcoinError(BitcoinErrorCode.UnknownAddressPrefix, { encoded });
    }
 
    /**
     * Encodes a native segwit P2WPKH or P2WSH address using bech32.
     * @param prefix hrp part of the address
     * @param version segwit version
     * @param program witness program
     * @returns
     */
    public static encodeBech32(prefix: string, version: number, program: Buffer): string {
        const words = [version].concat(Bech32.bufferToWords(program, true));
        return Bech32.encode(prefix, words, Bech32Version.Bech32);
    }
 
    /**
     * Decodes a bech32 address and returns the corresponding network,
     * segwit version, and witness program.
     * @param encoded
     * @returns
     */
    public static decodeBech32(
        encoded: string,
    ): { network: Network; version: number; program: Buffer } {
        const { hrp, words, version: checksum } = Bech32.decode(encoded);
        const version = words[0];
        if (version < 0 || version > 16) {
            throw new BitcoinError(BitcoinErrorCode.InvalidSegwitVersion, { encoded, version });
        }
 
        if (version === 0 && checksum !== Bech32Version.Bech32) {
            throw new BitcoinError(BitcoinErrorCode.InvalidBech32Encoding);
        }
 
        if (version > 0 && checksum !== Bech32Version.Bech32m) {
            throw new BitcoinError(BitcoinErrorCode.InvalidBech32Encoding);
        }
 
        const program = Bech32.wordsToBuffer(words.slice(1), false);
 
        if (version === 0 && program.length !== 20 && program.length !== 32) {
            throw new BitcoinError(BitcoinErrorCode.InvalidWitnessProgram, {
                encoded,
                version,
                program,
                length: program.length,
            });
        }
 
        if (program.length < 2 || program.length > 40) {
            throw new BitcoinError(BitcoinErrorCode.InvalidWitnessProgram, {
                encoded,
                version,
                program,
                length: program.length,
            });
        }
 
        for (const network of Network.all) {
            if (hrp === network.p2wpkhPrefix) return { network, version, program };
        }
 
        throw new BitcoinError(BitcoinErrorCode.UnknownAddressPrefix, { encoded, hrp });
    }
}