UNPKG

shogun-core

Version:

SHOGUN SDK - Core library for Shogun SDK

109 lines (108 loc) 4.27 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const gun_1 = __importDefault(require("gun")); const web3Connector_1 = require("./web3Connector"); const web3Signer_1 = require("./web3Signer"); const web3Chain = () => { const web3 = new web3Connector_1.Web3Connector(); const signer = new web3Signer_1.Web3Signer(web3); // Initialize the web3 chain object if it doesn't exist if (!gun_1.default.chain.web3) { gun_1.default.chain.web3 = {}; } gun_1.default.chain.web3.connect = async function () { return await web3.connectMetaMask(); }; gun_1.default.chain.web3.generateCredentials = async function (address) { return await web3.generateCredentials(address); }; gun_1.default.chain.web3.getSigner = async function () { return await web3.getSigner(); }; gun_1.default.chain.web3.getProvider = async function () { return await web3.getProvider(); }; gun_1.default.chain.web3.signMessage = async function (message) { const signer = await web3.getSigner(); return await signer.signMessage(message); }; gun_1.default.chain.web3.verifySignature = async function (message, signature) { return await web3.verifySignature(message, signature); }; gun_1.default.chain.web3.isMetaMaskAvailable = function () { return web3Connector_1.Web3Connector.isMetaMaskAvailable(); }; /** * Setup oneshot signing for an Ethereum address * Creates signing credential with consistent password generation */ gun_1.default.chain.web3.setupOneshotSigning = async function (address) { return await signer.createSigningCredential(address); }; /** * Create authenticator for Web3 signing * Returns a function that can be used with SEA.sign */ gun_1.default.chain.web3.createAuthenticator = function (address) { return signer.createAuthenticator(address); }; /** * Create derived key pair from Web3 credential * Uses the same password generation as normal Web3 approach */ gun_1.default.chain.web3.createDerivedKeyPair = async function (address, extra) { return await signer.createDerivedKeyPair(address, extra); }; /** * Quick sign method that combines Web3 verification with derived key signing * Similar to webauthn.js but for Web3 */ gun_1.default.chain.web3.quickSign = async function (data, address, extra) { return await signer.signWithDerivedKeys(data, address, extra); }; /** * Creates a Gun user from Web3 signing credential * Ensures SAME user as normal approach */ gun_1.default.chain.web3.createGunUserFromSigningCredential = async function (address) { return await signer.createGunUser(address, this); }; /** * Get the Gun user public key for a signing credential */ gun_1.default.chain.web3.getGunUserPubFromSigningCredential = function (address) { return signer.getGunUserPub(address); }; /** * Get the password (for consistency checking) */ gun_1.default.chain.web3.getPassword = function (address) { return signer.getPassword(address); }; /** * Verify consistency between oneshot and normal approaches */ gun_1.default.chain.web3.verifyConsistency = async function (address, expectedUserPub) { return await signer.verifyConsistency(address, expectedUserPub); }; /** * Complete consistent oneshot signing workflow * Creates the SAME Gun user as normal approach */ gun_1.default.chain.web3.setupConsistentOneshotSigning = async function (address) { const credential = await signer.createSigningCredential(address); const authenticator = signer.createAuthenticator(address); const gunUser = await signer.createGunUser(address, this); return { credential, authenticator, gunUser, username: credential.username, password: credential.password, }; }; }; exports.default = web3Chain;