Skip to content

Commit

Permalink
구매 구현, key struct 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
kyoungbinkim committed Mar 14, 2023
1 parent 9b19494 commit b44e5a5
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 8 deletions.
2 changes: 2 additions & 0 deletions front/src/react/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export default function LoginService() {
sessionStorage.setItem('loginTk', loginTk);
sessionStorage.setItem('jwtToken', res.data.token);

console.log(res.data)

httpCli.defaults.headers.common['access-token'] = JSON.stringify(res.data);
navigate('/');
return;
Expand Down
3 changes: 2 additions & 1 deletion front/src/react/registerData.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ const RegisterDataComponent = () => {
"desc" : `${desc}`,
"data" : `${textFile}`
}

// console.log(reqBody)ㄴ
httpCli.post('/data/register/', reqBody).then(
async res => {
console.log(res)
alert('데이터 등록이 완료되었습니다.')
console.log(res.data);
console.log(res.data.receipt);
Expand Down
6 changes: 3 additions & 3 deletions front/src/wallet/keyStruct.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default class UserKey {
const userPublicKey = {
ena : null,
pkOwn : pk_own,
pkEnc : Curve.basePointMul(types.hexToInt(sk_own)).toString(16)
pkEnc : Curve.basePointMul(types.hexToInt(sk_enc)).toString(16)
};
// azerothFront code userPublicKey.pkEnc.toString(16)
userPublicKey.ena = mimc7.hash(userPublicKey.pkOwn, userPublicKey.pkEnc);
Expand All @@ -85,8 +85,8 @@ export default class UserKey {

const userPublicKey = {
ena : null,
pkOwn : mimc7.hash(sk_own),
pkEnc : Curve.basePointMul(skBigInt).toString(16)
pkOwn : pk_own,
pkEnc : Curve.basePointMul(types.hexToInt(sk_enc)).toString(16)
};

// azerothFront code userPublicKey.pkEnc.toString(16)
Expand Down
66 changes: 66 additions & 0 deletions server/src/controller/genTradeController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import _ from 'lodash'
import Encryption from '../core/crypto/encryption';
import {
getDataInfoFromHct,
getUserInfoFormNickname
} from "../core/data/db.mysql";
import { getTradeContract } from "../core/contracts";

const pubEnc = new Encryption.publicKeyEncryption();

export const genTradeController = async (req, res) => {
try {
// console.log(req)
const h_ct = req.params.h_ct;
const tx_hash = req.params.tx_hash;
const dataInfo = (await getDataInfoFromHct(h_ct))[0];
const usrInfo = await getUserInfoFormNickname(_.get(dataInfo, 'owner_nickname'));

console.log("usrInfo", JSON.stringify(usrInfo, null, 2));

const receipt = await getTradeContract().eth.getTransaction(tx_hash)
console.log(receipt);

const tmp = _.get(receipt, 'input').slice(10)
for(let i=0; i<27; i++ ){
console.log(i, tmp.slice(i*64, (i+1)*64),)
}

const dec_ct = decrypCT(
_.get(receipt, 'input').slice(10),
_.get(usrInfo, 'sk_enc')
)

console.log(dec_ct)
// 2 : g^r
// 3 : c1
// 12 ~ 17 : CT
res.send({
flag: true
})
} catch (error) {
console.log(error)
res.send({
flag : false
})
}
}

const decrypCT = (data, sk_enc_peer) => {
const c0 = sliceData(data, 2)
const c1 = sliceData(data, 3)

let c2 = []
for (let i=12; i<18; i++){
c2.push(sliceData(data, i))
}

return pubEnc.Dec(
new Encryption.pCT(c0, c1, c2),
sk_enc_peer
)
}

const sliceData = (data ,idx) =>{
return data.slice(idx * 64 , (idx+1) * 64)
}
10 changes: 10 additions & 0 deletions server/src/core/contracts/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ export default class contract extends Web3Interface {
async getAllAddr() {
return (await this.eth.getAccounts());
}

async getTx(txHash) {
try {
const receipt = this.eth.getTransaction(txHash)
} catch (error) {
console.log(error)
return undefined;
}

}
}

export function registDataInputJsonToContractFormat(inputJson) {
Expand Down
2 changes: 2 additions & 0 deletions server/src/core/crypto/jwt.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export function verify(token, sk) {
export const authMiddleWare = async (req, res, next) => {

const jwtHeader = JSON.parse((req.headers['access-token'] || req.query.token) ?? '{}')
console.log(jwtHeader, req.headers);
if(jwtHeader === {}){
return res.send({
msg : 'do not have token',
Expand All @@ -52,6 +53,7 @@ export const authMiddleWare = async (req, res, next) => {
}
console.log(jwtHeader, req.headers);
try {

const sk = await getSkEncKey(jwtHeader.loginTk);
const token = jwtHeader.token;
if(!token){
Expand Down
11 changes: 11 additions & 0 deletions server/src/core/data/db.mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,17 @@ export async function getUserInfo(lgTk) {
}
}

export async function getUserInfoFormNickname(nickname) {
try {
const getUserKeyQuery = `SELECT * from user where nickname=?`
const [rows] = await promiseConnection.execute(getUserKeyQuery, [`${nickname}`])
return rows[0]
} catch (error) {
console.log(error);
return undefined;
}
}

export async function getUserKeysFromNickname(nickname) {
try {
const getUserKeyQuery = `SELECT nickname, pk_enc, pk_own from user where nickname=?`
Expand Down
6 changes: 3 additions & 3 deletions server/src/core/wallet/keyStruct.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export default class UserKey {
const userPublicKey = {
ena : null,
pkOwn : pk_own,
pkEnc : Curve.basePointMul(types.hexToInt(sk_own)).toString(16)
pkEnc : Curve.basePointMul(types.hexToInt(sk_enc)).toString(16)
};
// azerothFront code userPublicKey.pkEnc.toString(16)
userPublicKey.ena = mimc7.hash(userPublicKey.pkOwn, userPublicKey.pkEnc);
Expand All @@ -84,8 +84,8 @@ export default class UserKey {

const userPublicKey = {
ena : null,
pkOwn : mimc7.hash(sk_own),
pkEnc : Curve.basePointMul(skBigInt).toString(16)
pkOwn : pk_own,
pkEnc : Curve.basePointMul(types.hexToInt(sk_enc)).toString(16)
};

// azerothFront code userPublicKey.pkEnc.toString(16)
Expand Down
3 changes: 3 additions & 0 deletions server/src/routers/data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
getMyDataController,
getInfoFromHct
} from '../../controller/dataController';
import { genTradeController } from '../../controller/genTradeController';
import { registDataController } from '../../controller/registDataController';

const dataRouter = express.Router();
Expand All @@ -20,4 +21,6 @@ dataRouter.get('/mydata', getMyDataController);

dataRouter.get('/info/:h_ct', getInfoFromHct);

dataRouter.get('/gentrade/:h_ct/:tx_hash', genTradeController);

export default dataRouter;
2 changes: 1 addition & 1 deletion tradeContract/keys.json

Large diffs are not rendered by default.

0 comments on commit b44e5a5

Please sign in to comment.