UNPKG

3.91 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.decodeSeed = exports.deriveNodeAddress = exports.deriveAddress = exports.verify = exports.sign = exports.deriveKeypair = exports.generateSeed = void 0;
7const ripple_address_codec_1 = require("ripple-address-codec");
8Object.defineProperty(exports, "decodeSeed", { enumerable: true, get: function () { return ripple_address_codec_1.decodeSeed; } });
9const ripemd160_1 = require("@xrplf/isomorphic/ripemd160");
10const sha256_1 = require("@xrplf/isomorphic/sha256");
11const utils_1 = require("@xrplf/isomorphic/utils");
12const utils_2 = require("./signing-schemes/secp256k1/utils");
13const Sha512_1 = __importDefault(require("./utils/Sha512"));
14const assert_1 = __importDefault(require("./utils/assert"));
15const getAlgorithmFromKey_1 = require("./utils/getAlgorithmFromKey");
16const secp256k1_1 = __importDefault(require("./signing-schemes/secp256k1"));
17const ed25519_1 = __importDefault(require("./signing-schemes/ed25519"));
18function getSigningScheme(algorithm) {
19 const schemes = { 'ecdsa-secp256k1': secp256k1_1.default, ed25519: ed25519_1.default };
20 return schemes[algorithm];
21}
22function generateSeed(options = {}) {
23 assert_1.default.ok(!options.entropy || options.entropy.length >= 16, 'entropy too short');
24 const entropy = options.entropy
25 ? options.entropy.slice(0, 16)
26 : (0, utils_1.randomBytes)(16);
27 const type = options.algorithm === 'ed25519' ? 'ed25519' : 'secp256k1';
28 return (0, ripple_address_codec_1.encodeSeed)(entropy, type);
29}
30exports.generateSeed = generateSeed;
31function deriveKeypair(seed, options) {
32 var _a;
33 const decoded = (0, ripple_address_codec_1.decodeSeed)(seed);
34 const proposedAlgorithm = (_a = options === null || options === void 0 ? void 0 : options.algorithm) !== null && _a !== void 0 ? _a : decoded.type;
35 const algorithm = proposedAlgorithm === 'ed25519' ? 'ed25519' : 'ecdsa-secp256k1';
36 const scheme = getSigningScheme(algorithm);
37 const keypair = scheme.deriveKeypair(decoded.bytes, options);
38 const messageToVerify = Sha512_1.default.half('This test message should verify.');
39 const signature = scheme.sign(messageToVerify, keypair.privateKey);
40 /* istanbul ignore if */
41 if (!scheme.verify(messageToVerify, signature, keypair.publicKey)) {
42 throw new Error('derived keypair did not generate verifiable signature');
43 }
44 return keypair;
45}
46exports.deriveKeypair = deriveKeypair;
47function sign(messageHex, privateKey) {
48 const algorithm = (0, getAlgorithmFromKey_1.getAlgorithmFromPrivateKey)(privateKey);
49 return getSigningScheme(algorithm).sign((0, utils_1.hexToBytes)(messageHex), privateKey);
50}
51exports.sign = sign;
52function verify(messageHex, signature, publicKey) {
53 const algorithm = (0, getAlgorithmFromKey_1.getAlgorithmFromPublicKey)(publicKey);
54 return getSigningScheme(algorithm).verify((0, utils_1.hexToBytes)(messageHex), signature, publicKey);
55}
56exports.verify = verify;
57function computePublicKeyHash(publicKeyBytes) {
58 return (0, ripemd160_1.ripemd160)((0, sha256_1.sha256)(publicKeyBytes));
59}
60function deriveAddressFromBytes(publicKeyBytes) {
61 return (0, ripple_address_codec_1.encodeAccountID)(computePublicKeyHash(publicKeyBytes));
62}
63function deriveAddress(publicKey) {
64 return deriveAddressFromBytes((0, utils_1.hexToBytes)(publicKey));
65}
66exports.deriveAddress = deriveAddress;
67function deriveNodeAddress(publicKey) {
68 const generatorBytes = (0, ripple_address_codec_1.decodeNodePublic)(publicKey);
69 const accountPublicBytes = (0, utils_2.accountPublicFromPublicGenerator)(generatorBytes);
70 return deriveAddressFromBytes(accountPublicBytes);
71}
72exports.deriveNodeAddress = deriveNodeAddress;
73//# sourceMappingURL=index.js.map
\No newline at end of file