-
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.
feat: replace attestor handler with attestor functions
- Loading branch information
1 parent
6a2bbda
commit 8c3174a
Showing
7 changed files
with
164 additions
and
77 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 was deleted.
Oops, something went wrong.
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,83 @@ | ||
import { isEmpty, join } from 'ramda'; | ||
import { | ||
FundingTXAttestorInfo, | ||
WithdrawDepositTXAttestorInfo, | ||
} from 'src/models/attestor.models.js'; | ||
import { AttestorError } from 'src/models/errors.js'; | ||
|
||
export async function sendRequest(url: string, body: string): Promise<boolean | string> { | ||
const response = await fetch(url, { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' }, | ||
body, | ||
}); | ||
if (!response.ok) { | ||
throw new AttestorError(`Attestor Response ${url} was not OK: ${response.statusText}`); | ||
} | ||
return true; | ||
} | ||
|
||
export async function submitFundingPSBT( | ||
attestorRootURLs: string[], | ||
fundingTXAttestorInfo: FundingTXAttestorInfo | ||
): Promise<void> { | ||
if (isEmpty(attestorRootURLs)) { | ||
throw new AttestorError('No Attestor URLs provided'); | ||
} | ||
|
||
const fundingEndpoints = attestorRootURLs.map(url => `${url}/app/create-psbt-event`); | ||
|
||
const body = JSON.stringify({ | ||
uuid: fundingTXAttestorInfo.vaultUUID, | ||
funding_transaction_psbt: fundingTXAttestorInfo.fundingPSBT, | ||
mint_address: fundingTXAttestorInfo.userEthereumAddress, | ||
chain: fundingTXAttestorInfo.attestorChainID, | ||
alice_pubkey: fundingTXAttestorInfo.userBitcoinTaprootPublicKey, | ||
}); | ||
|
||
const attestorResponses: (boolean | string)[] = await Promise.all( | ||
fundingEndpoints.map(async url => | ||
sendRequest(url, body) | ||
.then(response => response) | ||
.catch(error => error.message) | ||
) | ||
); | ||
|
||
if (attestorResponses.every(response => response !== true)) { | ||
throw new AttestorError( | ||
`Error sending [Funding] Transaction to Attestors: | ||
${join('|', attestorResponses)}` | ||
); | ||
} | ||
} | ||
|
||
export async function submitWithdrawDepositPSBT( | ||
attestorRootURLs: string[], | ||
withdrawDepositTXAttestorInfo: WithdrawDepositTXAttestorInfo | ||
): Promise<void> { | ||
if (isEmpty(attestorRootURLs)) { | ||
throw new AttestorError('No Attestor URLs provided'); | ||
} | ||
|
||
const depositWithdrawEndpoints = attestorRootURLs.map(url => `${url}/app/withdraw`); | ||
|
||
const body = JSON.stringify({ | ||
uuid: withdrawDepositTXAttestorInfo.vaultUUID, | ||
wd_psbt: withdrawDepositTXAttestorInfo.depositWithdrawPSBT, | ||
}); | ||
|
||
const attestorResponses: (boolean | string)[] = await Promise.all( | ||
depositWithdrawEndpoints.map(async url => | ||
sendRequest(url, body) | ||
.then(response => response) | ||
.catch(error => error.message) | ||
) | ||
); | ||
|
||
if (attestorResponses.every(response => response !== true)) { | ||
throw new AttestorError( | ||
`Error sending [Deposit/Withdraw] Transaction to Attestors: | ||
${join('|', attestorResponses)}` | ||
); | ||
} | ||
} |
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,4 @@ | ||
export { | ||
submitFundingPSBT, | ||
submitWithdrawDepositPSBT, | ||
} from '../attestor/attestor-request.functions.js'; |
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 * as attestorRequestFunctions from '../../src/functions/attestor/attestor-request.functions.js'; | ||
import { submitFundingPSBT } from '../../src/functions/attestor/attestor-request.functions.js'; | ||
|
||
describe('Attestor ', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
describe('submitFundingPSBT', () => { | ||
it('should not throw an error if all requests were succesful', async () => { | ||
jest | ||
.spyOn(attestorRequestFunctions, 'sendRequest') | ||
.mockImplementationOnce(async () => true) | ||
.mockImplementationOnce(async () => true) | ||
.mockImplementationOnce(async () => true); | ||
|
||
await expect( | ||
submitFundingPSBT( | ||
['http://localhost:3000', 'http://localhost:4000', 'http://localhost:5000'], | ||
{ | ||
vaultUUID: 'vaultUUID', | ||
fundingPSBT: 'fundingPSBT', | ||
userEthereumAddress: 'userEthereumAddress', | ||
attestorChainID: 'evm-arbitrum', | ||
userBitcoinTaprootPublicKey: 'userBitcoinTaprootPublicKey', | ||
} | ||
) | ||
).resolves.not.toThrow(); | ||
}); | ||
|
||
it('should not throw an error if not all requests were succesful', async () => { | ||
jest | ||
.spyOn(attestorRequestFunctions, 'sendRequest') | ||
.mockImplementationOnce(async () => true) | ||
.mockImplementationOnce(async () => true) | ||
.mockImplementationOnce(async () => false); | ||
|
||
await expect( | ||
submitFundingPSBT( | ||
['http://localhost:3000', 'http://localhost:4000', 'http://localhost:5000'], | ||
{ | ||
vaultUUID: 'vaultUUID', | ||
fundingPSBT: 'fundingPSBT', | ||
userEthereumAddress: 'userEthereumAddress', | ||
attestorChainID: 'evm-arbitrum', | ||
userBitcoinTaprootPublicKey: 'userBitcoinTaprootPublicKey', | ||
} | ||
) | ||
).resolves.not.toThrow(); | ||
}); | ||
}); | ||
}); |
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