forked from purefiprotocol/sdk-solidity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverifierV3Test.ts
264 lines (216 loc) · 11.6 KB
/
verifierV3Test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import { expect } from "chai";
import hre, { ethers } from "hardhat";
import { utils } from "ethers";
import { ContextTestContract, PureFiIssuerRegistry, PureFiVerifier } from "../typechain-types";
import { BigNumber } from "ethers";
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
import { arrayify, Bytes, defaultAbiCoder, keccak256, ParamType, parseEther, recoverAddress, solidityPack, toUtf8Bytes } from "ethers/lib/utils";
import EthCrypto, { publicKeyByPrivateKey, sign, util } from 'eth-crypto';
import { VerificationPackageStruct } from "../typechain-types/contracts/PureFiVerifier";
const ADDRESS_STUB = "0x0000000000000000000000000000000000001230";
const NULL_ADDRESS = "0x0000000000000000000000000000000000000000"
const DEFAULT_AML_RULE = 100;
const DEFAULT_KYC_RULE = 200;
const DEFAULT_AML_KYC_RULE = 300;
describe("VerifierV3", function () {
let admin: SignerWithAddress;
let alice: SignerWithAddress;
// let bob: SignerWithAddress;
// let carl: SignerWithAddress;
let verifier: PureFiVerifier;
let issuerRegistry : PureFiIssuerRegistry;
let test : ContextTestContract;
const currentTimestamp = BigNumber.from(1664541916 + 123456789);
const ruleId = 431017;
const sessionId = 123321;
const privateKey = "e3ad95aa7e9678e96fb3d867c789e765db97f9d2018fca4068979df0832a5178";
const signerIdentity = {
privateKey : privateKey,
publicKey : EthCrypto.publicKeyByPrivateKey(privateKey),
address : EthCrypto.publicKey.toAddress(publicKeyByPrivateKey(privateKey))
};
before(async () => {
[admin, alice] = await ethers.getSigners();
const VERIFIER = await hre.ethers.getContractFactory("PureFiVerifier");
const ISSUER_REGISTRY = await hre.ethers.getContractFactory("PureFiIssuerRegistry");
const CONTEXT_TEST = await hre.ethers.getContractFactory("ContextTestContract");
issuerRegistry = await ISSUER_REGISTRY.deploy();
await issuerRegistry.deployed();
issuerRegistry.initialize(admin.address);
verifier = await VERIFIER.deploy();
await verifier.deployed();
test = await CONTEXT_TEST.deploy(verifier.address);
await test.deployed();
await verifier.initialize(issuerRegistry.address, ADDRESS_STUB);
const proof = utils.arrayify("0x0000000000000000000000000000000000000000000000000000000000000123");
await issuerRegistry.register(signerIdentity.address, proof);
await verifier.setUint256(3, 300);
await verifier.setUint256(4, DEFAULT_AML_RULE);
await verifier.setUint256(5, DEFAULT_KYC_RULE);
await verifier.setUint256(6, DEFAULT_AML_KYC_RULE);
});
it("test validatePureFiData", async function () {
//purefipackage
const packageType = 1;
// packagedata
const sender = signerIdentity.address;
// rule id
const ruleId = 100;
const timestamp = currentTimestamp;
const sessionId = 1234;
const purefiPackage = defaultAbiCoder.encode(["uint8", "uint256", "uint256", "address"], [packageType, ruleId, sessionId, sender]);
const message = solidityPack(["uint64", "bytes"], [timestamp, purefiPackage]);
const hash = keccak256(message);
const signature = sign(signerIdentity.privateKey, hash);
const purefiData = defaultAbiCoder.encode(["uint64", "bytes", "bytes"], [ timestamp, signature, purefiPackage ]);
// console.log("Result : \n", await verifier.validatePureFiData(purefiData));
const [infoBytes, statusCode] = await verifier.validatePureFiData(purefiData);
expect(statusCode).eq(0);
const info = await verifier.decodePureFiPackage(infoBytes);
expect(info[0]).eq(1);
expect(info[1]).eq(BigNumber.from(sessionId));
expect(info[2]).eq(BigNumber.from(ruleId));
expect(info[3]).eq(sender);
expect(info[4]).eq(NULL_ADDRESS);
expect(info[5]).eq(NULL_ADDRESS);
expect(info[6]).eq(BigNumber.from(0));
expect(info[7]).eq("0x");
});
it("test decodePureFiPackage type 2", async function (){
const type = 2;
const sender = signerIdentity.address;
const receiver = alice.address;
const token = ADDRESS_STUB;
const amount = BigNumber.from(1000000000000);
const packageToDecode = utils.defaultAbiCoder.encode(
["uint8", "uint256", "uint256", "address", "address", "address", "uint256"],
[type, ruleId, sessionId, sender, receiver, token, amount]
);
const info = await verifier.decodePureFiPackage(packageToDecode);
expect(info[0]).eq(2);
expect(info[1]).eq(BigNumber.from(sessionId));
expect(info[2]).eq(BigNumber.from(ruleId));
expect(info[3]).eq(sender);
expect(info[4]).eq(receiver);
expect(info[5]).eq(token);
expect(info[6]).eq(amount);
expect(info[7]).eq("0x");
});
it("test decodePureFiPackage type 3", async function (){
const type = 3;
const payload = defaultAbiCoder.encode(["uint256"], [456]);
const sender = signerIdentity.address;
const packageToDecode = utils.defaultAbiCoder.encode(
["uint8", "uint256", "uint256", "address", "bytes"],
[type, ruleId, sessionId, sender, payload]
);
const info = await verifier.decodePureFiPackage(packageToDecode);
expect(info[0]).eq(3);
expect(info[1]).eq(BigNumber.from(sessionId));
expect(info[2]).eq(BigNumber.from(ruleId));
expect(info[3]).eq(sender);
expect(info[4]).eq(NULL_ADDRESS);
expect(info[5]).eq(NULL_ADDRESS);
expect(info[6]).eq(BigNumber.from(0));
expect(info[7]).eq(payload);
});
it("test PureFiContext withPureFiContext modifier", async function () {
{
const type = 1;
const sender = signerIdentity.address;
const ruleId = 100;
const purefiPackage = defaultAbiCoder.encode(["uint8", "uint256", "uint256", "address"], [type, ruleId, sessionId, sender]);
const message = solidityPack(["uint64", "bytes"], [currentTimestamp, purefiPackage]);
const hash = keccak256(message);
const signature = sign(signerIdentity.privateKey, hash);
const purefiData = defaultAbiCoder.encode(["uint64", "bytes", "bytes"], [ currentTimestamp, signature, purefiPackage ]);
await test.funcWithPureFiContext(purefiData);
// assert with hardoced value
expect(await test.getCounter()).eq(100);
}
{
const type = 2;
const sender = signerIdentity.address;
const ruleId = 100;
const receiver = alice.address;
const token = ADDRESS_STUB;
const amount = BigNumber.from(1000000000000);
const purefiPackage = defaultAbiCoder.encode(
["uint8", "uint256", "uint256", "address", "address", "address", "uint256" ],
[type, ruleId, sessionId, sender, receiver, token, amount]
);
const message = solidityPack(["uint64", "bytes"], [currentTimestamp, purefiPackage]);
const hash = keccak256(message);
const signature = sign(signerIdentity.privateKey, hash);
const purefiData = defaultAbiCoder.encode(["uint64", "bytes", "bytes"], [currentTimestamp, signature, purefiPackage]);
// console.log("Res type 2: \n", (await test.funcWithPureFiContext(purefiData)).data );
await test.funcWithPureFiContext(purefiData)
}
{
const type = 3;
const ruleId = 100;
const sender = signerIdentity.address;
const payload = defaultAbiCoder.encode(["uint256"], [456]);
const purefiPackage = defaultAbiCoder.encode(
["uint8", "uint256", "uint256", "address", "bytes" ],
[type, ruleId, sessionId, sender, payload]
);
const message = solidityPack(["uint64", "bytes"], [currentTimestamp, purefiPackage]);
const hash = keccak256(message);
const signature = sign(signerIdentity.privateKey, hash);
const purefiData = defaultAbiCoder.encode(["uint64", "bytes", "bytes"], [currentTimestamp, signature, purefiPackage]);
// console.log("Res type 3: \n", (await test.funcWithPureFiContext(purefiData)).data );
await test.funcWithPureFiContext(purefiData)
}
});
it("test PureFiContext withDefaultAddressVerification modifier", async function(){
{
const type = 1;
const sender = signerIdentity.address;
const ruleId = DEFAULT_AML_RULE;
const purefiPackage = defaultAbiCoder.encode(["uint8", "uint256", "uint256", "address"], [type, ruleId, sessionId, sender]);
const message = solidityPack(["uint64", "bytes"], [currentTimestamp, purefiPackage]);
const hash = keccak256(message);
const signature = sign(signerIdentity.privateKey, hash);
const purefiData = defaultAbiCoder.encode(["uint64", "bytes", "bytes"], [ currentTimestamp, signature, purefiPackage ]);
await test.funcWithDefaultAddressVerification(2, sender, purefiData);
expect(await test.getCounter()).eq(200);
}
{
const type = 1;
const sender = signerIdentity.address;
const ruleId = DEFAULT_KYC_RULE;
const purefiPackage = defaultAbiCoder.encode(["uint8", "uint256", "uint256", "address"], [type, ruleId, sessionId, sender]);
const message = solidityPack(["uint64", "bytes"], [currentTimestamp, purefiPackage]);
const hash = keccak256(message);
const signature = sign(signerIdentity.privateKey, hash);
const purefiData = defaultAbiCoder.encode(["uint64", "bytes", "bytes"], [ currentTimestamp, signature, purefiPackage ]);
await test.funcWithDefaultAddressVerification(1, sender, purefiData);
}
{
const type = 1;
const sender = signerIdentity.address;
const ruleId = DEFAULT_AML_KYC_RULE;
const purefiPackage = defaultAbiCoder.encode(["uint8", "uint256", "uint256", "address"], [type, ruleId, sessionId, sender]);
const message = solidityPack(["uint64", "bytes"], [currentTimestamp, purefiPackage]);
const hash = keccak256(message);
const signature = sign(signerIdentity.privateKey, hash);
const purefiData = defaultAbiCoder.encode(["uint64", "bytes", "bytes"], [ currentTimestamp, signature, purefiPackage ]);
await test.funcWithDefaultAddressVerification(3, sender, purefiData);
}
});
it("test PureFiContext withCustomAddressVerification modifier", async function(){
{
const type = 1;
const sender = signerIdentity.address;
const ruleId = 123456;
const purefiPackage = defaultAbiCoder.encode(["uint8", "uint256", "uint256", "address"], [type, ruleId, sessionId, sender]);
const message = solidityPack(["uint64", "bytes"], [currentTimestamp, purefiPackage]);
const hash = keccak256(message);
const signature = sign(signerIdentity.privateKey, hash);
const purefiData = defaultAbiCoder.encode(["uint64", "bytes", "bytes"], [ currentTimestamp, signature, purefiPackage ]);
await test.funcWithCustomAddressVerification(ruleId, sender, purefiData);
expect(await test.getCounter()).eq(300);
}
});
});