Skip to content

Commit

Permalink
added nonce in sign verify orders
Browse files Browse the repository at this point in the history
  • Loading branch information
avishkarabhishek786 committed Feb 3, 2021
1 parent 227ce60 commit f195965
Show file tree
Hide file tree
Showing 9 changed files with 4,466 additions and 2,373 deletions.
38 changes: 25 additions & 13 deletions client/src/Paypal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const abi = require('ethereumjs-abi');

const Paypal = () => {

const BUYING_TOKEN_ADDRESS = "0x0dea6892e73e38a2854e8aF4983AFdf67615a93d";
const BUYING_TOKEN_ADDRESS = "0xa20799F4F3C425ca3F5984CEA2a081FeaE32bc31";

const [web3, setWeb3] = useState(undefined);
const [networkId, setNetworkId] = useState(undefined);
Expand Down Expand Up @@ -51,8 +51,8 @@ const Paypal = () => {
const tokenFactory = new web3.eth.Contract(TokenFactory.abi, tokenFactoryData.address);
setTokenFactory(tokenFactory);

const buyingToken = new web3.eth.Contract(VINC.abi, BUYING_TOKEN_ADDRESS);
setVinc(buyingToken);
const vinc = new web3.eth.Contract(VINC.abi, BUYING_TOKEN_ADDRESS);
setVinc(vinc);
}

useEffect(() => {
Expand All @@ -71,9 +71,10 @@ const Paypal = () => {
typeof web3 !== 'undefined'
&& typeof loggedInAccount !== 'undefined'
&& typeof tokenFactory !== 'undefined'
&& typeof vinc !== 'undefined'
&& isError.state !== true
)
}, [web3, loggedInAccount, tokenFactory]);
}, [web3, loggedInAccount, tokenFactory, vinc]);

useEffect(() => {
console.log(isReady());
Expand Down Expand Up @@ -117,9 +118,9 @@ const Paypal = () => {
setVinc(undefined);
}

const signOrder = useCallback((numberOfTokens) => {
console.log(isReady());
if(!isReady()) return;
const signOrder = async (numberOfTokens, nonce) => {

if(!isReady()) return null;
if(typeof BUYING_TOKEN_ADDRESS !== "string" || BUYING_TOKEN_ADDRESS.length<1) {
return console.warn("BUYING_TOKEN_ADDRESS not set");
}
Expand All @@ -132,18 +133,20 @@ const Paypal = () => {
let sha3hash = web3.utils.soliditySha3(
{type: 'address', value: loggedInAccount},
{type: 'uint256', value: numberOfTokensInWei},
{type: 'address', value: BUYING_TOKEN_ADDRESS}
{type: 'address', value: BUYING_TOKEN_ADDRESS},
{type: 'uint64', value: nonce}
);
console.log(loggedInAccount);
console.log(numberOfTokens);
console.log(numberOfTokensInWei);
console.log(BUYING_TOKEN_ADDRESS);
console.log(sha3hash);
console.log(nonce);

const signature = web3.eth.sign(sha3hash, loggedInAccount);
console.log(signature);
return signature;

}, [isReady]);
}

//const [checkout, setCheckout] = useState(false);

Expand All @@ -159,11 +162,22 @@ const Paypal = () => {

let amout_of_tokens = puchase_data.purchase_units[0].amount["value"];

const buyerSignature = await signOrder(amout_of_tokens);
// Get nonce
const nonce = await vinc.methods.getNonce(0, loggedInAccount).call();

const buyerSignature = await signOrder(amout_of_tokens, nonce);
console.log(buyerSignature);
if(typeof buyerSignature !== "string" || buyerSignature.length<1) {
setIsError({
state: true,
msg: "Invalid signature: " + buyerSignature
});
return false;
}

puchase_data.purchaser_eth_address = loggedInAccount;
puchase_data.buyer_signature = buyerSignature;
puchase_data.nonce = nonce;

console.log(puchase_data);

Expand Down Expand Up @@ -263,8 +277,6 @@ const Paypal = () => {
</div>
</div>
</div>

<input type="button" onClick={()=>signOrder(9)} value="Sign" />
</>
)

Expand Down
44 changes: 24 additions & 20 deletions client/src/RedeemCash.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ import React, { useState, useEffect, useCallback } from 'react';
import { Navbar } from './Elems';
import "./index.css";
import { getWeb3 } from "./utils";
import VINC from "./abis/VINC.json";
import { ToastContainer, toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';

const RedeemCash = () => {

const SELLING_TOKEN_ADDRESS = "0x0dea6892e73e38a2854e8aF4983AFdf67615a93d";
const SELLING_TOKEN_ADDRESS = "0xa20799F4F3C425ca3F5984CEA2a081FeaE32bc31";

const [web3, setWeb3] = useState(undefined);
const [networkId, setNetworkId] = useState(undefined);
const [loggedInAccount, setAccounts] = useState(undefined);
const [vinc, setVinc] = useState(undefined);
const [isError, setIsError] = useState({
state: false,
msg: ""
Expand All @@ -26,14 +28,16 @@ const RedeemCash = () => {
web3.eth.defaultAccount = loggedInAccount[0];
setWeb3(web3);
setAccounts(loggedInAccount[0]);

const vinc = new web3.eth.Contract(VINC.abi, SELLING_TOKEN_ADDRESS);
setVinc(vinc);
}

useEffect(() => {
init();

window.ethereum.on('accountsChanged', loginAcc => {
setAccounts(loginAcc[0]);
unsetStates();
});

}, []);
Expand All @@ -42,24 +46,14 @@ const RedeemCash = () => {
return (
typeof web3 !== 'undefined'
&& typeof loggedInAccount !== 'undefined'
&& typeof vinc === "object"
&& isError.state !== true
)
}, [web3, loggedInAccount]);
}, [web3, loggedInAccount, vinc, isError]);

useEffect(() => {
const signOrder = async (numberOfTokens, nonce) => {
console.log(isReady());
console.log(loggedInAccount);
if (!isReady()) return;

}, [isReady])

const unsetStates = () => {

}

const signOrder = useCallback((numberOfTokens) => {
console.log(isReady());
if(!isReady()) return;
if(!isReady()) return null;
if(typeof SELLING_TOKEN_ADDRESS !== "string" || SELLING_TOKEN_ADDRESS.length<1) {
return console.warn("SELLING_TOKEN_ADDRESS not set");
}
Expand All @@ -72,7 +66,8 @@ const RedeemCash = () => {
let sha3hash = web3.utils.soliditySha3(
{type: 'address', value: loggedInAccount},
{type: 'uint256', value: numberOfTokensInWei},
{type: 'address', value: SELLING_TOKEN_ADDRESS}
{type: 'address', value: SELLING_TOKEN_ADDRESS},
{type: 'uint64', value: nonce}
);
console.log(loggedInAccount);
console.log(numberOfTokens);
Expand All @@ -83,7 +78,7 @@ const RedeemCash = () => {
console.log(signature);
return signature;

}, [isReady]);
}

const sendRedeemCashRequest = async(e) => {

Expand All @@ -102,13 +97,22 @@ const RedeemCash = () => {
return;
}

const sellerSignature = await signOrder(sellingTokenAmount);
// Get nonce
const nonce = await vinc.methods.getNonce(1, loggedInAccount).call();

const sellerSignature = await signOrder(sellingTokenAmount, nonce);

if(typeof sellerSignature !== "string" || sellerSignature.length<1) {
showMessage("Empty signature", false);
return;
}

const redeem_data = {
sellingTokenAmount,
payPalEmail,
redeemerEthAddr: loggedInAccount,
sellerSignature: sellerSignature
sellerSignature: sellerSignature,
nonce: nonce
};

console.log(redeem_data);
Expand Down
2 changes: 1 addition & 1 deletion client/src/abis/ECDSA.json
Original file line number Diff line number Diff line change
Expand Up @@ -3505,7 +3505,7 @@
},
"networks": {},
"schemaVersion": "3.3.2",
"updatedAt": "2021-02-02T04:36:16.655Z",
"updatedAt": "2021-02-03T08:12:46.008Z",
"devdoc": {
"details": "Elliptic Curve Digital Signature Algorithm (ECDSA) operations. These functions can be used to verify that a message was signed by the holder of the private keys of a given address.",
"kind": "dev",
Expand Down
6 changes: 3 additions & 3 deletions client/src/abis/Migrations.json
Original file line number Diff line number Diff line change
Expand Up @@ -847,12 +847,12 @@
"5777": {
"events": {},
"links": {},
"address": "0x021ea7c8Cfe27C91C952ce3481aB612c2ceFc862",
"transactionHash": "0xca894f9bcc57d626b3cc1cd69f14a8eb8c1f3186197fd3e3221bc0ed44624102"
"address": "0xaF870Ee9986a64DE953C11c3ab314D6cAc8F16c0",
"transactionHash": "0xd45623ef9191e4e381748931137483dfe7de103c2e273832781a759c2d9fe183"
}
},
"schemaVersion": "3.3.2",
"updatedAt": "2021-02-02T07:11:04.107Z",
"updatedAt": "2021-02-03T08:12:56.162Z",
"networkType": "ethereum",
"devdoc": {
"kind": "dev",
Expand Down
Loading

0 comments on commit f195965

Please sign in to comment.