smartledger-sdk
Version:
A comprehensive blockchain and cryptographic operations SDK for JavaScript
187 lines (151 loc) • 4.69 kB
JavaScript
import BSVKeys from "./Keys.js";
import BSVSignature from "./Signature.js";
import Encryption from "./Encryption.js";
import SecretSharer from "./SecretSharer.js";
import Hash from "./Hashes.js";
import bsv from "bsv";
import { v4 as uuidv4, v5 as uuidv5 } from "uuid";
class SmartLedger {
constructor() {
this.encryption = new Encryption();
this.signature = new BSVSignature();
this.keys = new BSVKeys();
this.secretSharer = new SecretSharer();
this.hash = Hash;
this.uuid = uuidv4();
this.bsv = bsv;
}
// Key Management Methods
static generateMnemonic() {
return BSVKeys.generateMnemonic();
}
static generateKeyPair() {
return BSVKeys.generateKeyPair();
}
static fromWIF(wif) {
return BSVKeys.fromWIF(wif);
}
static fromMnemonic(mnemonic, purpose = "financial") {
return BSVKeys.fromMnemonic(mnemonic, purpose);
}
generateAllKeys(mnemonic) {
return BSVKeys.generateAllKeys(mnemonic);
}
// Signature Methods
static signWithWIF(data, wif) {
const signer = BSVSignature.fromWIF(wif);
return signer.sign(data);
}
static signWithMnemonic(data, mnemonic, purpose = "document") {
const signer = BSVSignature.fromMnemonic(mnemonic, purpose);
return signer.sign(data);
}
static verifySignature(data, signature, publicKey) {
const verifier = BSVSignature.fromPublicKey(publicKey);
return verifier.verify(data, signature);
}
// Encryption Methods
static encrypt(data, key) {
return Encryption.encrypt(data, key);
}
static decrypt(ciphertext, key) {
return Encryption.decrypt(ciphertext, key);
}
// Secret Sharing Methods
splitSecret(secret, shares, threshold) {
return this.secretSharer.splitSecret(secret, shares, threshold);
}
combineShares(shares) {
return this.secretSharer.combineShares(shares);
}
shareToHex(share) {
return this.secretSharer.shareToHex(share);
}
hexToShare(hexShare) {
return this.secretSharer.hexToShare(hexShare);
}
// Hash Methods
static hash256(data) {
return Hash.hash256(data);
}
static hash512(data) {
return Hash.hash512(data);
}
static doubleHash256(data) {
return Hash.doubleHash256(data);
}
static hash160(data) {
return Hash.hash160(data);
}
static verifyHash(data, hash, algorithm = "256") {
return Hash.verifyHash(data, hash, algorithm);
}
// Instance Hash Methods
hash256(data) {
return this.hash.hash256(data);
}
hash512(data) {
return this.hash.hash512(data);
}
doubleHash256(data) {
return this.hash.doubleHash256(data);
}
hash160(data) {
return this.hash.hash160(data);
}
verifyHash(data, hash, algorithm = "256") {
return this.hash.verifyHash(data, hash, algorithm);
}
// UUID Methods
getUUID() {
return this.uuid;
}
static generateUUIDv5(name, namespace = "smartledger.example.com") {
// Create a namespace UUID using a domain
const namespaceUUID = uuidv5(namespace, uuidv5.DNS);
// Generate a v5 UUID using the namespace and name
return uuidv5(name, namespaceUUID);
}
}
export default SmartLedger;
export { SmartLedger };
// Example usage
const ledger = new SmartLedger();
const mnemonic = SmartLedger.generateMnemonic();
const keys = ledger.generateAllKeys(mnemonic);
console.log(keys);
// Encryption example
const encryptedData = SmartLedger.encrypt(
{ message: "Hello, World!" },
"my-secret-key"
);
console.log("Encrypted:", encryptedData);
const decryptedData = SmartLedger.decrypt(encryptedData, "my-secret-key");
console.log("Decrypted:", decryptedData);
// Hash example
const message = "Hello, SmartLedger!";
console.log("SHA256:", SmartLedger.hash256(message));
console.log("Double SHA256:", SmartLedger.doubleHash256(message));
console.log("HASH160:", SmartLedger.hash160(message));
// UUID examples
console.log("\nUUID Examples:");
console.log("Random UUIDv4:", ledger.getUUID());
// UUIDv5 examples
const resourceName = "transaction/123";
const uuidForResource = SmartLedger.generateUUIDv5(resourceName);
console.log("UUIDv5 for resource:", uuidForResource);
// Example of consistent UUIDs
const repeatedUuid1 = SmartLedger.generateUUIDv5(resourceName);
const repeatedUuid2 = SmartLedger.generateUUIDv5(resourceName);
console.log(
"UUIDv5 consistency check:",
repeatedUuid1 === repeatedUuid2 ? "✓ Matching" : "✗ Not matching"
);
// Example with custom namespace
const customNamespace = "myapp.smartledger.com";
const customResource = "user/456";
const uuidWithCustomNamespace = SmartLedger.generateUUIDv5(
customResource,
customNamespace
);
console.log("UUIDv5 with custom namespace:", uuidWithCustomNamespace);