UNPKG

3.54 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 });
6var tweetnacl_1 = __importDefault(require("tweetnacl"));
7function bytesToHex(bytes) {
8 return Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)
9 .toString('hex')
10 .toUpperCase();
11}
12exports.bytesToHex = bytesToHex;
13function numberToHex(num) {
14 return "0x" + num.toString(16);
15}
16exports.numberToHex = numberToHex;
17function hexToNumber(hex) {
18 return parseInt(hex, 16);
19}
20exports.hexToNumber = hexToNumber;
21function bytesToHexAddr(bytes) {
22 return '0x' + bytesToHex(bytes);
23}
24exports.bytesToHexAddr = bytesToHexAddr;
25function getGUID() {
26 function s4() {
27 return Math.floor((1 + Math.random()) * 0x10000)
28 .toString(16)
29 .substring(1);
30 }
31 return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
32}
33exports.getGUID = getGUID;
34exports.SIGNATURE_LENGTH = tweetnacl_1.default.sign.signatureLength;
35exports.PRIVATE_KEY_LENGTH = tweetnacl_1.default.sign.secretKeyLength;
36exports.PUBLIC_KEY_LENGTH = tweetnacl_1.default.sign.publicKeyLength;
37/**
38 * Generates a private key for signing.
39 * @returns 64-byte private key.
40 */
41function generatePrivateKey() {
42 var pair = tweetnacl_1.default.sign.keyPair();
43 return pair.secretKey;
44}
45exports.generatePrivateKey = generatePrivateKey;
46/**
47 * Generates the public key that corresponds to the given private key.
48 * @param privateKey 64-byte private key.
49 * @returns 32-byte public key.
50 */
51function publicKeyFromPrivateKey(privateKey) {
52 var pair = tweetnacl_1.default.sign.keyPair.fromSecretKey(privateKey);
53 return pair.publicKey;
54}
55exports.publicKeyFromPrivateKey = publicKeyFromPrivateKey;
56/**
57 * Signs a message with the given private key.
58 * @param msg Message to be signed.
59 * @param privateKey 64-byte private key to sign with.
60 * @returns The generated signature.
61 */
62function sign(msg, privateKey) {
63 var sigMsg = tweetnacl_1.default.sign(msg, privateKey);
64 return sigMsg.slice(0, exports.SIGNATURE_LENGTH);
65}
66exports.sign = sign;
67/**
68 * Encodes bytes to a base64 string.
69 * @param bytes Array of bytes to encode to string.
70 * @returns base64 encoded string.
71 */
72function Uint8ArrayToB64(bytes) {
73 return Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength).toString('base64');
74}
75exports.Uint8ArrayToB64 = Uint8ArrayToB64;
76/**
77 * Decodes bytes from a base64 string.
78 * @param s String to decode.
79 * @returns Array of bytes.
80 */
81function B64ToUint8Array(s) {
82 return Buffer.from(s, 'base64');
83}
84exports.B64ToUint8Array = B64ToUint8Array;
85function bufferToProtobufBytes(input) {
86 // Buffer in Node is a Uint8Array, but someone broke a runtime type check in Protobuf 3.4.0,
87 // so Protobuf fails to serialize/deserialize Buffer(s). Have to wait for
88 // https://github.com/google/protobuf/pull/4378 to make into a release (maybe 3.5.3)
89 // so that https://github.com/google/protobuf/issues/1319 is fixed... no one seems to be
90 // in any rush to push out a new release though.
91 // In the meantime work around the issue by copying the Buffer into a plain Uint8Array:
92 return input.constructor === Buffer ? new Uint8Array(input) : input;
93}
94exports.bufferToProtobufBytes = bufferToProtobufBytes;
95//# sourceMappingURL=crypto-utils.js.map
\No newline at end of file