1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | exports.decodeSeed = exports.deriveNodeAddress = exports.deriveAddress = exports.verify = exports.sign = exports.deriveKeypair = exports.generateSeed = void 0;
|
7 | const ripple_address_codec_1 = require("ripple-address-codec");
|
8 | Object.defineProperty(exports, "decodeSeed", { enumerable: true, get: function () { return ripple_address_codec_1.decodeSeed; } });
|
9 | const ripemd160_1 = require("@xrplf/isomorphic/ripemd160");
|
10 | const sha256_1 = require("@xrplf/isomorphic/sha256");
|
11 | const utils_1 = require("@xrplf/isomorphic/utils");
|
12 | const utils_2 = require("./signing-schemes/secp256k1/utils");
|
13 | const Sha512_1 = __importDefault(require("./utils/Sha512"));
|
14 | const assert_1 = __importDefault(require("./utils/assert"));
|
15 | const getAlgorithmFromKey_1 = require("./utils/getAlgorithmFromKey");
|
16 | const secp256k1_1 = __importDefault(require("./signing-schemes/secp256k1"));
|
17 | const ed25519_1 = __importDefault(require("./signing-schemes/ed25519"));
|
18 | function getSigningScheme(algorithm) {
|
19 | const schemes = { 'ecdsa-secp256k1': secp256k1_1.default, ed25519: ed25519_1.default };
|
20 | return schemes[algorithm];
|
21 | }
|
22 | function 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 | }
|
30 | exports.generateSeed = generateSeed;
|
31 | function 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 |
|
41 | if (!scheme.verify(messageToVerify, signature, keypair.publicKey)) {
|
42 | throw new Error('derived keypair did not generate verifiable signature');
|
43 | }
|
44 | return keypair;
|
45 | }
|
46 | exports.deriveKeypair = deriveKeypair;
|
47 | function sign(messageHex, privateKey) {
|
48 | const algorithm = (0, getAlgorithmFromKey_1.getAlgorithmFromPrivateKey)(privateKey);
|
49 | return getSigningScheme(algorithm).sign((0, utils_1.hexToBytes)(messageHex), privateKey);
|
50 | }
|
51 | exports.sign = sign;
|
52 | function 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 | }
|
56 | exports.verify = verify;
|
57 | function computePublicKeyHash(publicKeyBytes) {
|
58 | return (0, ripemd160_1.ripemd160)((0, sha256_1.sha256)(publicKeyBytes));
|
59 | }
|
60 | function deriveAddressFromBytes(publicKeyBytes) {
|
61 | return (0, ripple_address_codec_1.encodeAccountID)(computePublicKeyHash(publicKeyBytes));
|
62 | }
|
63 | function deriveAddress(publicKey) {
|
64 | return deriveAddressFromBytes((0, utils_1.hexToBytes)(publicKey));
|
65 | }
|
66 | exports.deriveAddress = deriveAddress;
|
67 | function 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 | }
|
72 | exports.deriveNodeAddress = deriveNodeAddress;
|
73 |
|
\ | No newline at end of file |