synkrokonn-dev
Version:
Plugin-based cross-chain orchestration middleware for Web3 enterprise automation.
51 lines (50 loc) • 2 kB
JavaScript
import { Gateway, Wallets } from 'fabric-network';
import * as path from 'path';
import * as fs from 'fs';
import { fileURLToPath } from 'url';
// Fix for __dirname in ES modules
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export class FabricAdapter {
constructor() {
this.contract = null;
this.gateway = new Gateway();
}
async connect() {
const ccpPath = path.resolve(__dirname, 'connection-org1.json');
const ccp = JSON.parse(fs.readFileSync(ccpPath, 'utf8'));
const walletPath = path.join(__dirname, 'wallet');
const wallet = await Wallets.newFileSystemWallet(walletPath);
await this.gateway.connect(ccp, {
wallet,
identity: 'admin',
discovery: { enabled: true, asLocalhost: true }
});
const network = await this.gateway.getNetwork('mychannel');
this.contract = network.getContract('basic');
const listener = async (event) => {
console.log(`Fabric Event Received: ${event.eventName}`);
const payload = event.payload.toString();
console.log(`Payload:`, payload);
};
await this.contract.addContractListener(listener);
}
async getAccountAddress() {
return 'admin@org1.example.com'; // or actual identity string if available
}
async sendTransaction(contractAddress, functionName, args) {
if (!this.contract)
throw new Error("Fabric contract not initialized");
const result = await this.contract.submitTransaction(functionName, ...args.values);
return result.toString();
}
async query(functionName, args) {
if (!this.contract)
throw new Error("Fabric contract not initialized");
const result = await this.contract.evaluateTransaction(functionName, ...args);
return result.toString();
}
async disconnect() {
this.gateway.disconnect();
}
}