This repository has been archived by the owner on Apr 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #99 from wibsonorg/WIB-894-exposes-addresses-by-ba…
…tpaId Endpoint for exposes addresses in a payment associated with a batPayId
- Loading branch information
Showing
19 changed files
with
212 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { sellersByPayIndex } from '../utils/stores'; | ||
import { BatPay } from '../blockchain/contracts'; | ||
import { packMessage } from '../utils/wibson-lib/cryptography'; | ||
import { decryptData } from '../services/signingService'; | ||
|
||
const ZERO_ACCOUNT = '0x0000000000000000000000000000000000000000'; | ||
|
||
const ERROR_INCOMPLETE_REGISTRATION = { | ||
message: 'The registration for this id is not completed still', | ||
code: 'registrationImcompleted', | ||
}; | ||
|
||
const ERROR_INVALID_BATPAY_ID = { | ||
message: 'Try with a valid id', | ||
code: 'invalidId', | ||
}; | ||
|
||
const ERROR_INVALID_SIGNATURE = { | ||
message: 'The signature is invalid or don\'t correspond to this batPayId', | ||
code: 'invalidSignature', | ||
}; | ||
|
||
/** | ||
* @async | ||
* @function getAddressesByBatPayId | ||
* @param {Object} [params] data payload needed in the operation. | ||
* @param {number} [params.batPayId] ID in BatPay. | ||
* @param {number} [params.payIndex] Payment index in BatPay. | ||
* @param {string} [params.signature] The signed of the owner of the BatPay ID. | ||
* @returns {Array} An array of the addresses. | ||
*/ | ||
export const getAddressesByBatPayId = async ({ payIndex, batPayId, signature }) => { | ||
try { | ||
const { owner } = await BatPay.methods.accounts(batPayId).call(); | ||
if (owner === ZERO_ACCOUNT) { | ||
return { error: ERROR_INCOMPLETE_REGISTRATION }; | ||
} | ||
try { | ||
const decrypted = await decryptData({ senderAddress: owner, encryptedData: signature }); | ||
if (decrypted !== packMessage(batPayId, payIndex)) { | ||
return { error: ERROR_INVALID_SIGNATURE }; | ||
} | ||
} catch (_e) { | ||
return { error: ERROR_INVALID_SIGNATURE }; | ||
} | ||
} catch (e) { | ||
return { error: ERROR_INVALID_BATPAY_ID }; | ||
} | ||
const sellers = await sellersByPayIndex.safeFetch(payIndex, {}); | ||
return { addresses: sellers[batPayId] || [] }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import td from 'testdouble'; | ||
import sinon from 'sinon'; | ||
import test from 'ava'; | ||
|
||
const fakeCall = { | ||
call: sinon.stub(), | ||
}; | ||
|
||
export const { BatPay } = td.replace('../../src/blockchain/contracts', { | ||
BatPay: { | ||
methods: { | ||
accounts: sinon.stub().returns(fakeCall), | ||
}, | ||
}, | ||
}); | ||
|
||
export const { packMessage } = td.replace( | ||
'../../src/utils/wibson-lib/cryptography', | ||
{ | ||
packMessage: sinon.stub().returns('someMessage'), | ||
}, | ||
); | ||
|
||
export const { decryptData } = td.replace('../../src/services/signingService', { | ||
decryptData: sinon.stub().resolves('someMessage'), | ||
}); | ||
|
||
export const { sellersByPayIndex } = td.replace('../../src/utils/stores', { | ||
sellersByPayIndex: { | ||
safeFetch: sinon | ||
.stub() | ||
.resolves({ | ||
8: ['0x075a22bc34b55322cabb0aa87d9e590e01b942c4'], | ||
9: ['0x075a22bc34b55322cabb0aa87d9e590e01b942c5'], | ||
10: ['0x075a22bc34b55322cabb0aa87d9e590e01b942c6'], | ||
}), | ||
}, | ||
}); | ||
|
||
test.beforeEach(() => { | ||
BatPay.methods.accounts().call.resolves({ owner: '0x051027180c70923729e8f090bba0d378bc949ce5' }); | ||
}); | ||
|
||
test.afterEach(sinon.resetHistory); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { serial as it } from 'ava'; | ||
import { decryptData, packMessage, BatPay } from './getAddressesByBatPayId.mock'; | ||
import { getAddressesByBatPayId } from '../../src/operations/getAddressesByBatPayId'; | ||
|
||
const params = { | ||
payIndex: 5, | ||
batPayId: 8, | ||
signature: '0x612a0e967efa6414fc1c2071ff3b9675a1554dd76cb6c17dfa892308fab97f41', | ||
publicKey: '12653256c4a94316da675b758537a7391b07ccbb31d85744081c769a353a50aa0a52e74e4326d3acfa5b8ffa55b5aee4b61ed99cce3c17cb5f3bd078060d9361', | ||
}; | ||
|
||
it('Exposes an array of addresses that receive a specific payment in a specific BatPay ID', async (assert) => { | ||
const result = await getAddressesByBatPayId(params); | ||
assert.is(await decryptData(), packMessage()); | ||
assert.deepEqual(result.addresses, ['0x075a22bc34b55322cabb0aa87d9e590e01b942c4']); | ||
}); | ||
|
||
it('Invalid Id', async (assert) => { | ||
BatPay.methods.accounts().call.rejects('error'); | ||
const { error } = await getAddressesByBatPayId(params); | ||
assert.is(error.code, 'invalidId'); | ||
}); | ||
|
||
it('When the resgistration id is not completed', async (assert) => { | ||
BatPay.methods.accounts().call.resolves({ owner: '0x0000000000000000000000000000000000000000' }); | ||
const { error } = await getAddressesByBatPayId(params); | ||
assert.is(error.code, 'registrationImcompleted'); | ||
}); | ||
|
||
it('When the signature is invalid or don\'t correspond to this batPayId', async (assert) => { | ||
decryptData.resolves('otherMessage'); | ||
const { error } = await getAddressesByBatPayId(params); | ||
assert.is(error.code, 'invalidSignature'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
-13 Bytes
(97%)
notary-api/test/queues/snapshots/notarizationsQueue.test.js.snap
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.