smartledger-sdk
Version:
A comprehensive blockchain and cryptographic operations SDK for JavaScript
93 lines (86 loc) • 3.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.BSVSignature = void 0;
var _bsv = _interopRequireDefault(require("bsv"));
var _Keys = require("./Keys.js");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
class BSVSignature {
privateKey;
publicKey;
purpose;
constructor(privateKey = null, publicKey = null) {
this.privateKey = privateKey;
this.publicKey = publicKey;
}
static fromWIF(wif) {
const privateKey = _bsv.default.PrivateKey.fromWIF(wif);
return new BSVSignature(privateKey, privateKey.publicKey);
}
static fromPrivateKey(privateKey, purpose = null) {
return new BSVSignature(privateKey, privateKey.publicKey);
}
static fromMnemonic(mnemonic, purpose = "document") {
const keys = _Keys.BSVKeys.fromMnemonic(mnemonic, purpose);
return new BSVSignature(keys.privateKey, keys.publicKey);
}
static fromPublicKey(publicKeyHex) {
const publicKey = _bsv.default.PublicKey.fromString(publicKeyHex);
return new BSVSignature(null, publicKey);
}
sign(data) {
if (!this.privateKey) {
throw new Error("Private key is required for signing");
}
const dataBuffer = Buffer.from(data);
const hashBuffer = _bsv.default.crypto.Hash.sha256(dataBuffer);
const signature = _bsv.default.crypto.ECDSA.sign(hashBuffer, this.privateKey);
return signature.toString();
}
verify(data, signatureStr, publicKeyStr) {
try {
const publicKey = _bsv.default.PublicKey.fromString(publicKeyStr);
const signature = _bsv.default.crypto.Signature.fromString(signatureStr);
const dataBuffer = Buffer.from(data);
const hashBuffer = _bsv.default.crypto.Hash.sha256(dataBuffer);
return _bsv.default.crypto.ECDSA.verify(hashBuffer, signature, publicKey);
} catch (error) {
console.error("Signature verification error:", error);
return false;
}
}
getPublicKey() {
return this.publicKey;
}
getPurpose() {
return this.purpose;
}
}
// // Test example
// if (import.meta.url === `file://${process.cwd()}/Signature.js`) {
// try {
// // Generate a new mnemonic
// const mnemonic = BSVKeys.generateMnemonic();
// console.log("Mnemonic:", mnemonic.toString());
// // Create signatures for different purposes
// const purposes = ["document", "contractual", "identity"];
// const testData = "Hello, Web3Keys!";
// for (const purpose of purposes) {
// console.log(`\nTesting ${purpose} signature:`);
// const signer = BSVSignature.fromMnemonic(mnemonic, purpose);
// // Sign the data
// const signature = signer.sign(testData);
// console.log("Signature:", signature.toString());
// // Verify the signature
// const isValid = signer.verify(testData, signature.toString(), signer.getPublicKey());
// console.log("Public Key:", signer.getPublicKey().toString());
// console.log("Purpose:", signer.getPurpose());
// console.log("Signature Valid:", isValid);
// }
// } catch (error) {
// console.error("Error:", error.message);
// }
// }
exports.BSVSignature = BSVSignature;
var _default = exports.default = BSVSignature;