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 8afd1e2 commit 4a910ab
Show file tree
Hide file tree
Showing 10 changed files with 324 additions and 83 deletions.
3 changes: 1 addition & 2 deletions cpp/contracts/adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,7 @@ void adapter::settle(const name& caller, const operation& operation, const metad
auto search_token_bytes = idx_registry.find(operation.token);
check(search_token_bytes != idx_registry.end(), "invalid token");

checksum256 event_id = sha256((const char*)metadata.preimage.data(), metadata.preimage.size());

checksum256 event_id; // output
pam::check_authorization(get_self(), operation, metadata, event_id);

past_events _past_events(get_self(), get_self().value);
Expand Down
19 changes: 13 additions & 6 deletions cpp/contracts/pam.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#pragma once

#include "metadata.hpp"
#include "operation.hpp"
#include <eosio/crypto.hpp>
#include <eosio/singleton.hpp>

#include "utils.hpp"
#include "metadata.hpp"
#include "operation.hpp"

namespace eosio {
using bytes = std::vector<uint8_t>;
Expand Down Expand Up @@ -64,7 +64,7 @@ namespace eosio {
return true;
}

void check_authorization(name adapter, const operation& operation, const metadata& metadata, checksum256 event_id) {
void check_authorization(name adapter, const operation& operation, const metadata& metadata, checksum256& event_id) {
check(context_checks(operation, metadata), "unexpected context");

tee_pubkey _tee_pubkey(adapter, adapter.value);
Expand All @@ -78,18 +78,21 @@ namespace eosio {
bytes exp_emitter = itr_mappings->emitter;
bytes exp_topic_zero = itr_mappings->topic_zero;

event_id = sha256((const char*)metadata.preimage.data(), metadata.preimage.size());


signature sig = convert_bytes_to_signature(metadata.signature);
public_key recovered_pubkey = recover_key(event_id, sig);
check(recovered_pubkey == tee_key, "invalid signature");

offset = 0;
bytes event_payload(metadata.preimage.begin() + 98, metadata.preimage.end());
bytes emitter = extract_32bytes(event_payload, offset);
check(emitter == exp_emitter && !is_all_zeros(emitter), "unexpected Emitter");
check(emitter == exp_emitter && !is_all_zeros(emitter), "unexpected emitter");
offset += 32;

bytes topic_zero = extract_32bytes(event_payload, offset);
check(topic_zero == exp_topic_zero && !is_all_zeros(topic_zero), "unexpected topic Zero");
check(topic_zero == exp_topic_zero && !is_all_zeros(topic_zero), "unexpected topic zero");
offset += 32 * 3; // skip other topics

// check nonce
Expand All @@ -101,12 +104,16 @@ namespace eosio {

// check origin token
bytes token = extract_32bytes(event_data, offset);
print("\ntoken\n");
printhex(token.data(), token.size());
checksum256 token_hash = bytes32_to_checksum256(token);
check(operation.token == token_hash, "token adddress do not match");
check(operation.token == token_hash, "token address do not match");
offset += 32;

// check destination chain id
bytes dest_chain_id = extract_32bytes(event_data, offset);
print("\ndest_chain_id\n");
printhex(dest_chain_id.data(), dest_chain_id.size());
check(operation.destinationChainId == dest_chain_id, "destination chain id does not match with the expected one");
check(CHAIN_ID == dest_chain_id, "destination chain id does not match with the current chain");
offset += 32;
Expand Down
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,
// })
// })
// })
})
Loading

0 comments on commit 4a910ab

Please sign in to comment.