1234567891011121314151617import type {IKeyringPair} from '@polkadot/types/types';1819import {expect, itEth, usingEthPlaygrounds} from '@unique/test-utils/eth/util.js';2021describe('Precompiles', () => {22 let donor: IKeyringPair;2324 before(async function() {25 await usingEthPlaygrounds(async (_, privateKey) => {26 donor = await privateKey({url: import.meta.url});27 });28 });2930 itEth('ecrecover is supported', async ({helper}) => {31 const owner = await helper.eth.createAccountWithBalance(donor);32 const ecrecoverCompiledСontract = await helper.ethContract.compile(33 'ECRECOVER',34 `35 // SPDX-License-Identifier: MIT36 pragma solidity ^0.8.17;3738 contract ECRECOVER{39 address addressTest = 0x12Cb274aAD8251C875c0bf6872b67d9983E53fDd;40 bytes32 msgHash1 = 0xc51dac836bc7841a01c4b631fa620904fc8724d7f9f1d3c420f0e02adf229d50;41 bytes32 msgHash2 = 0xc51dac836bc7841a01c4b631fa620904fc8724d7f9f1d3c420f0e02adf229d51;42 uint8 v = 0x1b;43 bytes32 r = 0x44287513919034a471a7dc2b2ed121f95984ae23b20f9637ba8dff471b6719ef;44 bytes32 s = 0x7d7dc30309a3baffbfd9342b97d0e804092c0aeb5821319aa732bc09146eafb4;454647 function verifyValid() public view returns(bool) {48 // Use ECRECOVER to verify address49 return ecrecover(msgHash1, v, r, s) == (addressTest);50 }5152 function verifyInvalid() public view returns(bool) {53 // Use ECRECOVER to verify address54 return ecrecover(msgHash2, v, r, s) == (addressTest);55 }56 }57 `,58 );5960 const ecrecoverСontract = await helper.ethContract.deployByAbi(owner, ecrecoverCompiledСontract.abi, ecrecoverCompiledСontract.object);61 expect(await ecrecoverСontract.methods.verifyValid().call({from: owner})).to.be.true;62 expect(await ecrecoverСontract.methods.verifyInvalid().call({from: owner})).to.be.false;63 });6465 itEth('sr25519 is supported', async ({helper}) => {66 const owner = await helper.eth.createAccountWithBalance(donor);67 const sr25519CompiledСontract = await helper.ethContract.compile(68 'SR25519Contract',69 `70 // SPDX-License-Identifier: MIT71 pragma solidity ^0.8.17;7273 /**74 * @title SR25519 signature interface.75 */76 interface SR25519 {77 /**78 * @dev Verify signed message using SR25519 crypto.79 * @return A boolean confirming whether the public key is signer for the message. 80 */81 function verify(82 bytes32 public_key,83 bytes calldata signature,84 bytes calldata message85 ) external view returns (bool);86 }8788 contract SR25519Contract{89 SR25519 public constant sr25519 = SR25519(0x0000000000000000000000000000000000005002);9091 bytes32 public_key = 0x96b2f9237ed0890fbeed891ebb81b91ac0d5d5b6e3afcdbc95df1b68d9f14036;92 bytes signature = hex"4a5d733d7c568f2e88abf0467fd497f87c1be3e940ed897efdf9da72eaad394ef9918cb574ee99c54485775b17a0deaf46ff7a1f10346cea39fff0e4ede97689";93 bytes message1 = hex"7372323535313920697320737570706f72746564";94 bytes message2 = hex"7372323535313920697320737570706f7274656401";959697 function verifyValid() public view returns(bool) {98 return sr25519.verify(public_key, signature, message1);99 }100101 function verifyInvalid() public view returns(bool) {102 return sr25519.verify(public_key, signature, message2);103 }104 }105 `,106 );107108 const sr25519Сontract = await helper.ethContract.deployByAbi(owner, sr25519CompiledСontract.abi, sr25519CompiledСontract.object);109 expect(await sr25519Сontract.methods.verifyValid().call({from: owner})).to.be.true;110 expect(await sr25519Сontract.methods.verifyInvalid().call({from: owner})).to.be.false;111 });112});