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 | 1x 1x 1x 1x 1x 1x 1x 2x 2x 330x 330x 7x 323x 323x 9x 8x 5x 4x 4x 1x 3x 3x 20x 4x 4x 310x 60x 4x 4x 6x | import * as crypto from "@node-lightning/crypto"; import { PrivateKey } from "./PrivateKey"; import { Script } from "./Script"; import { Address } from "./Address"; import { BitcoinError } from "./BitcoinError"; import { BitcoinErrorCode } from "./BitcoinErrorCode"; import { Network } from "./Network"; /** * This class represents a point on an secp256k1 elliptic curve and acts * as a public key along with the network for which the key belongs. The * public key can be serialized into a buffer using the SEC compressed * and uncompressed format. */ export class PublicKey { /** * Returns the public key associated with the WIF input * @param input WIF encoded private key * @returns public key assocated with the WIF encoding */ public static fromWif(input: string): PublicKey { const [, pubkey] = PrivateKey.fromWif(input); return pubkey; } public readonly compressed: boolean; private readonly _buffer: Buffer; /** * Constructs a new instance of a PublicKey and requires a valid key * or an exception will be throw. * @param buffer A valid 33-byte or 65-byte SEC encoded public key * @param network The corresponding network where the public key belongs */ constructor(buffer: Buffer, readonly network: Network) { if (!crypto.validPublicKey(buffer)) { throw new BitcoinError(BitcoinErrorCode.PubKeyInvalid, { key: buffer }); } this.compressed = buffer.length === 33; this._buffer = buffer; } /** * Tweaks a public key by adding tweak * G to the point. The equation is * T = P + t*G * * @param tweak 32-byte scalar value that is multiplied by G * @returns a new instance of PublicKey containing the tweaked value */ public tweakAdd(tweak: Buffer) { const result = crypto.publicKeyTweakAdd(this._buffer, tweak, this.compressed); return new PublicKey(result, this.network); } /** * Tweaks a public key by multiplying it against a scalar. The equation is * T = P * t * * @param tweak 32-byte tweak to multiply against the public key * @returns a new instance of PublicKey containing the tweaked value */ public tweakMul(tweak: Buffer) { const result = crypto.publicKeyTweakMul(this._buffer, tweak, true); return new PublicKey(result, this.network); } /** * Adds two public key points together. * @param other The other public key that should be added * @returns a new instance of PublicKey containing the added points */ public add(other: PublicKey): PublicKey { if (this.network !== other.network) { throw new BitcoinError(BitcoinErrorCode.NetworkMismatch, { me: this, other }); } const result = crypto.publicKeyCombine( [this.toBuffer(), other.toBuffer()], this.compressed, ); return new PublicKey(result, this.network); } /** * Returns the hash160 of the public key * @returns 20-byte hash160 of the public key */ public hash160(): Buffer { return crypto.hash160(this.toBuffer()); } /** * Convert the public key to compressed or uncompressed. * @param compressed */ public toPubKey(compressed: boolean): PublicKey { const buffer = crypto.convertPublicKey(this._buffer, compressed); return new PublicKey(buffer, this.network); } /** * Serializes the PublicKey instance into a 33-byte or 65-byte SEC * encoded buffer depending on whether the public key is compressed * or uncompressed. * @returns 33-byte or 65-byte buffer */ public toBuffer(): Buffer { return Buffer.from(this._buffer); } /** * Serializes the PublicKey instance nito a 33-byte or 65-byte SEC * encoded hex value. * @returns */ public toHex(): string { return this.toBuffer().toString("hex"); } /** * Returns a P2PKH for the public key. * @returns Base58 encoded Bitcoin address */ public toP2pkhAddress(): string { return Address.encodeBase58(this.network.p2pkhPrefix, this.hash160()); } /** * Returns a nested segwit addrress (P2SH-P2WPKH). * @returns Base58 encoded Bitcoin address */ public toP2nwpkhAddress(): string { return Script.p2wpkhLock(this.hash160()).toP2shAddress(this.network); } /** * Returns a P2WPKH address for the public key. * @returns bech32 encoded segwit address */ public toP2wpkhAddress(): string { return Address.encodeBech32(this.network.p2wpkhPrefix, 0, this.hash160()); } } |