signing-key.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. "use strict";
  2. /**
  3. * Add details about signing here.
  4. *
  5. * @_subsection: api/crypto:Signing [about-signing]
  6. */
  7. Object.defineProperty(exports, "__esModule", { value: true });
  8. exports.SigningKey = void 0;
  9. const secp256k1_1 = require("@noble/curves/secp256k1");
  10. const index_js_1 = require("../utils/index.js");
  11. const signature_js_1 = require("./signature.js");
  12. /**
  13. * A **SigningKey** provides high-level access to the elliptic curve
  14. * cryptography (ECC) operations and key management.
  15. */
  16. class SigningKey {
  17. #privateKey;
  18. /**
  19. * Creates a new **SigningKey** for %%privateKey%%.
  20. */
  21. constructor(privateKey) {
  22. (0, index_js_1.assertArgument)((0, index_js_1.dataLength)(privateKey) === 32, "invalid private key", "privateKey", "[REDACTED]");
  23. this.#privateKey = (0, index_js_1.hexlify)(privateKey);
  24. }
  25. /**
  26. * The private key.
  27. */
  28. get privateKey() { return this.#privateKey; }
  29. /**
  30. * The uncompressed public key.
  31. *
  32. * This will always begin with the prefix ``0x04`` and be 132
  33. * characters long (the ``0x`` prefix and 130 hexadecimal nibbles).
  34. */
  35. get publicKey() { return SigningKey.computePublicKey(this.#privateKey); }
  36. /**
  37. * The compressed public key.
  38. *
  39. * This will always begin with either the prefix ``0x02`` or ``0x03``
  40. * and be 68 characters long (the ``0x`` prefix and 33 hexadecimal
  41. * nibbles)
  42. */
  43. get compressedPublicKey() { return SigningKey.computePublicKey(this.#privateKey, true); }
  44. /**
  45. * Return the signature of the signed %%digest%%.
  46. */
  47. sign(digest) {
  48. (0, index_js_1.assertArgument)((0, index_js_1.dataLength)(digest) === 32, "invalid digest length", "digest", digest);
  49. const sig = secp256k1_1.secp256k1.sign((0, index_js_1.getBytesCopy)(digest), (0, index_js_1.getBytesCopy)(this.#privateKey), {
  50. lowS: true
  51. });
  52. return signature_js_1.Signature.from({
  53. r: (0, index_js_1.toBeHex)(sig.r, 32),
  54. s: (0, index_js_1.toBeHex)(sig.s, 32),
  55. v: (sig.recovery ? 0x1c : 0x1b)
  56. });
  57. }
  58. /**
  59. * Returns the [[link-wiki-ecdh]] shared secret between this
  60. * private key and the %%other%% key.
  61. *
  62. * The %%other%% key may be any type of key, a raw public key,
  63. * a compressed/uncompressed pubic key or aprivate key.
  64. *
  65. * Best practice is usually to use a cryptographic hash on the
  66. * returned value before using it as a symetric secret.
  67. *
  68. * @example:
  69. * sign1 = new SigningKey(id("some-secret-1"))
  70. * sign2 = new SigningKey(id("some-secret-2"))
  71. *
  72. * // Notice that privA.computeSharedSecret(pubB)...
  73. * sign1.computeSharedSecret(sign2.publicKey)
  74. * //_result:
  75. *
  76. * // ...is equal to privB.computeSharedSecret(pubA).
  77. * sign2.computeSharedSecret(sign1.publicKey)
  78. * //_result:
  79. */
  80. computeSharedSecret(other) {
  81. const pubKey = SigningKey.computePublicKey(other);
  82. return (0, index_js_1.hexlify)(secp256k1_1.secp256k1.getSharedSecret((0, index_js_1.getBytesCopy)(this.#privateKey), (0, index_js_1.getBytes)(pubKey), false));
  83. }
  84. /**
  85. * Compute the public key for %%key%%, optionally %%compressed%%.
  86. *
  87. * The %%key%% may be any type of key, a raw public key, a
  88. * compressed/uncompressed public key or private key.
  89. *
  90. * @example:
  91. * sign = new SigningKey(id("some-secret"));
  92. *
  93. * // Compute the uncompressed public key for a private key
  94. * SigningKey.computePublicKey(sign.privateKey)
  95. * //_result:
  96. *
  97. * // Compute the compressed public key for a private key
  98. * SigningKey.computePublicKey(sign.privateKey, true)
  99. * //_result:
  100. *
  101. * // Compute the uncompressed public key
  102. * SigningKey.computePublicKey(sign.publicKey, false);
  103. * //_result:
  104. *
  105. * // Compute the Compressed a public key
  106. * SigningKey.computePublicKey(sign.publicKey, true);
  107. * //_result:
  108. */
  109. static computePublicKey(key, compressed) {
  110. let bytes = (0, index_js_1.getBytes)(key, "key");
  111. // private key
  112. if (bytes.length === 32) {
  113. const pubKey = secp256k1_1.secp256k1.getPublicKey(bytes, !!compressed);
  114. return (0, index_js_1.hexlify)(pubKey);
  115. }
  116. // raw public key; use uncompressed key with 0x04 prefix
  117. if (bytes.length === 64) {
  118. const pub = new Uint8Array(65);
  119. pub[0] = 0x04;
  120. pub.set(bytes, 1);
  121. bytes = pub;
  122. }
  123. const point = secp256k1_1.secp256k1.ProjectivePoint.fromHex(bytes);
  124. return (0, index_js_1.hexlify)(point.toRawBytes(compressed));
  125. }
  126. /**
  127. * Returns the public key for the private key which produced the
  128. * %%signature%% for the given %%digest%%.
  129. *
  130. * @example:
  131. * key = new SigningKey(id("some-secret"))
  132. * digest = id("hello world")
  133. * sig = key.sign(digest)
  134. *
  135. * // Notice the signer public key...
  136. * key.publicKey
  137. * //_result:
  138. *
  139. * // ...is equal to the recovered public key
  140. * SigningKey.recoverPublicKey(digest, sig)
  141. * //_result:
  142. *
  143. */
  144. static recoverPublicKey(digest, signature) {
  145. (0, index_js_1.assertArgument)((0, index_js_1.dataLength)(digest) === 32, "invalid digest length", "digest", digest);
  146. const sig = signature_js_1.Signature.from(signature);
  147. let secpSig = secp256k1_1.secp256k1.Signature.fromCompact((0, index_js_1.getBytesCopy)((0, index_js_1.concat)([sig.r, sig.s])));
  148. secpSig = secpSig.addRecoveryBit(sig.yParity);
  149. const pubKey = secpSig.recoverPublicKey((0, index_js_1.getBytesCopy)(digest));
  150. (0, index_js_1.assertArgument)(pubKey != null, "invalid signautre for digest", "signature", signature);
  151. return "0x" + pubKey.toHex(false);
  152. }
  153. /**
  154. * Returns the point resulting from adding the ellipic curve points
  155. * %%p0%% and %%p1%%.
  156. *
  157. * This is not a common function most developers should require, but
  158. * can be useful for certain privacy-specific techniques.
  159. *
  160. * For example, it is used by [[HDNodeWallet]] to compute child
  161. * addresses from parent public keys and chain codes.
  162. */
  163. static addPoints(p0, p1, compressed) {
  164. const pub0 = secp256k1_1.secp256k1.ProjectivePoint.fromHex(SigningKey.computePublicKey(p0).substring(2));
  165. const pub1 = secp256k1_1.secp256k1.ProjectivePoint.fromHex(SigningKey.computePublicKey(p1).substring(2));
  166. return "0x" + pub0.add(pub1).toHex(!!compressed);
  167. }
  168. }
  169. exports.SigningKey = SigningKey;
  170. //# sourceMappingURL=signing-key.js.map