UNPKG

@bsv/sdk

Version:

BSV Blockchain Software Development Kit

53 lines 2.25 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createNonce = void 0; const Utils = __importStar(require("../../primitives/utils.js")); const Random_js_1 = __importDefault(require("../../primitives/Random.js")); /** * Creates a nonce derived from a wallet * @param wallet * @param counterparty - The counterparty to the nonce creation. Defaults to 'self'. * @returns A random nonce derived with a wallet */ async function createNonce(wallet, counterparty = 'self', originator) { // Generate 16 random bytes for the first half of the data const firstHalf = (0, Random_js_1.default)(16); // Create an sha256 HMAC const { hmac } = await wallet.createHmac({ protocolID: [2, 'server hmac'], keyID: Utils.toUTF8(firstHalf), data: firstHalf, counterparty }, originator); // Concatenate firstHalf and secondHalf as the nonce bytes const nonceBytes = [...firstHalf, ...hmac]; return Utils.toBase64(nonceBytes); } exports.createNonce = createNonce; //# sourceMappingURL=createNonce.js.map