ripple-keypairs
Version:
Cryptographic key pairs for the XRP Ledger
44 lines • 2.43 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const utils_js_1 = require("@noble/curves/utils.js");
const secp256k1_js_1 = require("@noble/curves/secp256k1.js");
const utils_1 = require("@xrplf/isomorphic/utils");
const utils_2 = require("./utils");
const assert_1 = __importDefault(require("../../utils/assert"));
const Sha512_1 = __importDefault(require("../../utils/Sha512"));
const SECP256K1_PREFIX = '00';
const secp256k1 = {
deriveKeypair(entropy, options) {
const derived = (0, utils_2.derivePrivateKey)(entropy, options);
const privateKey = SECP256K1_PREFIX + (0, utils_1.bytesToHex)((0, utils_js_1.numberToBytesBE)(derived, 32));
const publicKey = (0, utils_1.bytesToHex)(secp256k1_js_1.secp256k1.getPublicKey((0, utils_js_1.numberToBytesBE)(derived, 32), true));
return { privateKey, publicKey };
},
sign(message, privateKey) {
// Some callers pass the privateKey with the prefix, others without.
// @noble/curves will throw if the key is not exactly 32 bytes, so we
// normalize it before passing to the sign method.
assert_1.default.ok((privateKey.length === 66 && privateKey.startsWith(SECP256K1_PREFIX)) ||
privateKey.length === 64);
const normedPrivateKey = privateKey.length === 66 ? privateKey.slice(2) : privateKey;
return (0, utils_1.bytesToHex)(secp256k1_js_1.secp256k1.sign(Sha512_1.default.half(message), (0, utils_1.hexToBytes)(normedPrivateKey), {
// "Canonical" signatures
lowS: true,
// Would fail tests if signatures aren't deterministic
extraEntropy: undefined,
format: 'der',
// We pass a pre-hashed message (Sha512Half), so disable secp256k1's
// default SHA-256 prehashing (added as default in @noble/curves 2.0.0)
prehash: false,
})).toUpperCase();
},
verify(message, signature, publicKey) {
const decoded = secp256k1_js_1.secp256k1.Signature.fromHex(signature, 'der');
return secp256k1_js_1.secp256k1.verify(decoded.toBytes('compact'), Sha512_1.default.half(message), (0, utils_1.hexToBytes)(publicKey), { prehash: false });
},
};
exports.default = secp256k1;
//# sourceMappingURL=index.js.map