Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
gitmp01 committed Oct 31, 2024
1 parent 72e0be0 commit ee94349
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 68 deletions.
1 change: 1 addition & 0 deletions cpp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
},
"devDependencies": {
"@eosnetwork/vert": "^1.0.0",
"@pnetwork/event-attestator": "workspace:^",
"@types/ramda": "^0",
"@wharfkit/antelope": "^1.0.12",
"chai": "^4.3.10",
Expand Down
118 changes: 61 additions & 57 deletions cpp/test/adapter-local.test.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
const { expect } = require('chai')
const { Blockchain, expectToThrow, mintTokens } = require('@eosnetwork/vert')
const { Blockchain, expectToThrow } = require('@eosnetwork/vert')
const { deploy } = require('./utils/deploy')
const { Asset, Bytes, PublicKey } = require('@wharfkit/antelope')
const R = require('ramda')
const {
active,
precision,
getAccountCodeRaw,
getSymbolCodeRaw,
getSingletonInstance,
logExecutionTraces,
prettyTrace,
} = require('./utils/eos-ext')
const { getXbytesHex, hexToString, removeNullChars } = require('./utils/bytes-utils')
const {
getXbytesHex,
hexToString,
removeNullChars,
} = require('./utils/bytes-utils')
const { getEventBytes } = require('./utils/get-event-bytes')
const { substract, no0x } = require('./utils/wharfkit-ext')
const { substract } = require('./utils/wharfkit-ext')
const { getAccountsBalances } = require('./utils/get-token-balance')
const { getMetadataSample } = require('./utils/get-metadata-sample')
const { getOperationSample } = require('./utils/get-operation-sample')
const errors = require('./utils/errors')

const { no0x } = require('./utils')
const ethers = require('ethers')

const getSwapMemo = (sender, destinationChainId, recipient, data) =>
Expand Down Expand Up @@ -230,7 +230,7 @@ describe('Adapter EOS -> ETH testing', () => {
await token.contract.actions
.transfer([user, adapter.account, quantity, memo])
.send(active(user))

const after = getAccountsBalances(
[user, lockbox.account, adapter.account, feemanager],
[token, xerc20],
Expand Down Expand Up @@ -286,7 +286,7 @@ describe('Adapter EOS -> ETH testing', () => {
const eventBytes = getEventBytes(adapter.contract)
const expectedEventBytes =
'00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000746b6e2e746f6b656e00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000008a88f6dc465640000000000000000000000000000000000000000000000000000000000075736572000000000000000000000000000000000000000000000000000000000000002a307836386262656436613437313934656666316366353134623530656139313839353539376663393165'
expect(eventBytes).to.be.equal(expectedEventBytes)
expect(eventBytes).to.be.equal(expectedEventBytes)

offset = 0
const nonce = getXbytesHex(eventBytes, offset, 32)
Expand All @@ -301,11 +301,15 @@ describe('Adapter EOS -> ETH testing', () => {
offset += 32
const recipientLen = getXbytesHex(eventBytes, offset, 32)
offset += 32
const swapRecipient = getXbytesHex(eventBytes, offset, parseInt(recipientLen, 16))
const swapRecipient = getXbytesHex(
eventBytes,
offset,
parseInt(recipientLen, 16),
)
offset += parseInt(recipientLen, 16)
const userData = eventBytes.slice(offset * 2, eventBytes.length)

const expectedAmount = (parseInt(amount) - intFees) * (10 ** 18)
const expectedAmount = (parseInt(amount) - intFees) * 10 ** 18
expect(parseInt(nonce)).to.be.equal(before.storage.nonce)
expect(removeNullChars(hexToString(swapToken))).to.be.equal(token.account)
expect(destChainId).to.be.equal(destinationChainId.slice(2))
Expand Down Expand Up @@ -344,15 +348,15 @@ describe('Adapter EOS -> ETH testing', () => {
// 'hex',
// ),
// )
// const pubKey = PublicKey.from({ type: 'K1', compressed })
// const pubKey = PublicKey.from({ type: 'K1', compressed })
// await adapter.contract.actions
// .settee([pubKey, attestation])
// .send(active(adapter.account))
// const normalizedOriginChainId = hexStringToBytes('0000000000000000000000000000000000000000000000000000000000000001')

// const normalizedOriginChainId = hexStringToBytes('0000000000000000000000000000000000000000000000000000000000000001')
// const normalizedOriginAdapter = hexStringToBytes('000000000000000000000000cc9676b9bf25ce45a3a5f88205239afddecf1bc7')
// const normalizeTopicZero = hexStringToBytes('9b706941b48091a1c675b439064f40b9d43c577d9c7134cce93179b9b0bf2a52')

// await adapter.contract.actions
// .setemitter([normalizedOriginChainId, normalizedOriginAdapter])
// .send(active(adapter.account))
Expand Down Expand Up @@ -407,46 +411,46 @@ describe('Adapter EOS -> ETH testing', () => {
// )
// })

// it('Should send userdata to a receiver contract', async () => {
// const quantity = `1.0000 ${token.symbol}`
// const normalizedAmount = ethers
// .parseUnits(Asset.from(quantity).units.toString(), 18)
// .toString()

// const metadata = getMetadataSample()
// const operation = getOperationSample({
// amount: normalizedAmount,
// data: 'c0ffeec0ffeec0ffee',
// recipient: receiver.account,
// })

// const before = getAccountsBalances([receiver.account], [token, xerc20])

// // Fill in some tokens as collateral
// await token.contract.actions
// .transfer([user, lockbox.account, quantity, ''])
// .send(active(user))

// await adapter.contract.actions
// .settle([user, operation, metadata])
// .send(active(user))

// const after = getAccountsBalances([receiver.account], [token, xerc20])
// const receiverResults = receiver.contract.tables
// .results(getAccountCodeRaw(receiver.account))
// .getTableRow(0n)

// expect(
// substract(
// after[receiver.account][token.symbol],
// before[receiver.account][token.symbol],
// ).toString(),
// ).to.be.equal(quantity)

// expect(receiverResults).to.be.deep.equal({
// id: 0,
// data: operation.data,
// })
// })
// it('Should send userdata to a receiver contract', async () => {
// const quantity = `1.0000 ${token.symbol}`
// const normalizedAmount = ethers
// .parseUnits(Asset.from(quantity).units.toString(), 18)
// .toString()

// const metadata = getMetadataSample()
// const operation = getOperationSample({
// amount: normalizedAmount,
// data: 'c0ffeec0ffeec0ffee',
// recipient: receiver.account,
// })

// const before = getAccountsBalances([receiver.account], [token, xerc20])

// // Fill in some tokens as collateral
// await token.contract.actions
// .transfer([user, lockbox.account, quantity, ''])
// .send(active(user))

// await adapter.contract.actions
// .settle([user, operation, metadata])
// .send(active(user))

// const after = getAccountsBalances([receiver.account], [token, xerc20])
// const receiverResults = receiver.contract.tables
// .results(getAccountCodeRaw(receiver.account))
// .getTableRow(0n)

// expect(
// substract(
// after[receiver.account][token.symbol],
// before[receiver.account][token.symbol],
// ).toString(),
// ).to.be.equal(quantity)

// expect(receiverResults).to.be.deep.equal({
// id: 0,
// data: operation.data,
// })
// })
// })
})
138 changes: 127 additions & 11 deletions cpp/test/pam.test.js
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)
}
})
})
})
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2208,6 +2208,7 @@ __metadata:
resolution: "@pnetwork/eos-xerc20@workspace:cpp"
dependencies:
"@eosnetwork/vert": "npm:^1.0.0"
"@pnetwork/event-attestator": "workspace:^"
"@types/ramda": "npm:^0"
"@wharfkit/antelope": "npm:^1.0.12"
chai: "npm:^4.3.10"
Expand Down

0 comments on commit ee94349

Please sign in to comment.