diff --git a/packages/client-test/package.json b/packages/client-test/package.json index 18b3598..d03a982 100644 --- a/packages/client-test/package.json +++ b/packages/client-test/package.json @@ -57,7 +57,7 @@ "@types/mocha": "^10.0.0", "@types/node": "^12.20.43", "@types/urijs": "^1.19.12", - "acc-sdk-client-v2": "~2.7.0", + "acc-sdk-client-v2": "~2.8.0", "assert": "^2.0.0", "axios": "^1.6.7", "beautify": "^0.0.8", diff --git a/packages/client/package.json b/packages/client/package.json index eefa2aa..e09abbb 100644 --- a/packages/client/package.json +++ b/packages/client/package.json @@ -1,6 +1,6 @@ { "name": "acc-sdk-client-v2", - "version": "2.7.0", + "version": "2.8.0", "author": "BOSagora Foundation", "license": "AGPL-3.0-or-later", "main": "dist/index.js", @@ -81,7 +81,7 @@ "@ethersproject/providers": "^5.7.0", "@ethersproject/random": "^5.7.0", "@ethersproject/wallet": "^5.7.0", - "acc-contracts-lib-v2": "~2.5.0", + "acc-contracts-lib-v2": "~2.6.0", "acc-sdk-common-v2": "~2.0.0", "google-libphonenumber": "^3.2.35", "unfetch": "~4.2.0" diff --git a/packages/client/src/client-common/context.ts b/packages/client/src/client-common/context.ts index e88c322..ddc49b5 100644 --- a/packages/client/src/client-common/context.ts +++ b/packages/client/src/client-common/context.ts @@ -3,13 +3,13 @@ import { SupportedNetwork, SupportedNetworkArray } from "./interfaces/common"; import { InvalidAddressError, UnsupportedProtocolError, UnsupportedNetworkError } from "acc-sdk-common-v2"; import { getNetwork } from "../utils/Utilty"; import { LIVE_CONTRACTS } from "./constants"; -export { ContextParams } from "./interfaces/context"; import { isAddress } from "@ethersproject/address"; import { Network } from "@ethersproject/networks"; import { JsonRpcProvider, Networkish } from "@ethersproject/providers"; import { AddressZero } from "@ethersproject/constants"; import { Wallet } from "@ethersproject/wallet"; +export { ContextParams } from "./interfaces/context"; const supportedProtocols = ["https:", "http:"]; // if (typeof process !== "undefined" && process.env?.TESTING) { diff --git a/packages/client/src/utils/ContractUtils.ts b/packages/client/src/utils/ContractUtils.ts index 50eebad..9d29c33 100644 --- a/packages/client/src/utils/ContractUtils.ts +++ b/packages/client/src/utils/ContractUtils.ts @@ -560,6 +560,37 @@ export class ContractUtils { return arrayify(keccak256(encodedResult)); } + public static async getPurchaseSignature( + signer: Signer, + purchase: { + purchaseId: string; + amount: BigNumberish; + loyalty: BigNumberish; + currency: string; + shopId: BytesLike; + account: string; + phone: BytesLike; + sender: string; + }, + chainId: BigNumberish + ): Promise { + const encodedData = defaultAbiCoder.encode( + ["string", "uint256", "uint256", "string", "bytes32", "address", "bytes32", "address", "uint256"], + [ + purchase.purchaseId, + purchase.amount, + purchase.loyalty, + purchase.currency, + purchase.shopId, + purchase.account, + purchase.phone, + purchase.sender, + chainId + ] + ); + return signer.signMessage(arrayify(keccak256(encodedData))); + } + public static async signMessage(signer: Signer, message: Uint8Array): Promise { return signer.signMessage(message); } diff --git a/packages/client/test/history/shop.test.ts b/packages/client/test/history/shop.test.ts index 40e62a9..58a3d33 100644 --- a/packages/client/test/history/shop.test.ts +++ b/packages/client/test/history/shop.test.ts @@ -102,18 +102,28 @@ describe("Integrated test of Shop", () => { const loyaltyAmount = purchaseAmount.mul(5).div(100); const phoneHash = ContractUtils.getPhoneHash(""); const foundation = await accounts[AccountIndex.FOUNDATION].getAddress(); - const purchaseParams = users.map((m) => { - return { - purchaseId: NodeInfo.getPurchaseId(), - amount: purchaseAmount, - loyalty: loyaltyAmount, - currency: "php", - shopId: shops[0].shopId, - account: m.address, - phone: phoneHash, - sender: foundation - }; - }); + + const purchaseParams = await Promise.all( + users.map(async (m) => { + const purchaseItem = { + purchaseId: NodeInfo.getPurchaseId(), + amount: purchaseAmount, + loyalty: loyaltyAmount, + currency: "php", + shopId: shops[0].shopId, + account: m.address, + phone: phoneHash, + sender: foundation, + signature: "" + }; + purchaseItem.signature = await ContractUtils.getPurchaseSignature( + accounts[AccountIndex.FOUNDATION], + purchaseItem, + NodeInfo.getChainId() + ); + return purchaseItem; + }) + ); const purchaseMessage = ContractUtils.getPurchasesMessage(0, purchaseParams, NodeInfo.getChainId()); const signatures = await Promise.all( validatorWallets.map((m) => ContractUtils.signMessage(m, purchaseMessage)) diff --git a/packages/client/test/methods/ledger.test.ts b/packages/client/test/methods/ledger.test.ts index caabb05..1463569 100644 --- a/packages/client/test/methods/ledger.test.ts +++ b/packages/client/test/methods/ledger.test.ts @@ -166,8 +166,14 @@ describe("Ledger", () => { shopId: shopData[purchaseData[0].shopIndex].shopId, account: AddressZero, phone: phoneHash, - sender: await accounts[AccountIndex.FOUNDATION].getAddress() + sender: await accounts[AccountIndex.FOUNDATION].getAddress(), + signature: "" }; + purchaseParams.signature = await ContractUtils.getPurchaseSignature( + accounts[AccountIndex.FOUNDATION], + purchaseParams, + NodeInfo.getChainId() + ); const purchaseMessage = ContractUtils.getPurchasesMessage(0, [purchaseParams], NodeInfo.getChainId()); const signatures = await Promise.all( validatorWallets.map((m) => ContractUtils.signMessage(m, purchaseMessage)) @@ -195,8 +201,14 @@ describe("Ledger", () => { shopId: shopData[purchaseData[0].shopIndex].shopId, account: userAddress, phone: phoneHash, - sender: await accounts[AccountIndex.FOUNDATION].getAddress() + sender: await accounts[AccountIndex.FOUNDATION].getAddress(), + signature: "" }; + purchaseParams.signature = await ContractUtils.getPurchaseSignature( + accounts[AccountIndex.FOUNDATION], + purchaseParams, + NodeInfo.getChainId() + ); const purchaseMessage = ContractUtils.getPurchasesMessage(0, [purchaseParams], NodeInfo.getChainId()); const signatures = await Promise.all( validatorWallets.map((m) => ContractUtils.signMessage(m, purchaseMessage)) diff --git a/packages/client/test/methods/shop_refund.test.ts b/packages/client/test/methods/shop_refund.test.ts index e3ebeba..c27f87b 100644 --- a/packages/client/test/methods/shop_refund.test.ts +++ b/packages/client/test/methods/shop_refund.test.ts @@ -214,8 +214,14 @@ describe("Shop Withdrawal", () => { shopId: shopData[purchase.shopIndex].shopId, account: userAccount, phone: phoneHash, - sender: await accounts[AccountIndex.FOUNDATION].getAddress() + sender: await accounts[AccountIndex.FOUNDATION].getAddress(), + signature: "" }; + purchaseParams.signature = await ContractUtils.getPurchaseSignature( + accounts[AccountIndex.FOUNDATION], + purchaseParams, + NodeInfo.getChainId() + ); const purchaseMessage = ContractUtils.getPurchasesMessage(0, [purchaseParams], NodeInfo.getChainId()); const signatures = await Promise.all( validatorWallets.map((m) => ContractUtils.signMessage(m, purchaseMessage)) diff --git a/yarn.lock b/yarn.lock index 256fcc8..cb66592 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1968,10 +1968,10 @@ abbrev@^2.0.0: resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-2.0.0.tgz#cf59829b8b4f03f89dda2771cb7f3653828c89bf" integrity sha512-6/mh1E2u2YgEsCHdY0Yx5oW+61gZU+1vXaoiHHrpKeuRNNgFvS+/jrwHiQhB5apAf5oB7UB7E19ol2R2LKH8hQ== -acc-contracts-lib-v2@~2.5.0: - version "2.5.0" - resolved "https://registry.yarnpkg.com/acc-contracts-lib-v2/-/acc-contracts-lib-v2-2.5.0.tgz#2a1cfa376c5915e3a11545fcdc954376bbeec578" - integrity sha512-EUhHCcwAYAsjw4LE2zf2MbtdMiI8Ugk80LoNEg3eJGpnFr5w1BT2nIoAgIKkK1H5znSXk7UVpngjeyv2xkLhKA== +acc-contracts-lib-v2@~2.6.0: + version "2.6.0" + resolved "https://registry.yarnpkg.com/acc-contracts-lib-v2/-/acc-contracts-lib-v2-2.6.0.tgz#a6adb0fb19c7b3fe7120abeef71d492a1ca57540" + integrity sha512-7hJc/wwh+xgCDHXEznbMCxRVaMHTpW7s1bkbgBAkKBdvtH/aiiP39dB3rEUx4I4fG4OnH9JIcMZ9VXmrdgUaaQ== dependencies: ethers "5.7.0"