Skip to content

Commit

Permalink
Improve the ability to register multiple relay keys
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKim20 committed Sep 18, 2023
1 parent 2be15f7 commit f0a2b6f
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 42 deletions.
3 changes: 2 additions & 1 deletion packages/relay/config/config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ logging:
console: true

relay:
manager_key: ${MANAGER_KEY}
managerKeys:
- ${MANAGER_KEY1}

contracts:
tokenAddress : ${TOKEN_CONTRACT_ADDRESS}
Expand Down
7 changes: 6 additions & 1 deletion packages/relay/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ logging:
console: true

relay:
manager_key: ${MANAGER_KEY}
managerKeys:
- ${MANAGER_KEY1}
- ${MANAGER_KEY2}
- ${MANAGER_KEY3}
- ${MANAGER_KEY4}
- ${MANAGER_KEY5}

contracts:
tokenAddress : ${TOKEN_CONTRACT_ADDRESS}
Expand Down
7 changes: 6 additions & 1 deletion packages/relay/config/config_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ logging:
console: true

relay:
manager_key: ${MANAGER_KEY}
managerKeys:
- ${MANAGER_KEY1}
- ${MANAGER_KEY2}
- ${MANAGER_KEY3}
- ${MANAGER_KEY4}
- ${MANAGER_KEY5}

contracts:
tokenAddress : ${TOKEN_CONTRACT_ADDRESS}
Expand Down
16 changes: 14 additions & 2 deletions packages/relay/env/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,20 @@ FOUNDATION=0x56a083b8c342f4bc7288ac1d2b793373bba76d5e8cda28f1876f6c4bfafe4c46
# 0xc86c00147049695C3Cad5eA4ebb75B6bd4f09126
LINK_VALIDATOR1=0xd56ca4bc48b3114d7738236c748e2b75c32c9b1e8b2beb118b83e527ea6ef619

# 0x454cd944C9Eac1e84064c0D105931aDCEbC86c89
MANAGER_KEY=0xf17b5048d0acdcabcc1fa5f3e1aa6753243d4309a6a30f951d5712cb50c86671
# 0x4376b9f7F6E6fa2d543910dCec2CDcC290A97722
MANAGER_KEY1=0xf17b5048d0acdcabcc1fa5f3e1aa6753243d4309a6a30f951d5712cb50c86671

# 0x9f97e74e211D53B16491f33a1fF3A6E774B6Af26
MANAGER_KEY2=0x75c9c2faed55e507ca76bc866d7ca9113dc46e27d714960cf08ad0cb271adb52

# 0x1cfF2132B4196AD49076A15868dCe92445c4c128
MANAGER_KEY3=0x4fe23c407dee94dcfe64b36d67fc1331bf0309bc8649fb440db8326f4c80288b

# 0x70eB26f311Ad5c94Be746394f91AA51b72109A14
MANAGER_KEY4=0xce6f45e7a4ae40cac075b1a04b4b4ad31af6a524871fd52080166e959e5929c4

# 0xEaeB90D77f7756fBf177D6E0E1BB794639e6097f
MANAGER_KEY5=0xc645ef34a5428f3b00f2d4b8cc647380f0b939d4ae5ccb7a04ead77ab0a8628d

FOUNDATION_EMAIL=[email protected]
FOUNDATION_ADDRESS=0x4501F7aF010Cef3DcEaAfbc7Bfb2B39dE57df54d
Expand Down
11 changes: 0 additions & 11 deletions packages/relay/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,6 @@ function getAccounts() {
accounts.push(process.env.VALIDATOR5);
}

if (
process.env.MANAGER_KEY !== undefined &&
process.env.MANAGER_KEY.trim() !== "" &&
reg_bytes64.test(process.env.MANAGER_KEY)
) {
accounts.push(process.env.MANAGER_KEY);
} else {
process.env.MANAGER_KEY = Wallet.createRandom().privateKey;
accounts.push(process.env.MANAGER_KEY || "");
}

return accounts;
}

Expand Down
16 changes: 11 additions & 5 deletions packages/relay/src/common/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,23 +157,29 @@ export class RelayConfig implements IRelayConfig {
/**
* 계정의 비밀키 또는 키파일
*/
public manager_key: string;
public managerKeys: string[];

/**
* Constructor
*/
constructor() {
const defaults = RelayConfig.defaultValue();

this.manager_key = defaults.manager_key;
this.managerKeys = defaults.managerKeys;
}

/**
* Returns default value
*/
public static defaultValue(): IRelayConfig {
return {
manager_key: process.env.MANAGER_KEY || "",
managerKeys: [
process.env.MANAGER_KEY1 || "",
process.env.MANAGER_KEY2 || "",
process.env.MANAGER_KEY3 || "",
process.env.MANAGER_KEY4 || "",
process.env.MANAGER_KEY5 || "",
],
};
}

Expand All @@ -182,7 +188,7 @@ export class RelayConfig implements IRelayConfig {
* @param config The object of ILoggingConfig
*/
public readFromObject(config: IRelayConfig) {
if (config.manager_key !== undefined) this.manager_key = config.manager_key;
if (config.managerKeys !== undefined) this.managerKeys = config.managerKeys;
}
}

Expand Down Expand Up @@ -314,7 +320,7 @@ export interface ILoggingConfig {
}

export interface IRelayConfig {
manager_key: string;
managerKeys: string[];
}

export interface IContractsConfig {
Expand Down
27 changes: 14 additions & 13 deletions packages/relay/src/routers/DefaultRouter.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { NonceManager } from "@ethersproject/experimental";
import { Signer, Wallet } from "ethers";
import { body, validationResult } from "express-validator";
import * as hre from "hardhat";
import { Ledger, LinkCollection, Token } from "../../typechain-types";
import { Config } from "../common/Config";
import { logger } from "../common/Logger";
import { GasPriceManager } from "../contract/GasPriceManager";
import { WebService } from "../service/WebService";
import { ContractUtils } from "../utils/ContractUtils";
import { Validation } from "../validation";

import { NonceManager } from "@ethersproject/experimental";
import { Signer, Wallet } from "ethers";
import { body, validationResult } from "express-validator";
import * as hre from "hardhat";

import express from "express";
import { Validation } from "../validation";
import { ContractUtils } from "../utils/ContractUtils";

export class DefaultRouter {
/**
Expand All @@ -29,7 +30,7 @@ export class DefaultRouter {
* 트팬잭션을 중계할 때 사용될 지갑
* @private
*/
private relayWallet: Wallet;
private relayWallets: Wallet[];

/**
* ERC20 토큰 컨트랙트
Expand Down Expand Up @@ -57,7 +58,7 @@ export class DefaultRouter {
constructor(service: WebService, config: Config) {
this._web_service = service;
this._config = config;
this.relayWallet = new Wallet(this._config.relay.manager_key);
this.relayWallets = this._config.relay.managerKeys.map((m) => new Wallet(m));
}

private get app(): express.Application {
Expand All @@ -69,7 +70,7 @@ export class DefaultRouter {
* @private
*/
private getRelaySigner(): Signer {
return new NonceManager(new GasPriceManager(hre.ethers.provider.getSigner(this.relayWallet.address)));
return new NonceManager(new GasPriceManager(this.relayWallets[0].connect(hre.ethers.provider)));
}

/**
Expand Down Expand Up @@ -250,7 +251,7 @@ export class DefaultRouter {
);
}
const tx = await (await this.getLedgerContract())
.connect(await this.getRelaySigner())
.connect(this.getRelaySigner())
.payMileage(purchaseId, amount, email, franchiseeId, signer, signature);

logger.http(`TxHash(payMileage): `, tx.hash);
Expand Down Expand Up @@ -313,7 +314,7 @@ export class DefaultRouter {
);
}
const tx = await (await this.getLedgerContract())
.connect(await this.getRelaySigner())
.connect(this.getRelaySigner())
.payToken(purchaseId, amount, email, franchiseeId, signer, signature);

logger.http(`TxHash(payToken): `, tx.hash);
Expand Down Expand Up @@ -374,7 +375,7 @@ export class DefaultRouter {
);
}
const tx = await (await this.getLedgerContract())
.connect(await this.getRelaySigner())
.connect(this.getRelaySigner())
.exchangeTokenToMileage(email, amountToken, signer, signature);

logger.http(`TxHash(exchangeTokenToMileage): `, tx.hash);
Expand Down Expand Up @@ -435,7 +436,7 @@ export class DefaultRouter {
);
}
const tx = await (await this.getLedgerContract())
.connect(await this.getRelaySigner())
.connect(this.getRelaySigner())
.exchangeMileageToToken(email, amountMileage, signer, signature);

logger.http(`TxHash(exchangeMileageToToken): `, tx.hash);
Expand Down
7 changes: 3 additions & 4 deletions packages/relay/test/Config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ describe("Test of Config", () => {
assert.strictEqual(config.server.port.toString(), "3000");
assert.strictEqual(config.logging.folder, path.resolve("logs"));
assert.strictEqual(config.logging.level, "debug");
assert.strictEqual(
config.relay.manager_key,
"0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d"
);
assert.deepStrictEqual(config.relay.managerKeys, [
"0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d",
]);
assert.strictEqual(config.contracts.tokenAddress, "0x898Bf21a9e1fF51d3F1248E0A253f6A58C3a736a");
assert.strictEqual(config.contracts.ledgerAddress, "0xc573eF6FDcaf1461FF2BB75a70B7685ad395AA2d");
assert.strictEqual(config.contracts.emailLinkerAddress, "0x8CA2D0080a42DB61cbe59611551412D294FC5911");
Expand Down
29 changes: 26 additions & 3 deletions packages/relay/test/Endpoints.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,21 @@ chai.use(solidity);
describe("Test of Server", function () {
this.timeout(1000 * 60 * 5);
const provider = hre.waffle.provider;
const [deployer, foundation, validator1, validator2, validator3, relay, user1, user2, user3] =
provider.getWallets();
const [
deployer,
foundation,
validator1,
validator2,
validator3,
user1,
user2,
user3,
relay1,
relay2,
relay3,
relay4,
relay5,
] = provider.getWallets();

const validators = [validator1, validator2, validator3];
const linkValidators = [validator1];
Expand Down Expand Up @@ -211,6 +224,14 @@ describe("Test of Server", function () {
config.contracts.tokenAddress = tokenContract.address;
config.contracts.emailLinkerAddress = linkCollectionContract.address;
config.contracts.ledgerAddress = ledgerContract.address;

config.relay.managerKeys = [
relay1.privateKey,
relay2.privateKey,
relay3.privateKey,
relay4.privateKey,
relay5.privateKey,
];
});

before("Create TestServer", async () => {
Expand Down Expand Up @@ -244,7 +265,9 @@ describe("Test of Server", function () {
const hash = emailHashes[0];
const signature = await ContractUtils.sign(users[0], hash, nonce);
reqId = ContractUtils.getRequestId(hash, users[0].address, nonce);
await expect(linkCollectionContract.connect(relay).addRequest(reqId, hash, users[0].address, signature))
await expect(
linkCollectionContract.connect(relay1).addRequest(reqId, hash, users[0].address, signature)
)
.to.emit(linkCollectionContract, "AddedRequestItem")
.withArgs(reqId, hash, users[0].address);
await linkCollectionContract.connect(validator1).voteRequest(reqId);
Expand Down
3 changes: 2 additions & 1 deletion packages/relay/test/config.test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ logging:
level: debug

relay:
manager_key: "0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d"
managerKeys:
- "0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d"

contracts:
tokenAddress : "0x898Bf21a9e1fF51d3F1248E0A253f6A58C3a736a"
Expand Down

0 comments on commit f0a2b6f

Please sign in to comment.