UNPKG

@firefly-exchange/library-sui

Version:

Sui library housing helper methods, classes to interact with Bluefin protocol(s) deployed on Sui

67 lines (66 loc) 2.75 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; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.OnboardingSigner = void 0; const secp = __importStar(require("@noble/secp256k1")); const sha256_1 = require("@noble/hashes/sha256"); const library_1 = require("../library"); const types_1 = require("../types"); class OnboardingSigner { /** * Creates hash of given message and signs it with given private key or web3 provider * @param message string to be sign * @param signer RawSigner * @returns signature */ static async createOnboardSignature(message, signer) { if (!signer) { throw Error(`Invalid signer`); } const msgHash = (0, sha256_1.sha256)((0, library_1.hexToBuffer)(message)); const signedMsg = await signer.signPersonalMessage(msgHash); return signedMsg.signature; } /** * Recovers user address from the signature and compares it with given public address * @param address public address of user * @param message message to be signed * @param signature * @returns */ static verifyOnboardSignature(address, message, signature) { const signatureWithR = (0, library_1.hexToBuffer)(signature); if (signatureWithR.length == 65) { const sig = signatureWithR.subarray(0, 64); const rByte = signatureWithR[64]; const hash = (0, library_1.hexToBuffer)(message); const publicKey = secp.recoverPublicKey(hash, sig, rByte, true); const secp256k1PK = new types_1.Secp256k1PublicKey(publicKey); return secp256k1PK.toSuiAddress() === address; } return false; } } exports.OnboardingSigner = OnboardingSigner;