wallet-storage
Version:
BRC100 conforming wallet, wallet storage and wallet signer components
237 lines • 11.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.setupTestWallet = exports.validateDiscoverByAttributesResponse = exports.validateDiscoverByIdentityKeyResponse = exports.validateProveCertificateResponse = exports.validateRelinquishCertificateResponse = exports.validateAcquireCertificateResponse = exports.validateCertificatesResponse = exports.generateMockDiscoverByAttributesResponse = exports.generateDiscoverByAttributesArgs = exports.generateMockDiscoverByIdentityKeyResponse = exports.generateDiscoverByIdentityKeyArgs = exports.generateMockProveCertificateResponse = exports.generateProveCertificateArgs = exports.generateMockRelinquishCertificateResponse = exports.generateRelinquishCertificateArgs = exports.generateMockAcquireCertificateResponse = exports.generateAcquireCertificateArgs = exports.generateMockCertificatesResponse = exports.generateListCertificatesArgs = exports.mockKeyDeriver = exports.mockWalletSigner = exports.log = exports.setLogging = void 0;
exports.cleanUnsentTransactionsUsingAbort = cleanUnsentTransactionsUsingAbort;
exports.cleanUnsignedTransactionsUsingAbort = cleanUnsignedTransactionsUsingAbort;
exports.cleanUnprocessedTransactionsUsingAbort = cleanUnprocessedTransactionsUsingAbort;
const Wallet_1 = require("../../src/Wallet");
const globals_1 = require("@jest/globals");
/**
* Logging Utility
* Centralized logging for debugging test cases.
*/
let logEnabled = true;
const setLogging = (enabled) => {
logEnabled = enabled;
};
exports.setLogging = setLogging;
const log = (message, ...optionalParams) => {
if (logEnabled) {
console.log(`[LOG] ${message}`, ...optionalParams);
}
};
exports.log = log;
/**
* Mock Utilities
* Provides reusable mock implementations for WalletSigner and KeyDeriver.
*/
const mockWalletSigner = () => ({
isAuthenticated: globals_1.jest.fn().mockReturnValue(true),
storageIdentity: { storageIdentityKey: 'mockStorageKey' },
getClientChangeKeyPair: globals_1.jest.fn().mockReturnValue({ publicKey: 'mockPublicKey' }),
chain: 'test',
getChain: globals_1.jest.fn(),
getHeaderForHeight: globals_1.jest.fn(),
getHeight: globals_1.jest.fn(),
getNetwork: globals_1.jest.fn(),
listCertificatesSdk: globals_1.jest.fn(),
acquireCertificateSdk: globals_1.jest.fn(),
relinquishCertificateSdk: globals_1.jest.fn(),
proveCertificateSdk: globals_1.jest.fn(),
discoverByIdentityKeySdk: globals_1.jest.fn(),
discoverByAttributesSdk: globals_1.jest.fn()
});
exports.mockWalletSigner = mockWalletSigner;
const mockKeyDeriver = () => ({
rootKey: {
deriveChild: globals_1.jest.fn(),
toPublicKey: globals_1.jest.fn(() => ({ toString: globals_1.jest.fn().mockReturnValue('mockIdentityKey') }))
},
identityKey: 'mockIdentityKey',
derivePublicKey: globals_1.jest.fn(),
derivePrivateKey: globals_1.jest.fn(),
deriveSymmetricKey: globals_1.jest.fn(),
revealCounterpartySecret: globals_1.jest.fn(),
revealSpecificSecret: globals_1.jest.fn()
});
exports.mockKeyDeriver = mockKeyDeriver;
/**
* Argument and Response Generators
* Creates reusable test data for arguments and expected responses.
*/
const generateListCertificatesArgs = (overrides = {}) => ({
certifiers: [],
types: [],
limit: 10,
offset: 0,
privileged: false,
...overrides
});
exports.generateListCertificatesArgs = generateListCertificatesArgs;
const generateMockCertificatesResponse = (overrides = {}) => ({
certificates: [],
...overrides
});
exports.generateMockCertificatesResponse = generateMockCertificatesResponse;
const generateAcquireCertificateArgs = (overrides = {}) => ({
type: 'mockType', // Base64String: A valid certificate type
certifier: 'mockCertifier', // PubKeyHex: Certifier's public key
acquisitionProtocol: 'direct', // AcquisitionProtocol: 'direct' or 'issuance'
fields: { fieldName: 'mockValue' }, // Record of CertificateFieldNameUnder50Bytes to string
serialNumber: 'mockSerialNumber', // Optional: Base64String for the serial number
revocationOutpoint: 'mockTxid.0', // Optional: OutpointString for revocation
signature: 'mockSignature', // Optional: HexString for signature
keyringRevealer: 'certifier', // Optional: KeyringRevealer
keyringForSubject: { fieldName: 'mockKeyringValue' }, // Optional: Record for keyring
privileged: false, // Optional: BooleanDefaultFalse for privileged access
privilegedReason: 'Testing', // Optional: DescriptionString5to50Bytes for privileged reason
...overrides // Allow overrides for specific test cases
});
exports.generateAcquireCertificateArgs = generateAcquireCertificateArgs;
const generateMockAcquireCertificateResponse = (overrides = {}) => ({
success: true,
...overrides
});
exports.generateMockAcquireCertificateResponse = generateMockAcquireCertificateResponse;
const generateRelinquishCertificateArgs = (overrides = {}) => ({
type: 'mockType', // Base64String: A valid certificate type
serialNumber: 'mockSerialNumber', // Base64String: The certificate's serial number
certifier: 'mockCertifier', // PubKeyHex: Certifier's public key
...overrides // Allow overrides for specific test cases
});
exports.generateRelinquishCertificateArgs = generateRelinquishCertificateArgs;
const generateMockRelinquishCertificateResponse = (overrides = {}) => ({
success: true,
...overrides
});
exports.generateMockRelinquishCertificateResponse = generateMockRelinquishCertificateResponse;
const generateProveCertificateArgs = (overrides = {}) => ({
certificate: {
type: 'mockType',
certifier: 'mockCertifier',
serialNumber: 'mockSerialNumber'
}, // Mock partial WalletCertificate
fieldsToReveal: ['name', 'email'], // Mock fields to reveal (adjust as per valid field names in your schema)
verifier: 'mockVerifierPublicKey', // Mock verifier's public key
privileged: false, // Default to non-privileged
privilegedReason: 'Testing', // Reason for privileged access (if needed)
...overrides // Allow specific overrides for testing
});
exports.generateProveCertificateArgs = generateProveCertificateArgs;
const generateMockProveCertificateResponse = (overrides = {}) => ({
proof: 'mockProof',
...overrides
});
exports.generateMockProveCertificateResponse = generateMockProveCertificateResponse;
const generateDiscoverByIdentityKeyArgs = (overrides = {}) => ({
identityKey: 'mockIdentityKey',
...overrides
});
exports.generateDiscoverByIdentityKeyArgs = generateDiscoverByIdentityKeyArgs;
const generateMockDiscoverByIdentityKeyResponse = (overrides = {}) => ({
certificates: [],
...overrides
});
exports.generateMockDiscoverByIdentityKeyResponse = generateMockDiscoverByIdentityKeyResponse;
const generateDiscoverByAttributesArgs = (overrides = {}) => ({
attributes: { mockAttribute: 'value' },
...overrides
});
exports.generateDiscoverByAttributesArgs = generateDiscoverByAttributesArgs;
const generateMockDiscoverByAttributesResponse = (overrides = {}) => ({
certificates: [],
...overrides
});
exports.generateMockDiscoverByAttributesResponse = generateMockDiscoverByAttributesResponse;
/**
* Validation Helpers
* Provides functions to validate results and handle errors in tests.
*/
const validateCertificatesResponse = (result, expected) => {
expect(result).toEqual(expected);
};
exports.validateCertificatesResponse = validateCertificatesResponse;
const validateAcquireCertificateResponse = (result, expected) => {
expect(result).toEqual(expected);
};
exports.validateAcquireCertificateResponse = validateAcquireCertificateResponse;
const validateRelinquishCertificateResponse = (result, expected) => {
expect(result).toEqual(expected);
};
exports.validateRelinquishCertificateResponse = validateRelinquishCertificateResponse;
const validateProveCertificateResponse = (result, expected) => {
expect(result).toEqual(expected);
};
exports.validateProveCertificateResponse = validateProveCertificateResponse;
const validateDiscoverByIdentityKeyResponse = (result, expected) => {
expect(result).toEqual(expected);
};
exports.validateDiscoverByIdentityKeyResponse = validateDiscoverByIdentityKeyResponse;
const validateDiscoverByAttributesResponse = (result, expected) => {
expect(result).toEqual(expected);
};
exports.validateDiscoverByAttributesResponse = validateDiscoverByAttributesResponse;
/**
* Test Utilities
* Sets up a mock Wallet instance and associated dependencies for tests.
*/
const setupTestWallet = () => {
const mockSigner = (0, exports.mockWalletSigner)();
const mockKeyDeriverInstance = (0, exports.mockKeyDeriver)();
const wallet = new Wallet_1.Wallet(mockSigner);
return { wallet, mockSigner, mockKeyDeriver: mockKeyDeriverInstance };
};
exports.setupTestWallet = setupTestWallet;
/**
* Aborts all transactions with a specific status in the storage and asserts they are aborted.
*
* @param {Wallet} wallet - The wallet instance used to abort actions.
* @param {StorageKnex} storage - The storage instance to query transactions from.
* @param {sdk.TransactionStatus} status - The transaction status used to filter transactions.
* @returns {Promise<boolean>} - Resolves to `true` if all matching transactions were successfully aborted.
*/
async function cleanTransactionsUsingAbort(wallet, storage, status) {
const transactions = await storage.findTransactions({ partial: { status } });
await Promise.all(transactions.map(async (transaction) => {
const result = await wallet.abortAction({ reference: transaction.reference });
expect(result.aborted).toBe(true); // Assert that each abort was successful
}));
return true;
}
/**
* Aborts all transactions with the status `'nosend'` in the storage and verifies success.
*
* @param {Wallet} wallet - The wallet instance used to abort actions.
* @param {StorageKnex} storage - The storage instance to query transactions from.
* @returns {Promise<boolean>} - Resolves to `true` if all `'nosend'` transactions were successfully aborted.
*/
async function cleanUnsentTransactionsUsingAbort(wallet, storage) {
const result = await cleanTransactionsUsingAbort(wallet, storage, 'nosend');
expect(result).toBe(true);
return result;
}
/**
* Aborts all transactions with the status `'unsigned'` in the storage and verifies success.
*
* @param {Wallet} wallet - The wallet instance used to abort actions.
* @param {StorageKnex} storage - The storage instance to query transactions from.
* @returns {Promise<boolean>} - Resolves to `true` if all `'unsigned'` transactions were successfully aborted.
*/
async function cleanUnsignedTransactionsUsingAbort(wallet, storage) {
const result = await cleanTransactionsUsingAbort(wallet, storage, 'unsigned');
expect(result).toBe(true);
return result;
}
/**
* Aborts all transactions with the status `'unprocessed'` in the storage and verifies success.
*
* @param {Wallet} wallet - The wallet instance used to abort actions.
* @param {StorageKnex} storage - The storage instance to query transactions from.
* @returns {Promise<boolean>} - Resolves to `true` if all `'unprocessed'` transactions were successfully aborted.
*/
async function cleanUnprocessedTransactionsUsingAbort(wallet, storage) {
const result = await cleanTransactionsUsingAbort(wallet, storage, 'unprocessed');
expect(result).toBe(true);
return result;
}
//# sourceMappingURL=TestUtilsMethodTests.js.map