Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

index.js 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. Object.defineProperty(exports, "__esModule", { value: true });
  26. exports.decodeSeed = exports.deriveNodeAddress = exports.deriveAddress = exports.verify = exports.sign = exports.deriveKeypair = exports.generateSeed = void 0;
  27. const assert = __importStar(require("assert"));
  28. const brorand = require("brorand");
  29. const hashjs = __importStar(require("hash.js"));
  30. const elliptic = __importStar(require("elliptic"));
  31. const addressCodec = __importStar(require("ripple-address-codec"));
  32. const secp256k1_1 = require("./secp256k1");
  33. const utils = __importStar(require("./utils"));
  34. const Ed25519 = elliptic.eddsa('ed25519');
  35. const Secp256k1 = elliptic.ec('secp256k1');
  36. const { hexToBytes } = utils;
  37. const { bytesToHex } = utils;
  38. function generateSeed(options = {}) {
  39. assert.ok(!options.entropy || options.entropy.length >= 16, 'entropy too short');
  40. const entropy = options.entropy ? options.entropy.slice(0, 16) : brorand(16);
  41. const type = options.algorithm === 'ed25519' ? 'ed25519' : 'secp256k1';
  42. return addressCodec.encodeSeed(Buffer.from(entropy), type);
  43. }
  44. exports.generateSeed = generateSeed;
  45. function hash(message) {
  46. return hashjs.sha512().update(message).digest().slice(0, 32);
  47. }
  48. const secp256k1 = {
  49. deriveKeypair(entropy, options) {
  50. const prefix = '00';
  51. const privateKey = prefix + (0, secp256k1_1.derivePrivateKey)(entropy, options).toString(16, 64).toUpperCase();
  52. const publicKey = bytesToHex(Secp256k1.keyFromPrivate(privateKey.slice(2))
  53. .getPublic()
  54. .encodeCompressed());
  55. return { privateKey, publicKey };
  56. },
  57. sign(message, privateKey) {
  58. return bytesToHex(Secp256k1.sign(hash(message), hexToBytes(privateKey), {
  59. canonical: true,
  60. }).toDER());
  61. },
  62. verify(message, signature, publicKey) {
  63. return Secp256k1.verify(hash(message), signature, hexToBytes(publicKey));
  64. },
  65. };
  66. const ed25519 = {
  67. deriveKeypair(entropy) {
  68. const prefix = 'ED';
  69. const rawPrivateKey = hash(entropy);
  70. const privateKey = prefix + bytesToHex(rawPrivateKey);
  71. const publicKey = prefix + bytesToHex(Ed25519.keyFromSecret(rawPrivateKey).pubBytes());
  72. return { privateKey, publicKey };
  73. },
  74. sign(message, privateKey) {
  75. // caution: Ed25519.sign interprets all strings as hex, stripping
  76. // any non-hex characters without warning
  77. assert.ok(Array.isArray(message), 'message must be array of octets');
  78. return bytesToHex(Ed25519.sign(message, hexToBytes(privateKey).slice(1)).toBytes());
  79. },
  80. verify(message, signature, publicKey) {
  81. return Ed25519.verify(message, hexToBytes(signature), hexToBytes(publicKey).slice(1));
  82. },
  83. };
  84. function select(algorithm) {
  85. const methods = { 'ecdsa-secp256k1': secp256k1, ed25519 };
  86. return methods[algorithm];
  87. }
  88. function deriveKeypair(seed, options) {
  89. const decoded = addressCodec.decodeSeed(seed);
  90. const algorithm = decoded.type === 'ed25519' ? 'ed25519' : 'ecdsa-secp256k1';
  91. const method = select(algorithm);
  92. const keypair = method.deriveKeypair(decoded.bytes, options);
  93. const messageToVerify = hash('This test message should verify.');
  94. const signature = method.sign(messageToVerify, keypair.privateKey);
  95. /* istanbul ignore if */
  96. if (method.verify(messageToVerify, signature, keypair.publicKey) !== true) {
  97. throw new Error('derived keypair did not generate verifiable signature');
  98. }
  99. return keypair;
  100. }
  101. exports.deriveKeypair = deriveKeypair;
  102. function getAlgorithmFromKey(key) {
  103. const bytes = hexToBytes(key);
  104. return bytes.length === 33 && bytes[0] === 0xed
  105. ? 'ed25519'
  106. : 'ecdsa-secp256k1';
  107. }
  108. function sign(messageHex, privateKey) {
  109. const algorithm = getAlgorithmFromKey(privateKey);
  110. return select(algorithm).sign(hexToBytes(messageHex), privateKey);
  111. }
  112. exports.sign = sign;
  113. function verify(messageHex, signature, publicKey) {
  114. const algorithm = getAlgorithmFromKey(publicKey);
  115. return select(algorithm).verify(hexToBytes(messageHex), signature, publicKey);
  116. }
  117. exports.verify = verify;
  118. function deriveAddressFromBytes(publicKeyBytes) {
  119. return addressCodec.encodeAccountID(utils.computePublicKeyHash(publicKeyBytes));
  120. }
  121. function deriveAddress(publicKey) {
  122. return deriveAddressFromBytes(Buffer.from(hexToBytes(publicKey)));
  123. }
  124. exports.deriveAddress = deriveAddress;
  125. function deriveNodeAddress(publicKey) {
  126. const generatorBytes = addressCodec.decodeNodePublic(publicKey);
  127. const accountPublicBytes = (0, secp256k1_1.accountPublicFromPublicGenerator)(generatorBytes);
  128. return deriveAddressFromBytes(accountPublicBytes);
  129. }
  130. exports.deriveNodeAddress = deriveNodeAddress;
  131. const { decodeSeed } = addressCodec;
  132. exports.decodeSeed = decodeSeed;
  133. //# sourceMappingURL=index.js.map