-
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.
- Loading branch information
Showing
4 changed files
with
190 additions
and
68 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 |
---|---|---|
@@ -1,24 +1,140 @@ | ||
const { deploy } = require('./utils/deploy') | ||
const { Blockchain, expectToThrow } = require('@eosnetwork/vert') | ||
const { getMetadataSample } = require('./utils/get-metadata-sample') | ||
const { getOperationSample } = require('./utils/get-operation-sample') | ||
const { active } = require('./utils/eos-ext') | ||
const ethers = require('ethers') | ||
const { | ||
Chains, | ||
ProofcastEventAttestator, | ||
Protocols, | ||
Versions, | ||
} = require('@pnetwork/event-attestator') | ||
const { expect } = require('chai') | ||
const { zeroPadValue } = require('ethers') | ||
|
||
const { | ||
deploy, | ||
errors, | ||
getMetadataSample, | ||
getOperationSample, | ||
hexStringToBytes, | ||
hexToPublicKey, | ||
} = require('./utils') | ||
const { active, getSingletonInstance } = require('./utils/eos-ext') | ||
|
||
describe('PAM testing', () => { | ||
let pam | ||
const user = 'user' | ||
const name = 'pam' | ||
|
||
const pam = { | ||
account: 'pam', | ||
contract: null, | ||
} | ||
|
||
const adapter = { | ||
account: 'adapter', | ||
contract: null, | ||
} | ||
|
||
const privateKey = | ||
'dfcc79a57e91c42d7eea05f82a08bd1b7e77f30236bb7c56fe98d3366a1929c4' | ||
|
||
const ea = new ProofcastEventAttestator({ | ||
version: Versions.V1, | ||
protocolId: Protocols.Evm, | ||
chainId: Chains(Protocols.Evm).Mainnet, | ||
privateKey, | ||
}) | ||
|
||
console.log('pubkey', ea.publicKey) | ||
|
||
const pubKey = hexToPublicKey( | ||
'0380472f799469d9af8790307a022802785c2b1e2f9c0930bdf9bafe193245e7a3', | ||
) | ||
const evmEmitter = hexStringToBytes( | ||
zeroPadValue('0x5623D0aF4bfb6F7B18d6618C166d518E4357ceE2', 32), | ||
) | ||
const evmTopic0 = hexStringToBytes( | ||
'0x66756e6473206172652073616675207361667520736166752073616675202e2e', | ||
) | ||
|
||
const attestation = [] | ||
const blockchain = new Blockchain() | ||
const operation = getOperationSample() | ||
const metadata = getMetadataSample() | ||
|
||
before(async () => { | ||
blockchain.createAccounts(user) | ||
// pam = deploy(blockchain, name, 'contracts/build/test.pam') | ||
pam.contract = deploy(blockchain, pam.account, 'contracts/build/test.pam') | ||
adapter.contract = deploy( | ||
blockchain, | ||
adapter.account, | ||
'contracts/build/adapter', | ||
) | ||
}) | ||
describe('pam::isauthorized', () => { | ||
|
||
describe('pam::check_authorization', () => { | ||
it('Should set the adapter contract', async () => { | ||
await pam.contract.actions | ||
.setadapter([adapter.account]) | ||
.send(active(pam.account)) | ||
|
||
expect(getSingletonInstance(pam.contract, 'adapter')).to.be.equal( | ||
adapter.account, | ||
) | ||
}) | ||
|
||
it('Should reject when the public key is not set', async () => { | ||
const action = pam.contract.actions | ||
.isauthorized([operation, metadata]) | ||
.send(active(user)) | ||
|
||
await expectToThrow(action, errors.SINGLETON_NOT_EXISTING) | ||
}) | ||
|
||
it('Should reject when the origin_chain_id is not set', async () => { | ||
await adapter.contract.actions | ||
.settee([pubKey, attestation]) | ||
.send(active(adapter.account)) | ||
|
||
const action = pam.contract.actions | ||
.isauthorized([operation, metadata]) | ||
.send(active(user)) | ||
|
||
await expectToThrow(action, errors.ORIGIN_CHAINID_NOT_REGISTERED) | ||
}) | ||
|
||
it('Should reject if the signature is invalid', async () => { | ||
await adapter.contract.actions | ||
.setorigin([operation.originChainId, evmEmitter, evmTopic0]) | ||
.send(active(adapter.account)) | ||
|
||
const action = pam.contract.actions | ||
.isauthorized([operation, metadata]) | ||
.send(active(user)) | ||
|
||
await expectToThrow(action, errors.INVALID_SIGNATURE) | ||
}) | ||
|
||
it('Should authorize the operation successfully', async () => { | ||
const operation = getOperationSample() | ||
const metadata = getMetadataSample() | ||
const data = | ||
'0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000ea000000000000000000000000000000000000000000000000000000000000000000000000000000000000000051a240271ab8ab9f9a21c82d9a85396b704e164d0000000000000000000000000000000000000000000000000000000000007a6a00000000000000000000000000000000000000000000000000000000000026fc0000000000000000000000002b5ad5c4795c026514f8317c7a215e218dccd6cf000000000000000000000000000000000000000000000000000000000000002a30783638313345623933363233373245454636323030663362316462433366383139363731634241363900000000000000000000000000000000000000000000' | ||
const event = { | ||
blockHash: operation.blockId, | ||
transactionHash: operation.txId, | ||
address: evmEmitter, | ||
topics: [evmTopic0, ethers.zeroPadValue('0x', 32)], | ||
data, | ||
} | ||
|
||
const signature = ea.sign(event) | ||
const preimage = ea.getEventPreImage(event) | ||
|
||
const metadata = getMetadataSample({ signature, preimage }) | ||
|
||
// await pam.actions.isauthorized([operation, metadata]).send(active(user)) | ||
try { | ||
await pam.contract.actions | ||
.isauthorized([operation, metadata]) | ||
.send(active(user)) | ||
} finally { | ||
console.log(pam.contract.bc.console) | ||
} | ||
}) | ||
}) | ||
}) |
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