Skip to content
Snippets Groups Projects
secrets.js 977 B
import { secrets } from "secrets.js-grempe";
import { encryptMessage } from "./signingUtilities.js";

export /**
 * Function generates a random bits length string, and output it in hexadecimal format
 *
 * @param {number} bits
 */
const generateRecoveryKey = bits => secrets.random(bits);

/**
 * Divide a secret expressed in hexadecimal form into numShares number of shares, requiring that threshold number of shares be present for reconstructing the secret
 *
 * @param {string} secret
 * @param {number} numShares
 * @param {number} threshold
 * @param {number} [padLength=128]
 */
export const divideSecretToShares = (
  secret,
  numShares,
  threshold,
  padLength = 128
) => secrets.share(secret, numShares, threshold, padLength);

/**
 * Reconstructs a secret from shares
 *
 * @param {array} shares
 */
export const combineSecret = shares => secrets.combine(shares);

export const encryptShare = (share, publicKey) =>
  encryptMessage(share, publicKey, "secretPart");