Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x | import { AcquireCertificateArgs, DiscoverByAttributesArgs, DiscoverByIdentityKeyArgs, ListCertificatesArgs, ProveCertificateArgs, RelinquishCertificateArgs } from '@bsv/sdk'
import { sdk, StorageKnex } from '../../src/index.all'
import { Wallet } from '../../src/Wallet'
import { jest } from '@jest/globals'
/**
* Logging Utility
* Centralized logging for debugging test cases.
*/
let logEnabled: boolean = true
export const setLogging = (enabled: boolean): void => {
logEnabled = enabled
}
export const log = (message: string, ...optionalParams: any[]): void => {
Iif (logEnabled) {
console.log(`[LOG] ${message}`, ...optionalParams)
}
}
/**
* Mock Utilities
* Provides reusable mock implementations for WalletSigner and KeyDeriver.
*/
export const mockWalletSigner = (): any => ({
isAuthenticated: jest.fn().mockReturnValue(true),
storageIdentity: { storageIdentityKey: 'mockStorageKey' },
getClientChangeKeyPair: jest.fn().mockReturnValue({ publicKey: 'mockPublicKey' }),
chain: 'test',
getChain: jest.fn(),
getHeaderForHeight: jest.fn(),
getHeight: jest.fn(),
getNetwork: jest.fn(),
listCertificatesSdk: jest.fn(),
acquireCertificateSdk: jest.fn(),
relinquishCertificateSdk: jest.fn(),
proveCertificateSdk: jest.fn(),
discoverByIdentityKeySdk: jest.fn(),
discoverByAttributesSdk: jest.fn()
})
export const mockKeyDeriver = (): any => ({
rootKey: {
deriveChild: jest.fn(),
toPublicKey: jest.fn(() => ({ toString: jest.fn().mockReturnValue('mockIdentityKey') }))
},
identityKey: 'mockIdentityKey',
derivePublicKey: jest.fn(),
derivePrivateKey: jest.fn(),
deriveSymmetricKey: jest.fn(),
revealCounterpartySecret: jest.fn(),
revealSpecificSecret: jest.fn()
})
/**
* Argument and Response Generators
* Creates reusable test data for arguments and expected responses.
*/
export const generateListCertificatesArgs = (overrides = {}): ListCertificatesArgs => ({
certifiers: [],
types: [],
limit: 10,
offset: 0,
privileged: false,
...overrides
})
export const generateMockCertificatesResponse = (overrides = {}): any => ({
certificates: [],
...overrides
})
export const generateAcquireCertificateArgs = (overrides = {}): AcquireCertificateArgs => ({
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
})
export const generateMockAcquireCertificateResponse = (overrides = {}): any => ({
success: true,
...overrides
})
export const generateRelinquishCertificateArgs = (overrides = {}): RelinquishCertificateArgs => ({
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
})
export const generateMockRelinquishCertificateResponse = (overrides = {}): any => ({
success: true,
...overrides
})
export const generateProveCertificateArgs = (overrides = {}): ProveCertificateArgs => ({
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
})
export const generateMockProveCertificateResponse = (overrides = {}): any => ({
proof: 'mockProof',
...overrides
})
export const generateDiscoverByIdentityKeyArgs = (overrides = {}): DiscoverByIdentityKeyArgs => ({
identityKey: 'mockIdentityKey',
...overrides
})
export const generateMockDiscoverByIdentityKeyResponse = (overrides = {}): any => ({
certificates: [],
...overrides
})
export const generateDiscoverByAttributesArgs = (overrides = {}): DiscoverByAttributesArgs => ({
attributes: { mockAttribute: 'value' },
...overrides
})
export const generateMockDiscoverByAttributesResponse = (overrides = {}): any => ({
certificates: [],
...overrides
})
/**
* Validation Helpers
* Provides functions to validate results and handle errors in tests.
*/
export const validateCertificatesResponse = (result: any, expected: any): void => {
expect(result).toEqual(expected)
}
export const validateAcquireCertificateResponse = (result: any, expected: any): void => {
expect(result).toEqual(expected)
}
export const validateRelinquishCertificateResponse = (result: any, expected: any): void => {
expect(result).toEqual(expected)
}
export const validateProveCertificateResponse = (result: any, expected: any): void => {
expect(result).toEqual(expected)
}
export const validateDiscoverByIdentityKeyResponse = (result: any, expected: any): void => {
expect(result).toEqual(expected)
}
export const validateDiscoverByAttributesResponse = (result: any, expected: any): void => {
expect(result).toEqual(expected)
}
/**
* Test Utilities
* Sets up a mock Wallet instance and associated dependencies for tests.
*/
export const setupTestWallet = (): { wallet: Wallet; mockSigner: any; mockKeyDeriver: any } => {
const mockSigner = mockWalletSigner()
const mockKeyDeriverInstance = mockKeyDeriver()
const wallet = new Wallet(mockSigner, mockKeyDeriverInstance)
return { wallet, mockSigner, mockKeyDeriver: mockKeyDeriverInstance }
}
/**
* 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: Wallet, storage: StorageKnex, status: sdk.TransactionStatus): Promise<boolean> {
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.
*/
export async function cleanUnsentTransactionsUsingAbort(wallet: Wallet, storage: StorageKnex): Promise<boolean> {
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.
*/
export async function cleanUnsignedTransactionsUsingAbort(wallet: Wallet, storage: StorageKnex): Promise<boolean> {
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.
*/
export async function cleanUnprocessedTransactionsUsingAbort(wallet: Wallet, storage: StorageKnex): Promise<boolean> {
const result = await cleanTransactionsUsingAbort(wallet, storage, 'unprocessed')
expect(result).toBe(true)
return result
}
|