Skip to content

Commit

Permalink
Add test vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
ly0va committed Jun 4, 2021
1 parent 2e9f509 commit 4764e3a
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 29 deletions.
102 changes: 102 additions & 0 deletions etc/test_config/sdk/test-vectors.json
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,108 @@
"ethSignMessage": "0x57697468647261774e46542031303030303020746f3a203078313961613265643837313230373265393138363332323539373830653538373639386566353864660a4665653a20313030303030302e30204554480a4e6f6e63653a203132",
"ethSignature": "0x4a50341da6d2b1f0b64a4e37f753c02c43623e89cb0a291026c37fdcc723da9665453ce622f4dd6237bd98430ef0d75755694b1968f3b2d0ea8598f8bc43accf1b"
}
},
{
"inputs": {
"type": "Order",
"ethPrivateKey": "0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
"data": {
"accountId": 6,
"nonce": 18,
"tokenSell": 0,
"tokenBuy": 2,
"ratio": [
"1",
"2"
],
"amount": "1000000",
"recipient": "0x823b6a996cea19e0c41e250b20e2e804ea72ccdf",
"validFrom": 0,
"validUntil": 4294967295
},
"ethSignData": {
"tokenSell": "ETH",
"tokenBuy": "DAI",
"recipient": "0x823b6a996cea19e0c41e250b20e2e804ea72ccdf",
"amount": "1000.0",
"nonce": 18,
"ratio": [
"1",
"2"
]
}
},
"outputs": {
"signBytes": "0x6f0100000006823b6a996cea19e0c41e250b20e2e804ea72ccdf0000001200000000000000020000000000000000000000000000010000000000000000000000000000020001e84800000000000000000000000000ffffffff",
"signature": {
"pubKey": "40771354dc314593e071eaf4d0f42ccb1fad6c7006c57464feeb7ab5872b7490",
"signature": "b76c83011ea9e14cf679d35b9a7084832a78bf3f975c5b5c3315f80993c227afb7a1cd7e7b8fc225a48d8c9be78335736115890df5bbacfc52ecf47b4e089500"
},
"ethSignMessage": "0x4f7264657220666f7220313030302e3020455448202d3e204441490a526174696f3a20313a320a416464726573733a203078383233623661393936636561313965306334316532353062323065326538303465613732636364660a4e6f6e63653a203138",
"ethSignature": "0x841a4ed62572883b2272a56164eb33f7b0649029ba588a7230928cff698b49383045b47d35dcdee1beb33dd4ca6b944b945314a206f3f2838ddbe389a34fc8cb1c"
}
},
{
"inputs": {
"type": "Swap",
"ethPrivateKey": "0x000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f",
"data": {
"type": "Swap",
"orders": [
{
"accountId": 6,
"nonce": 18,
"tokenSell": 1,
"tokenBuy": 2,
"ratio": [
"1",
"2"
],
"amount": "1000000",
"recipient": "0x823b6a996cea19e0c41e250b20e2e804ea72ccdf",
"validFrom": 0,
"validUntil": 4294967295
},
{
"accountId": 44,
"nonce": 101,
"tokenSell": 2,
"tokenBuy": 1,
"ratio": [
"3",
"1"
],
"amount": "2500000",
"recipient": "0x63adbb48d1bc2cf54562910ce54b7ca06b87f319",
"validFrom": 0,
"validUntil": 4294967295
}
],
"nonce": 1,
"amounts": [
"1000000",
"2500000"
],
"submitterId": 5,
"submitterAddress": "0xedE35562d3555e61120a151B3c8e8e91d83a378a",
"feeToken": 3,
"fee": "123"
},
"ethSignData": {
"fee": "12.3",
"feeToken": "USDT",
"nonce": 1
}
},
"outputs": {
"signBytes": "0xf40100000005ede35562d3555e61120a151b3c8e8e91d83a378a000000017b1e76f6f124bae1917435a02cfbf5571d79ddb8380bc4bf4858c9e9969487000000030f600001e848000004c4b400",
"signature": {
"pubKey": "40771354dc314593e071eaf4d0f42ccb1fad6c7006c57464feeb7ab5872b7490",
"signature": "c13aabacf96448efb47763554753bfe2acc303a8297c8af59e718d685d422a901a43c42448f95cca632821df1ccb754950196e8444c0acef253c42c1578b5401"
},
"ethSignMessage": "0x53776170206665653a2031322e3320555344540a4e6f6e63653a2031",
"ethSignature": "0x3a459b40838e9445adc59e0cba4bf769b68deda8dadfedfe415f9e8be1c55443090f66cfbd13d96019b9faafb996a5a69d1bc0d1061f08ebf7cb8a1687e09a0f1c"
}
}
]
},
Expand Down
2 changes: 1 addition & 1 deletion infrastructure/reading-tool/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function loadTestVectorsConfig() {

export function getTokens(network: string) {
const configPath = `${process.env.ZKSYNC_HOME}/etc/tokens/${network}.json`;
console.log(configPath);
// console.log(configPath);
return JSON.parse(
fs.readFileSync(configPath, {
encoding: 'utf-8'
Expand Down
3 changes: 1 addition & 2 deletions sdk/zksync.js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"tests": "mocha -r ts-node/register tests/**/*.test.ts",
"build": "sh build.sh",
"watch": "tsc --watch",
"prepublish": "yarn build && rollup -c",
"generate-test-vectors": "yarn ts-node tests/test-generator.ts"
"prepublish": "yarn build && rollup -c"
}
}
98 changes: 72 additions & 26 deletions sdk/zksync.js/tests/vectors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ import { loadTestVectorsConfig } from 'reading-tool';
import { MintNFT, WithdrawNFT } from '../src/types';

const vectors = loadTestVectorsConfig();
const cryptoPrimitivesVectors = vectors['cryptoPrimitivesTest'];
const utilsVectors = vectors['utils'];
const txVectors = vectors['txTest'];
const txHashVectors = vectors['txHashTest'];

describe(cryptoPrimitivesVectors['description'], function () {
it('Keys and signatures', async function () {
for (const item of cryptoPrimitivesVectors['items']) {
const cryptoVectors = vectors.cryptoPrimitivesTest;
const utilsVectors = vectors.utils;
const txVectors = vectors.txTest;
const txHashVectors = vectors.txHashTest;

describe('Crypto tests', function () {
it(cryptoVectors.description, async function () {
for (const item of cryptoVectors.items) {
const seed = parseHexWithPrefix(item.inputs.seed);
const privateKey = await privateKeyFromSeed(seed);
const message = parseHexWithPrefix(item.inputs.message);
Expand All @@ -40,10 +40,13 @@ describe(cryptoPrimitivesVectors['description'], function () {
});
});

const amountPackingVectors = utilsVectors['amountPacking'];
describe(amountPackingVectors['description'], function () {
it('Token packing', function () {
for (const item of amountPackingVectors['items']) {
const amountPackingVectors = utilsVectors.amountPacking;
const feePackingVectors = utilsVectors.feePacking;
const tokenFormattingVectors = utilsVectors.tokenFormatting;

describe('Utils tests', function () {
it(amountPackingVectors.description, function () {
for (const item of amountPackingVectors.items) {
const tokenAmount = BigNumber.from(item.inputs.value);

assert.equal(
Expand All @@ -64,12 +67,9 @@ describe(amountPackingVectors['description'], function () {
}
}
});
});

const feePackingVectors = utilsVectors['feePacking'];
describe(feePackingVectors['description'], function () {
it('Fee packing', function () {
for (const item of feePackingVectors['items']) {
it(feePackingVectors.description, function () {
for (const item of feePackingVectors.items) {
const feeAmount = BigNumber.from(item.inputs.value);

assert.equal(isTransactionFeePackable(feeAmount), item.outputs.packable, `Fee '${feeAmount}' not packable`);
Expand All @@ -86,13 +86,10 @@ describe(feePackingVectors['description'], function () {
}
}
});
});

const tokenFormattingVectors = utilsVectors['tokenFormatting'];
describe(tokenFormattingVectors['description'], function () {
const tokens = {};
let id = 0;
for (const item of tokenFormattingVectors['items']) {
for (const item of tokenFormattingVectors.items) {
const token = item.inputs.token;
tokens[token] = {
address: '0x0000000000000000000000000000000000000000',
Expand All @@ -103,20 +100,19 @@ describe(tokenFormattingVectors['description'], function () {
id++;
}

it('Formatting tokens', function () {
it(tokenFormattingVectors.description, function () {
const tokenCache = new TokenSet(tokens);

for (const item of tokenFormattingVectors['items']) {
for (const item of tokenFormattingVectors.items) {
const unitsStr = tokenCache.formatToken(item.inputs.token, item.inputs.amount);
expect(`${unitsStr} ${item.inputs.token}`).to.eql(item.outputs.formatted);
}
});
});

describe(txVectors['description'], function () {
describe(txVectors.description, function () {
async function getSigner(ethPrivateKey) {
const ethWallet = new Wallet(ethPrivateKey);
//const fromAddress = ethWallet.address;
const { signer } = await zksync.Signer.fromETHSignature(ethWallet);
const ethMessageSigner = new zksync.EthMessageSigner(ethWallet, {
verificationMethod: 'ECDSA',
Expand Down Expand Up @@ -151,6 +147,56 @@ describe(txVectors['description'], function () {
}
});

it('Order signature', async function () {
for (const item of txVectors.items) {
const { type: txType, ethPrivateKey, data: order, ethSignData } = item.inputs;
const expected = item.outputs;
const privateKey = parseHexWithPrefix(ethPrivateKey);
const { signer, ethMessageSigner } = await getSigner(privateKey);

if (txType === 'Order') {
const signBytes = zksync.utils.serializeOrder(order);
const { signature } = await signer.signSyncOrder(order);

const { signature: ethSignature } = await ethMessageSigner.ethSignOrder(ethSignData);
const ethSignMessage = ethMessageSigner.getOrderEthSignMessage(ethSignData);

expect(utils.hexlify(signBytes)).to.eql(expected.signBytes, 'Sign bytes do not match');
expect(signature).to.eql(expected.signature, 'Signature does not match');
expect(ethSignature).to.eql(expected.ethSignature, 'Ethereum signature does not match');
expect(utils.hexlify(utils.toUtf8Bytes(ethSignMessage))).to.eql(
expected.ethSignMessage,
'Ethereum signature message does not match'
);
}
}
});

it('Swap signature', async function () {
for (const item of txVectors.items) {
const { type: txType, ethPrivateKey, data: order, ethSignData } = item.inputs;
const expected = item.outputs;
const privateKey = parseHexWithPrefix(ethPrivateKey);
const { signer, ethMessageSigner } = await getSigner(privateKey);

if (txType === 'Swap') {
const signBytes = await zksync.utils.serializeSwap(order);
const { signature } = await signer.signSyncSwap(order);

const { signature: ethSignature } = await ethMessageSigner.ethSignSwap(ethSignData);
const ethSignMessage = ethMessageSigner.getSwapEthSignMessage(ethSignData);

expect(utils.hexlify(signBytes)).to.eql(expected.signBytes, 'Sign bytes do not match');
expect(signature).to.eql(expected.signature, 'Signature does not match');
expect(ethSignature).to.eql(expected.ethSignature, 'Ethereum signature does not match');
expect(utils.hexlify(utils.toUtf8Bytes(ethSignMessage))).to.eql(
expected.ethSignMessage,
'Ethereum signature message does not match'
);
}
}
});

it('ChangePubKey signature', async function () {
for (const item of txVectors.items) {
const { type: txType, ethPrivateKey, data: changePubKeyData, ethSignData } = item.inputs;
Expand Down Expand Up @@ -280,7 +326,7 @@ describe(txVectors['description'], function () {
});
});

describe(txHashVectors['description'], function () {
describe(txHashVectors.description, function () {
it('Transaction hash', async function () {
for (const item of txHashVectors.items) {
const tx = item.inputs.tx;
Expand Down

0 comments on commit 4764e3a

Please sign in to comment.