Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/hooks/__tests__/useBCH.test.js
Show First 20 Lines • Show All 142 Lines • ▼ Show 20 Lines | it('sends XEC correctly', async () => { | ||||
expect(BCH.RawTransactions.sendRawTransaction).toHaveBeenCalledWith( | expect(BCH.RawTransactions.sendRawTransaction).toHaveBeenCalledWith( | ||||
expectedHex, | expectedHex, | ||||
); | ); | ||||
}); | }); | ||||
it('sends XEC correctly with an encrypted OP_RETURN message', async () => { | it('sends XEC correctly with an encrypted OP_RETURN message', async () => { | ||||
const { sendXec } = useBCH(); | const { sendXec } = useBCH(); | ||||
const BCH = new BCHJS(); | const BCH = new BCHJS(); | ||||
const { expectedTxId, utxos, wallet, destinationAddress, sendAmount } = | const { | ||||
sendBCHMock; | expectedTxId, | ||||
const expectedPubKeyResponse = { | expectedHex, | ||||
success: true, | utxos, | ||||
publicKey: | wallet, | ||||
'03451a3e61ae8eb76b8d4cd6057e4ebaf3ef63ae3fe5f441b72c743b5810b6a389', | destinationAddress, | ||||
}; | sendAmount, | ||||
} = sendBCHMock; | |||||
BCH.encryption.getPubKey = jest | |||||
.fn() | |||||
.mockResolvedValue(expectedPubKeyResponse); | |||||
BCH.RawTransactions.sendRawTransaction = jest | BCH.RawTransactions.sendRawTransaction = jest | ||||
.fn() | .fn() | ||||
.mockResolvedValue(expectedTxId); | .mockResolvedValue(expectedTxId); | ||||
expect( | expect( | ||||
await sendXec( | await sendXec( | ||||
BCH, | BCH, | ||||
wallet, | wallet, | ||||
▲ Show 20 Lines • Show All 420 Lines • ▼ Show 20 Lines | describe('useBCH hook', () => { | ||||
it(`handleEncryptedOpReturn() correctly encrypts a message based on a valid cash address`, async () => { | it(`handleEncryptedOpReturn() correctly encrypts a message based on a valid cash address`, async () => { | ||||
const { handleEncryptedOpReturn } = useBCH(); | const { handleEncryptedOpReturn } = useBCH(); | ||||
const BCH = new BCHJS(); | const BCH = new BCHJS(); | ||||
const destinationAddress = | const destinationAddress = | ||||
'bitcoincash:qqvuj09f80sw9j7qru84ptxf0hyqffc38gstxfs5ru'; | 'bitcoincash:qqvuj09f80sw9j7qru84ptxf0hyqffc38gstxfs5ru'; | ||||
const message = | const message = | ||||
'This message is encrypted by ecies-lite with default parameters'; | 'This message is encrypted by ecies-lite with default parameters'; | ||||
const expectedPubKeyResponse = { | |||||
success: true, | |||||
publicKey: | |||||
'03208c4f52229e021ddec5fc6e07a59fd66388ac52bc2a2c1e0f1afb24b0e275ac', | |||||
}; | |||||
BCH.encryption.getPubKey = jest | |||||
.fn() | |||||
.mockResolvedValue(expectedPubKeyResponse); | |||||
const result = await handleEncryptedOpReturn( | const result = await handleEncryptedOpReturn( | ||||
BCH, | BCH, | ||||
destinationAddress, | destinationAddress, | ||||
Buffer.from(message), | Buffer.from(message), | ||||
); | ); | ||||
// loop through each ecies encryption parameter from the object returned from the handleEncryptedOpReturn() call | // loop through each ecies encryption parameter from the object returned from the handleEncryptedOpReturn() call | ||||
Show All 22 Lines | it(`handleEncryptedOpReturn() correctly encrypts a message based on a valid cash address`, async () => { | ||||
expect(result[k].toString()).toEqual( | expect(result[k].toString()).toEqual( | ||||
'F9KxuR48O0wxa9tFYq6/Hy3joI2edKxLFSeDVk6JKZE=', | 'F9KxuR48O0wxa9tFYq6/Hy3joI2edKxLFSeDVk6JKZE=', | ||||
); | ); | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
}); | }); | ||||
it(`handleEncryptedOpReturn() correctly throws error when attempting to encrypt a message based on an invalid cash address`, async () => { | |||||
const { handleEncryptedOpReturn } = useBCH(); | |||||
const BCH = new BCHJS(); | |||||
const destinationAddress = 'bitcoincash:qqvINVALIDADDRESSSSSSru'; | |||||
const message = | |||||
'This message is encrypted by ecies-lite with default parameters'; | |||||
const expectedError = { | |||||
error: `Unsupported address format : ${destinationAddress}`, | |||||
success: false, | |||||
}; | |||||
let thrownError; | |||||
try { | |||||
await handleEncryptedOpReturn( | |||||
BCH, | |||||
destinationAddress, | |||||
Buffer.from(message), | |||||
); | |||||
} catch (err) { | |||||
thrownError = err; | |||||
} | |||||
expect(thrownError).toStrictEqual(expectedError); | |||||
}); | |||||
it(`handleEncryptedOpReturn() correctly throws error when attempting to encrypt a message based on null cash address input`, async () => { | |||||
const { handleEncryptedOpReturn } = useBCH(); | |||||
const BCH = new BCHJS(); | |||||
const destinationAddress = null; | |||||
const message = | |||||
'This message is encrypted by ecies-lite with default parameters'; | |||||
const expectedError = 'Input must be a valid Bitcoin Cash address.'; | |||||
let thrownError; | |||||
try { | |||||
await handleEncryptedOpReturn( | |||||
BCH, | |||||
destinationAddress, | |||||
Buffer.from(message), | |||||
); | |||||
} catch (err) { | |||||
thrownError = err; | |||||
} | |||||
expect(thrownError).toStrictEqual(new Error(expectedError)); | |||||
}); | |||||
it(`getRecipientPublicKey() correctly retrieves the public key of a cash address`, async () => { | it(`getRecipientPublicKey() correctly retrieves the public key of a cash address`, async () => { | ||||
const { getRecipientPublicKey } = useBCH(); | const { getRecipientPublicKey } = useBCH(); | ||||
const BCH = new BCHJS(); | const BCH = new BCHJS(); | ||||
const expectedPubKeyResponse = { | expect( | ||||
success: true, | await getRecipientPublicKey( | ||||
publicKey: | BCH, | ||||
'bitcoincash:qqvuj09f80sw9j7qru84ptxf0hyqffc38gstxfs5ru', | |||||
), | |||||
).toStrictEqual( | |||||
'03208c4f52229e021ddec5fc6e07a59fd66388ac52bc2a2c1e0f1afb24b0e275ac', | '03208c4f52229e021ddec5fc6e07a59fd66388ac52bc2a2c1e0f1afb24b0e275ac', | ||||
}; | |||||
const expectedPubKey = | |||||
'03208c4f52229e021ddec5fc6e07a59fd66388ac52bc2a2c1e0f1afb24b0e275ac'; | |||||
const destinationAddress = | |||||
'bitcoincash:qqvuj09f80sw9j7qru84ptxf0hyqffc38gstxfs5ru'; | |||||
BCH.encryption.getPubKey = jest | |||||
.fn() | |||||
.mockResolvedValue(expectedPubKeyResponse); | |||||
expect(await getRecipientPublicKey(BCH, destinationAddress)).toBe( | |||||
expectedPubKey, | |||||
); | ); | ||||
}); | }); | ||||
it(`getRecipientPublicKey() correctly throws error for an invalid cash address`, async () => { | |||||
const { getRecipientPublicKey } = useBCH(); | |||||
const BCH = new BCHJS(); | |||||
const destinationAddress = 'bitcoincash:qqvuj0INVALIDDDDDDDDDDs5ru'; | |||||
const expectedError = { | |||||
error: `Unsupported address format : ${destinationAddress}`, | |||||
success: false, | |||||
}; | |||||
let thrownError; | |||||
try { | |||||
await getRecipientPublicKey(BCH, destinationAddress); | |||||
} catch (err) { | |||||
thrownError = err; | |||||
} | |||||
expect(thrownError).toStrictEqual(expectedError); | |||||
}); | |||||
it(`getRecipientPublicKey() correctly throws error for a null cash address input`, async () => { | |||||
const { getRecipientPublicKey } = useBCH(); | |||||
const BCH = new BCHJS(); | |||||
const destinationAddress = null; | |||||
const expectedError = 'Input must be a valid Bitcoin Cash address.'; | |||||
let thrownError; | |||||
try { | |||||
await getRecipientPublicKey(BCH, destinationAddress); | |||||
} catch (err) { | |||||
thrownError = err; | |||||
} | |||||
expect(thrownError).toStrictEqual(new Error(expectedError)); | |||||
}); | |||||
}); | }); |