Skip to content

Commit

Permalink
Change assistant to provisioningAgent
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKim20 committed Oct 29, 2024
1 parent 1b74f4d commit c4e04fd
Show file tree
Hide file tree
Showing 15 changed files with 122 additions and 126 deletions.
30 changes: 13 additions & 17 deletions packages/contracts/contracts/controllers/LoyaltyProvider.sol
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ contract LoyaltyProvider is LoyaltyProviderStorage, Initializable, OwnableUpgrad
)
);
address recover = ECDSA.recover(ECDSA.toEthSignedMessageHash(purchaseDataHash), data.signature);
address assistant = ledgerContract.assistantOf(data.sender);
if ((assistant == address(0x0)) && (recover != data.sender)) continue;
if ((assistant != address(0x0)) && (recover != assistant)) continue;
address agent = ledgerContract.provisioningAgentOf(data.sender);
if ((agent == address(0x0)) && (recover != data.sender)) continue;
if ((agent != address(0x0)) && (recover != agent)) continue;

uint256 loyaltyValue = data.loyalty;
uint256 loyaltyPoint = currencyRateContract.convertCurrencyToPoint(loyaltyValue, data.currency);
Expand Down Expand Up @@ -285,19 +285,17 @@ contract LoyaltyProvider is LoyaltyProviderStorage, Initializable, OwnableUpgrad
if (recurve1 == _provider) {
sender = _provider;
} else {
address assistant = ledgerContract.assistantOf(_provider);
require(assistant != address(0x0), "1501");
address agent = ledgerContract.provisioningAgentOf(_provider);
require(agent != address(0x0), "1501");

address recurve2 = ECDSA.recover(
ECDSA.toEthSignedMessageHash(
keccak256(
abi.encode(_provider, _receiver, _point, block.chainid, ledgerContract.nonceOf(assistant))
)
keccak256(abi.encode(_provider, _receiver, _point, block.chainid, ledgerContract.nonceOf(agent)))
),
_signature
);
require(recurve2 == assistant, "1501");
sender = assistant;
require(recurve2 == agent, "1501");
sender = agent;
}

ledgerContract.providePoint(
Expand Down Expand Up @@ -333,19 +331,17 @@ contract LoyaltyProvider is LoyaltyProviderStorage, Initializable, OwnableUpgrad
if (recurve1 == _provider) {
sender = _provider;
} else {
address assistant = ledgerContract.assistantOf(_provider);
require(assistant != address(0x0), "1501");
address agent = ledgerContract.provisioningAgentOf(_provider);
require(agent != address(0x0), "1501");

address recurve2 = ECDSA.recover(
ECDSA.toEthSignedMessageHash(
keccak256(
abi.encode(_provider, _phoneHash, _point, block.chainid, ledgerContract.nonceOf(assistant))
)
keccak256(abi.encode(_provider, _phoneHash, _point, block.chainid, ledgerContract.nonceOf(agent)))
),
_signature
);
require(recurve2 == assistant, "1501");
sender = assistant;
require(recurve2 == agent, "1501");
sender = agent;
}

address receiver = linkContract.toAddress(_phoneHash);
Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/contracts/interfaces/ILedger.sol
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,5 @@ interface ILedger {

function isProvider(address _account) external view returns (bool);

function assistantOf(address _account) external view returns (address);
function provisioningAgentOf(address _account) external view returns (address);
}
38 changes: 19 additions & 19 deletions packages/contracts/contracts/ledger/Ledger.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ contract Ledger is LedgerStorage, Initializable, OwnableUpgradeable, UUPSUpgrade
event RegisteredProvider(address provider);
event UnregisteredProvider(address provider);

event RegisteredAssistant(address provider, address assistant);
event RegisteredProvisioningAgent(address account, address agent);

struct ManagementAddresses {
address system;
Expand Down Expand Up @@ -497,33 +497,33 @@ contract Ledger is LedgerStorage, Initializable, OwnableUpgradeable, UUPSUpgrade
protocolFeeAccount = _account;
}

function registerProvider(address _provider) external {
function registerProvider(address _account) external {
require(_msgSender() == owner(), "1050");
providers[_provider] = true;
emit RegisteredProvider(_provider);
providers[_account] = true;
emit RegisteredProvider(_account);
}

function unregisterProvider(address _provider) external {
function unregisterProvider(address _account) external {
require(_msgSender() == owner(), "1050");
providers[_provider] = false;
emit UnregisteredProvider(_provider);
}

function registerAssistant(address _provider, address _assistant, bytes calldata _signature) external {
require(providers[_provider], "1054");
bytes32 dataHash = keccak256(abi.encode(_provider, _assistant, block.chainid, nonce[_provider]));
require(ECDSA.recover(ECDSA.toEthSignedMessageHash(dataHash), _signature) == _provider, "1501");
assistants[_provider] = _assistant;
nonce[_provider]++;

emit RegisteredAssistant(_provider, _assistant);
providers[_account] = false;
emit UnregisteredProvider(_account);
}

function isProvider(address _account) external view override returns (bool) {
return providers[_account];
}

function assistantOf(address _account) external view override returns (address) {
return assistants[_account];
function registerProvisioningAgent(address _account, address _agent, bytes calldata _signature) external {
require(providers[_account], "1054");
bytes32 dataHash = keccak256(abi.encode(_account, _agent, block.chainid, nonce[_account]));
require(ECDSA.recover(ECDSA.toEthSignedMessageHash(dataHash), _signature) == _account, "1501");
provisioningAgents[_account] = _agent;
nonce[_account]++;

emit RegisteredProvisioningAgent(_account, _agent);
}

function provisioningAgentOf(address _account) external view override returns (address) {
return provisioningAgents[_account];
}
}
2 changes: 1 addition & 1 deletion packages/contracts/contracts/ledger/LedgerStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ contract LedgerStorage {
mapping(address => uint256) internal liquidity;

mapping(address => bool) internal providers;
mapping(address => address) internal assistants;
mapping(address => address) internal provisioningAgents;

address public systemAccount;
address public paymentFeeAccount;
Expand Down
8 changes: 4 additions & 4 deletions packages/contracts/deploy/side_chain_devnet/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ async function deployLedger(accounts: IAccount, deployment: Deployments) {

{
const nonce = await contract.nonceOf(accounts.system.address);
const message = ContractUtils.getRegisterAssistanceMessage(
const message = ContractUtils.getRegisterAgentMessage(
accounts.system.address,
accounts.publisher.address,
nonce,
Expand All @@ -916,11 +916,11 @@ async function deployLedger(accounts: IAccount, deployment: Deployments) {
const signature = await ContractUtils.signMessage(accounts.system, message);
const tx = await contract
.connect(accounts.certifiers[0])
.registerAssistant(accounts.system.address, accounts.publisher.address, signature);
console.log(`Register assistant address of system (tx: ${tx.hash})...`);
.registerProvisioningAgent(accounts.system.address, accounts.publisher.address, signature);
console.log(`Register agent address of system (tx: ${tx.hash})...`);
// await tx.wait();

const value = await contract.assistantOf(accounts.system.address);
const value = await contract.provisioningAgentOf(accounts.system.address);
console.log("Assistance of System Account: ", value);
}
}
Expand Down
8 changes: 4 additions & 4 deletions packages/contracts/src/utils/ContractUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -663,15 +663,15 @@ export class ContractUtils {
return arrayify(keccak256(encodedResult));
}

public static getRegisterAssistanceMessage(
provider: string,
assistance: string,
public static getRegisterAgentMessage(
account: string,
agent: string,
nonce: BigNumberish,
chainId: BigNumberish
): Uint8Array {
const encodedResult = defaultAbiCoder.encode(
["address", "address", "uint256", "uint256"],
[provider, assistance, chainId, nonce]
[account, agent, chainId, nonce]
);
return arrayify(keccak256(encodedResult));
}
Expand Down
22 changes: 11 additions & 11 deletions packages/contracts/test/08-Ledger-Provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,9 @@ describe("Test for Ledger", () => {
});

it("Register Assistance", async () => {
expect(await ledgerContract.assistantOf(deployments.accounts.users[0].address)).equal(AddressZero);
expect(await ledgerContract.provisioningAgentOf(deployments.accounts.users[0].address)).equal(AddressZero);
const nonce = await ledgerContract.nonceOf(deployments.accounts.users[0].address);
const message = ContractUtils.getRegisterAssistanceMessage(
const message = ContractUtils.getRegisterAgentMessage(
deployments.accounts.users[0].address,
deployments.accounts.users[2].address,
nonce,
Expand All @@ -368,37 +368,37 @@ describe("Test for Ledger", () => {
await expect(
ledgerContract
.connect(deployments.accounts.deployer)
.registerAssistant(
.registerProvisioningAgent(
deployments.accounts.users[0].address,
deployments.accounts.users[2].address,
signature
)
)
.emit(ledgerContract, "RegisteredAssistant")
.emit(ledgerContract, "RegisteredProvisioningAgent")
.withNamedArgs({
provider: deployments.accounts.users[0].address,
assistant: deployments.accounts.users[2].address,
account: deployments.accounts.users[0].address,
agent: deployments.accounts.users[2].address,
});

expect(await ledgerContract.assistantOf(deployments.accounts.users[0].address)).equal(
expect(await ledgerContract.provisioningAgentOf(deployments.accounts.users[0].address)).equal(
deployments.accounts.users[2].address
);
});

it("Provide point - assistance", async () => {
it("Provide point - agent", async () => {
const providePoint = Amount.make(100, 18).value;
const assistance = deployments.accounts.users[2];
const agent = deployments.accounts.users[2];
const provider = deployments.accounts.users[0];
const receiver = deployments.accounts.users[3];
const nonce = await ledgerContract.nonceOf(assistance.address);
const nonce = await ledgerContract.nonceOf(agent.address);
const message = ContractUtils.getProvidePointToAddressMessage(
provider.address,
receiver.address,
providePoint,
nonce,
hre.ethers.provider.network.chainId
);
const signature = await ContractUtils.signMessage(assistance, message);
const signature = await ContractUtils.signMessage(agent, message);
await expect(
providerContract
.connect(deployments.accounts.certifiers[0])
Expand Down
4 changes: 2 additions & 2 deletions packages/relay/scripts/provider/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ async function main() {

const provider = new Wallet("0x70438bc3ed02b5e4b76d496625cb7c06d6b7bf4362295b16fdfe91a046d4586c"); // 0x64D111eA9763c93a003cef491941A011B8df5a49
const receiver = new Wallet("0x595f911dcf0845cb1f2d0e5cec9f1ccfd62fa199ebeae215a72aa56014edbb32"); // 0xB6f69F0e9e70034ba0578C542476cC13eF739269
const assistant = await sideLedgerContract.assistantOf(provider.address);
console.log(`assistant: ${assistant}`);
const agent = await sideLedgerContract.provisioningAgentOf(provider.address);
console.log(`agent: ${agent}`);

const balance1 = await sideLedgerContract.pointBalanceOf(receiver.address);
const pointAmount = Amount.make(100, 18).value;
Expand Down
4 changes: 2 additions & 2 deletions packages/relay/scripts/provider/send_to_phone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ async function main() {

const provider = new Wallet("0x70438bc3ed02b5e4b76d496625cb7c06d6b7bf4362295b16fdfe91a046d4586c"); // 0x64D111eA9763c93a003cef491941A011B8df5a49
const receiver = ContractUtils.getPhoneHash("+82 10-9000-2000");
const assistant = await sideLedgerContract.assistantOf(provider.address);
console.log(`assistant: ${assistant}`);
const agent = await sideLedgerContract.provisioningAgentOf(provider.address);
console.log(`agent: ${agent}`);

const balance1 = await sideLedgerContract.unPayablePointBalanceOf(receiver);
const pointAmount = Amount.make(100, 18).value;
Expand Down
Loading

0 comments on commit c4e04fd

Please sign in to comment.