@bsv/payment-express-middleware
Version:
BSV Blockchain service monetization express middleware
94 lines (93 loc) • 4.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MockWallet = void 0;
const sdk_1 = require("@bsv/sdk");
/**
* MockWallet extends CompletedProtoWallet and provides concrete
* implementations for select methods used for testing.
*/
class MockWallet extends sdk_1.CompletedProtoWallet {
constructor(rootKeyOrKeyDeriver) {
super(rootKeyOrKeyDeriver);
this.storedCertificates = [];
if (rootKeyOrKeyDeriver instanceof sdk_1.KeyDeriver) {
this.keyDeriver = rootKeyOrKeyDeriver;
}
else if (typeof rootKeyOrKeyDeriver === 'string' ||
rootKeyOrKeyDeriver instanceof sdk_1.PrivateKey) {
this.keyDeriver = new sdk_1.KeyDeriver(rootKeyOrKeyDeriver);
}
else {
throw new Error('Invalid key deriver provided');
}
}
/**
* Add a master certificate to the wallet for testing purposes.
*/
addMasterCertificate(masterCertificate) {
this.storedCertificates.push(masterCertificate);
}
async createAction(args, originator) {
var _a, _b;
// Mock response based on provided arguments
const mockResponse = {
txid: ((_a = args === null || args === void 0 ? void 0 : args.options) === null || _a === void 0 ? void 0 : _a.returnTXIDOnly) ? 'mocked_txid_12345' : undefined,
tx: ((_b = args === null || args === void 0 ? void 0 : args.options) === null || _b === void 0 ? void 0 : _b.noSend) ? undefined : [0xBE, 0xEF]
};
return mockResponse;
}
/**
* Given a certificate and fields to reveal, this method creates a keyring
* for the verifier by leveraging the masterCertificate’s capabilities.
*/
async proveCertificate(args, originator) {
if (args === undefined) {
throw new Error('Must provide args for test');
}
const storedCert = this.storedCertificates.find(sc => sc.type === args.certificate.type &&
sc.subject === args.certificate.subject &&
sc.serialNumber === args.certificate.serialNumber &&
sc.certifier === args.certificate.certifier);
if (storedCert === undefined) {
throw new Error('Certificate not found in MockWallet.');
}
// Create the keyring for the verifier (using the masterCertificate's method)
const keyringForVerifier = await sdk_1.MasterCertificate.createKeyringForVerifier(this, storedCert.certifier, args.verifier, storedCert.fields, args.fieldsToReveal, storedCert.masterKeyring, storedCert.serialNumber);
return { keyringForVerifier };
}
/**
* Mock implementation of internalizeAction.
* Logs the provided action details and returns a successful response.
*/
async internalizeAction(args, originator) {
console.log('Mock internalizeAction called with:', { args, originator });
return await Promise.resolve({ accepted: true });
}
/**
* Returns any certificates whose certifier and type match the requested sets.
*/
async listCertificates(args, originator) {
// Filter certificates by requested certifiers and types
const filtered = this.storedCertificates.filter(cert => {
return (args === null || args === void 0 ? void 0 : args.certifiers.includes(cert.certifier)) && (args === null || args === void 0 ? void 0 : args.types.includes(cert.type));
});
// For testing, limit and offset can be ignored or handled trivially
const totalCertificates = filtered.length;
return {
totalCertificates,
certificates: filtered.map(cert => {
var _a;
return ({
type: cert.type,
subject: cert.subject,
serialNumber: cert.serialNumber,
certifier: cert.certifier,
revocationOutpoint: cert.revocationOutpoint,
signature: (_a = cert.signature) !== null && _a !== void 0 ? _a : '',
fields: cert.fields
});
})
};
}
}
exports.MockWallet = MockWallet;