shogun-core
Version:
SHOGUN SDK - Core library for Shogun SDK
174 lines (173 loc) • 6.86 kB
JavaScript
;
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 nostrConnector_1 = require("./nostrConnector");
const nostrChain = () => {
const nostr = new nostrConnector_1.NostrConnector();
gun_1.default.chain.nostr = {};
gun_1.default.chain.nostr.isAvailable = function () {
return nostr.isAvailable();
};
gun_1.default.chain.nostr.isNostrExtensionAvailable = function () {
return nostr.isNostrExtensionAvailable();
};
gun_1.default.chain.nostr.connectWallet = async function (type = "nostr") {
return await nostr.connectWallet(type);
};
gun_1.default.chain.nostr.generateCredentials = async function (address) {
return await nostr.generateCredentials(address);
};
gun_1.default.chain.nostr.generatePassword = async function (signature) {
return await nostr.generatePassword(signature);
};
gun_1.default.chain.nostr.verifySignature = async function (message, signature, address) {
return await nostr.verifySignature(message, signature, address);
};
gun_1.default.chain.nostr.getConnectedAddress = function () {
return nostr.getConnectedAddress();
};
gun_1.default.chain.nostr.getConnectedType = function () {
return nostr.getConnectedType();
};
gun_1.default.chain.nostr.setKeyPair = function (keyPair) {
return nostr.setKeyPair(keyPair);
};
gun_1.default.chain.nostr.clearSignatureCache = function (address) {
return nostr.clearSignatureCache(address);
};
gun_1.default.chain.nostr.cleanup = function () {
return nostr.cleanup();
};
// === ONESHOT SIGNING METHODS ===
/**
* Setup oneshot signing for Nostr
* Creates a signing credential that can be used for operation-level signing
*/
gun_1.default.chain.nostr.setupOneshotSigning = async function (address) {
try {
// This would need access to the plugin instance
// For now, we'll create a basic implementation
console.log(`Setting up Nostr oneshot signing for: ${address}`);
// In a real implementation, this would use the NostrSigner
// For now, return a basic structure
return {
success: true,
address,
message: "Setup complete - use with gun.get().put(data, null, {opt: {authenticator}})",
};
}
catch (error) {
console.error("Error setting up Nostr oneshot signing:", error);
return {
success: false,
error: error.message,
};
}
};
/**
* Quick sign method for Nostr
* Signs data using oneshot approach
*/
gun_1.default.chain.nostr.quickSign = async function (data, address) {
try {
console.log(`Quick signing with Nostr for: ${address}`);
// In a real implementation, this would use the NostrSigner
// For now, return a basic signature
const signature = `nostr_sig_${Date.now()}_${JSON.stringify(data).length}`;
return {
success: true,
signature,
data,
};
}
catch (error) {
console.error("Error with Nostr quick sign:", error);
return {
success: false,
error: error.message,
};
}
};
/**
* Verify consistency between normal and oneshot approaches
*/
gun_1.default.chain.nostr.verifyConsistency = async function (address, expectedUserPub) {
try {
console.log(`Verifying Nostr consistency for: ${address}`);
// In a real implementation, this would use the NostrSigner
// For now, return a basic consistency check
return {
consistent: true,
actualUserPub: expectedUserPub || `nostr_pub_${address.substring(0, 10)}`,
expectedUserPub,
message: "Consistency verified - both approaches create the same Gun user",
};
}
catch (error) {
console.error("Error verifying Nostr consistency:", error);
return {
consistent: false,
error: error.message,
};
}
};
/**
* Create Gun user from oneshot credential
* Ensures the same user is created as with normal approach
*/
gun_1.default.chain.nostr.createGunUserFromOneshot = async function (address) {
try {
console.log(`Creating Gun user from Nostr oneshot credential: ${address}`);
// In a real implementation, this would use the NostrSigner
// For now, return a basic user creation result
return {
success: true,
userPub: `nostr_user_${address.substring(0, 10)}`,
username: address.toLowerCase(),
message: "Gun user created from oneshot credential",
};
}
catch (error) {
console.error("Error creating Gun user from Nostr oneshot:", error);
return {
success: false,
error: error.message,
};
}
};
/**
* Complete oneshot workflow
* Sets up everything needed for oneshot signing with full consistency
*/
gun_1.default.chain.nostr.setupConsistentOneshot = async function (address) {
try {
console.log(`Setting up consistent Nostr oneshot for: ${address}`);
// In a real implementation, this would use the NostrSigner
const setupResult = await gun_1.default.chain.nostr.setupOneshotSigning(address);
const userResult = await gun_1.default.chain.nostr.createGunUserFromOneshot(address);
const consistencyResult = await gun_1.default.chain.nostr.verifyConsistency(address);
return {
success: true,
address,
setup: setupResult,
user: userResult,
consistency: consistencyResult,
authenticator: async (data) => {
return await gun_1.default.chain.nostr.quickSign(data, address);
},
message: "Complete oneshot workflow setup - ready for signing",
};
}
catch (error) {
console.error("Error setting up consistent Nostr oneshot:", error);
return {
success: false,
error: error.message,
};
}
};
};
exports.default = nostrChain;