Skip to content

Commit 0b68a0b

Browse files
Merge pull request #6 from AndriianChestnykh/tests-libs-browser
Updated encryption functions for browser
2 parents c9a94c0 + 6aea5e4 commit 0b68a0b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+223
-164
lines changed

.gitignore

100644100755
File mode changed.

LICENSE

100644100755
File mode changed.

README.md

100644100755
File mode changed.

Screenshot.png

100644100755
File mode changed.

contracts/GLDToken.sol

100644100755
File mode changed.

contracts/GameItem.sol

100644100755
File mode changed.

contracts/Hub.sol

100644100755
+3-3
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ contract Hub {
3939
emit PublicKeyRegistered(heir, publicKey);
4040
}
4141

42-
function registerPubKeyOracle(bytes calldata publicKey) public {
43-
registeredKeys[msg.sender] = publicKey;
44-
emit PublicKeyRegistered(msg.sender, publicKey);
42+
function registerPubKeyOracle(address oracle, bytes calldata publicKey) public {
43+
registeredKeys[oracle] = publicKey;
44+
emit PublicKeyRegistered(oracle, publicKey);
4545
}
4646

4747
//Function for getting public keys

contracts/Wallet.sol

100644100755
File mode changed.

dapps/oracle/cmd/main.go

100644100755
File mode changed.

dapps/oracle/config.yml

100644100755
File mode changed.

dapps/oracle/go.mod

100644100755
File mode changed.

dapps/oracle/go.sum

100644100755
File mode changed.

dapps/oracle/pkg/blockchain/blockchain.go

100644100755
File mode changed.

dapps/oracle/pkg/config/config.go

100644100755
File mode changed.

dapps/oracle/pkg/db/db.go

100644100755
File mode changed.

dapps/oracle/pkg/eth/client.go

100644100755
File mode changed.

dapps/oracle/pkg/eth/contracts.go

100644100755
File mode changed.

dapps/tx-interceptor-extension/images/icon-128.png

100644100755
File mode changed.

dapps/tx-interceptor-extension/images/icon-16.png

100644100755
File mode changed.

dapps/tx-interceptor-extension/images/icon-32.png

100644100755
File mode changed.

dapps/tx-interceptor-extension/images/icon-48.png

100644100755
File mode changed.

dapps/tx-interceptor-extension/manifest.json

100644100755
File mode changed.

dapps/tx-interceptor-extension/scripts/content.js

100644100755
File mode changed.

dapps/tx-interceptor-extension/scripts/foo.js

100644100755
File mode changed.

dapps/ui/heir.webpack.config.js

100644100755
File mode changed.

dapps/ui/owner.webpack.config.js

100644100755
File mode changed.

dapps/ui/src/index.css

100644100755
File mode changed.

dapps/ui/src/index.html

100644100755
File mode changed.

dapps/ui/src/index.js

100644100755
+25-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { ethers } from 'ethers'
1+
import { ethers } from 'ethers';
2+
import encryptIM from './../../utils/encrypt-im-browser-version.js';
23
import './index.css';
34

45
import WalletArtifact from '../../../artifacts/contracts/Wallet.sol/Wallet.json'
@@ -75,6 +76,24 @@ const provider = new ethers.providers.Web3Provider(window.ethereum)
7576
const hubAddress = DeployInfo.hubAddress
7677
const hubContract = new ethers.Contract(hubAddress, hubAbi, provider)
7778

79+
const accounts1 = {
80+
Owner: {
81+
address: "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
82+
publicKey: Buffer.from('0x04682c3ea377dafe9e4eb735af60c4edf2e581d529cc69816e768432a8aa09178470c9b1e703951f4a85e0dab7d8008e2a9e9e1794b0cfc6d430bc4aace3ad3e2', 'hex'),
83+
privateKey: Buffer.from('0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80', 'hex')
84+
},
85+
Heir: {
86+
address: "x70997970C51812dc3A010C7d01b50e0d17dc79C8",
87+
publicKey: Buffer.from('0x0406626fc5130be23a0e58c6d24e148a3e6cefd676162f4c176822aa885e6c2eb15a1657e3a4a865b516e7bf2288bfcb6d32cd7ecdc0f058b5bf84d28a5d9d2b2', 'hex'),
88+
privateKey: Buffer.from('0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d', 'hex'),
89+
},
90+
Oracle: {
91+
address: "0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC",
92+
publicKey: Buffer.from('0x0473a0b62325c802d13e0845e44a8199c91809a6df8a5be2f10c5270784b6db32de05b9818c92921488365ff6ba7258e72bc1e4aa05a6a8929787ba6cf0ddfb2b', 'hex'),
93+
privateKey: Buffer.from('0x5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a', 'hex'),
94+
}
95+
}
96+
7897
let walletInfo = {
7998
ownershipStatus: undefined,
8099
address: undefined,
@@ -139,20 +158,7 @@ async function onClickSignTypedData() {
139158
//todo - get heir address from heirAddress.value
140159
const signer = provider.getSigner(_accounts[0])
141160
const heirAddress = document.getElementById('heirAddress')
142-
const heir = provider.getSigner(heirAddress.value)
143-
144-
// //todo - get public key from heir
145-
// async function getPublicKeyFromHeir(address) {
146-
// try {
147-
// const publicKey = await hubContract.getPublicKeyHeir(address)
148-
// return publicKey
149-
// } catch {
150-
// console.error("Error getting public key:", error)
151-
// return null
152-
// }
153-
// }
154-
//
155-
// const publicKeyHeir = getPublicKeyFromHeir(heirAddress.value)
161+
const heir = provider.getSigner(heirAddress.value)
156162

157163
const im = {
158164
types: {
@@ -180,7 +186,10 @@ async function onClickSignTypedData() {
180186
signature: imSignature,
181187
}
182188

183-
signTypedDataResult.innerHTML = JSON.stringify(imWithSignature, null, 2)
189+
const encryptedIM = await encryptIM(imWithSignature, accounts1.Owner.privateKey, accounts1.Heir.publicKey);
190+
191+
// signTypedDataResult.innerHTML = JSON.stringify(imWithSignature, null, 2)
192+
signTypedDataResult.innerHTML = JSON.stringify(encryptedIM, null, 2)
184193
sendIMToOracleButton.disabled = false
185194
} catch (err) {
186195
console.error(err)
@@ -597,5 +606,3 @@ updateWalletButton.onclick = onClickUpdateWallet
597606
updateAccountButton.onclick = onClickUpdateAccount
598607

599608
window.addEventListener('DOMContentLoaded', initialize)
600-
601-
module.exports = {encryptIM, decryptIM}

dapps/ui/src/metamask-fox.svg

100644100755
File mode changed.

dapps/ui/src/styles/account-information-section.css

100644100755
File mode changed.

dapps/ui/src/styles/contract-connect-section.css

100644100755
File mode changed.

dapps/ui/src/styles/contract-info-details.css

100644100755
File mode changed.

dapps/ui/src/styles/contract-information-section.css

100644100755
File mode changed.

dapps/ui/src/styles/controller-transfer-section.css

100644100755
File mode changed.

dapps/ui/src/styles/header.css

100644100755
File mode changed.

dapps/ui/src/styles/inheritance-message-section.css

100644100755
File mode changed.

dapps/ui/src/styles/inheritance-sign-section.css

100644100755
File mode changed.

dapps/ui/src/styles/popup.css

100644100755
File mode changed.

dapps/ui/src/styles/transaction-section.css

100644100755
File mode changed.

dapps/ui/src/test-file.js

100644100755
File mode changed.

dapps/utils/compress-key.js

-9
This file was deleted.

dapps/utils/crypto-config-constant.js

100644100755
File mode changed.
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const sodium = require('sodium-universal');
2+
const CryptoJS = require('crypto-js');
3+
const cryptoConfigConstant = require('./crypto-config-constant.js');
4+
5+
async function decryptIM(encryptedMessage, recipientPrivateKey, senderPublicKey) {
6+
return new Promise((resolve, reject) => {
7+
try {
8+
if (recipientPrivateKey.startsWith("0x")) {
9+
recipientPrivateKey = recipientPrivateKey.substring(2);
10+
}
11+
12+
if (senderPublicKey.startsWith("0x")) {
13+
senderPublicKey = senderPublicKey.substring(2);
14+
}
15+
16+
const privKeyBuf = Uint8Array.from(recipientPrivateKey.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)));
17+
const pubKeyBuf = Uint8Array.from(senderPublicKey.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)));
18+
19+
const sharedSecret = new Uint8Array(sodium.crypto_scalarmult_BYTES);
20+
sodium.crypto_scalarmult(sharedSecret, privKeyBuf, pubKeyBuf);
21+
22+
const recipientSharedSecretHex = Array.prototype.map.call(sharedSecret, x => ('00' + x.toString(16)).slice(-2)).join('');
23+
const iv = new Uint8Array(cryptoConfigConstant.ivBytes).fill(0);
24+
const counter = CryptoJS.lib.WordArray.create('d8d8dae8405c447d86e84be03b71327b');
25+
const decrypted = CryptoJS.AES.decrypt(encryptedMessage, recipientSharedSecretHex, {
26+
iv: iv,
27+
mode: CryptoJS.mode.CTR,
28+
counter: counter
29+
});
30+
31+
resolve(decrypted.toString(CryptoJS.enc.Utf8));
32+
} catch (error) {
33+
reject(error);
34+
}
35+
});
36+
}
37+
38+
module.exports = decryptIM;

dapps/utils/decrypt-im.js

-41
This file was deleted.
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const sodium = require('sodium-universal');
2+
const CryptoJS = require('crypto-js');
3+
const cryptoConfigConstant = require('./crypto-config-constant.js');
4+
5+
async function encryptIM(message, senderPrivateKey, recipientPublicKey) {
6+
return new Promise((resolve, reject) => {
7+
try {
8+
9+
if (typeof message === 'object') {
10+
message = JSON.stringify(message);
11+
}
12+
13+
if (senderPrivateKey.startsWith("0x")) {
14+
senderPrivateKey = senderPrivateKey.substring(2);
15+
}
16+
17+
if (recipientPublicKey.startsWith("0x")) {
18+
recipientPublicKey = recipientPublicKey.substring(2);
19+
}
20+
21+
const privKeyBuf = Uint8Array.from(senderPrivateKey.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)));
22+
const pubKeyBuf = Uint8Array.from(recipientPublicKey.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)));
23+
24+
const sharedSecret = new Uint8Array(sodium.crypto_scalarmult_BYTES);
25+
sodium.crypto_scalarmult(sharedSecret, privKeyBuf, pubKeyBuf);
26+
27+
const senderSharedSecretHex = Array.prototype.map.call(sharedSecret, x => ('00' + x.toString(16)).slice(-2)).join('');
28+
const iv = new Uint8Array(cryptoConfigConstant.ivBytes).fill(0);
29+
const counter = CryptoJS.lib.WordArray.create('d8d8dae8405c447d86e84be03b71327b');
30+
const encrypted = CryptoJS.AES.encrypt(message, senderSharedSecretHex, {
31+
iv: iv,
32+
mode: CryptoJS.mode.CTR,
33+
counter: counter
34+
})
35+
36+
resolve (encrypted.toString());
37+
} catch (error) {
38+
reject(error);
39+
}
40+
})
41+
}
42+
43+
module.exports = encryptIM;

dapps/utils/encrypt-im.js

-43
This file was deleted.

deployInfo.json

100644100755
File mode changed.

hardhat.config.js

100644100755
File mode changed.

inheritance.png

100644100755
File mode changed.

package.json

100644100755
+3-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"@nomicfoundation/hardhat-toolbox": "^2.0.1",
2626
"@openzeppelin/contracts": "^4.8.1",
2727
"copy-webpack-plugin": "^11.0.0",
28-
"crypto": "^1.0.1",
28+
"crypto-js": "^4.2.0",
2929
"css-loader": "^6.8.1",
3030
"eth-sig-util": "^3.0.1",
3131
"fs": "^0.0.1-security",
@@ -35,7 +35,8 @@
3535
"node-polyfill-webpack-plugin": "^2.0.1",
3636
"secp256k1": "^5.0.0",
3737
"serve": "^14.2.0",
38-
"sodium": "^3.0.2",
38+
"sodium-javascript": "^0.8.0",
39+
"sodium-universal": "^4.0.1",
3940
"style-loader": "^3.3.3",
4041
"webpack": "^5.75.0",
4142
"webpack-cli": "^5.0.1",

pre-inheritance.png

100644100755
File mode changed.

scripts/deploy.js

100644100755
File mode changed.

test/encrypt-decrypt.test.js

100644100755
+45-14
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,60 @@
11
const { expect } = require("chai");
22
const { ethers } = require("hardhat");
33
const { utils } = require("ethers");
4-
const encryptIM = require("./../dapps/utils/encrypt-im.js");
5-
const decryptIM = require("./../dapps/utils/decrypt-im.js");
4+
// const encryptIM = require("./../dapps/utils/encrypt-im.js");
5+
// const decryptIM = require("./../dapps/utils/decrypt-im.js");
6+
7+
const encryptIM = require("./../dapps/utils/encrypt-im-browser-version.js");
8+
const decryptIM = require("./../dapps/utils/decrypt-im-browser-version.js");
9+
const sodium = require("sodium-universal");
610

711
const accounts = {
812
Owner: {
913
address: "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
10-
publicKey: Buffer.from('0x04682c3ea377dafe9e4eb735af60c4edf2e581d529cc69816e768432a8aa09178470c9b1e703951f4a85e0dab7d8008e2a9e9e1794b0cfc6d430bc4aace3ad3e2', 'hex'),
11-
privateKey: Buffer.from('0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80', 'hex')
14+
publicKey: '9adac7a7558764cf6bc45d88968b7d1e27b95e641c2827fba034350e91a44d22',
15+
privateKey: 'ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80',
1216
},
1317
Heir: {
14-
address: "x70997970C51812dc3A010C7d01b50e0d17dc79C8",
15-
publicKey: Buffer.from('0x0406626fc5130be23a0e58c6d24e148a3e6cefd676162f4c176822aa885e6c2eb15a1657e3a4a865b516e7bf2288bfcb6d32cd7ecdc0f058b5bf84d28a5d9d2b2', 'hex'),
16-
privateKey: Buffer.from('0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d', 'hex'),
18+
address: "0x70997970C51812dc3A010C7d01b50e0d17dc79C8",
19+
publicKey: '107302eea84f3f75c50184df4102862bf50f6fbcee88bf9d7d33852ab90f302d',
20+
privateKey: '59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d',
1721
},
1822
Oracle: {
1923
address: "0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC",
20-
publicKey: Buffer.from('0x0473a0b62325c802d13e0845e44a8199c91809a6df8a5be2f10c5270784b6db32de05b9818c92921488365ff6ba7258e72bc1e4aa05a6a8929787ba6cf0ddfb2b', 'hex'),
21-
privateKey: Buffer.from('0x5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a', 'hex'),
24+
publicKey: '9a09fdb91965386b6705c734ed36c3265144967b823e23a1b665ae8be922683c',
25+
privateKey: '5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a',
2226
}
2327
}
2428

2529
describe("Encryption and Decryption", function () {
26-
it("should encrypt and decrypt the message", async function () {
30+
before(() => {
31+
//TODO move to other file
32+
33+
const toHexString = (bytes) =>
34+
bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, '0'), '');
35+
36+
const ownerPubKey = new Uint8Array(sodium.crypto_scalarmult_BYTES);
37+
const ownerPrivKeyBuff = Uint8Array.from(accounts.Owner.privateKey.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)))
38+
sodium.crypto_scalarmult_base(ownerPubKey, ownerPrivKeyBuff)
39+
const ownerPubKeyHex = toHexString(Uint8Array.from(ownerPubKey));
40+
41+
const heirPubKey = new Uint8Array(sodium.crypto_scalarmult_BYTES);
42+
const heirPrivKeyBuff = Uint8Array.from(accounts.Heir.privateKey.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)))
43+
sodium.crypto_scalarmult_base(heirPubKey, heirPrivKeyBuff)
44+
const heirPubKeyHex = toHexString(Uint8Array.from(heirPubKey));
45+
console.log("s")
46+
47+
const oraclePubKey = new Uint8Array(sodium.crypto_scalarmult_BYTES);
48+
const oraclePrivKeyBuff = Uint8Array.from(accounts.Oracle.privateKey.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)))
49+
sodium.crypto_scalarmult_base(oraclePubKey, oraclePrivKeyBuff)
50+
const oraclePubKeyHex = toHexString(Uint8Array.from(oraclePubKey));
51+
52+
console.log("OwnerPubKey: ", ownerPubKeyHex);
53+
console.log("HeirPubKey: ", heirPubKeyHex);
54+
console.log("OraclePubKey: ", oraclePubKeyHex);
55+
});
56+
57+
it("should encrypt and decrypt the message (new functions)", async function () {
2758
const message = "Hello, world!";
2859

2960
// Encrypting a message from Owner to Heir
@@ -50,8 +81,8 @@ describe("Encryption and Decryption", function () {
5081

5182
expect(decryptedMessageFromOwnerToOracle).to.equal(encryptedMessage)
5283
})
53-
54-
it("create new account using ether.js and private key", async function () {
84+
85+
it("should encrypt the message, send it to Oracle, Oracle should encrypt and send it Heir, Heir should decrypt it from Oracle and decrypt it from Owner ", async function () {
5586
const message = "Hello, world!";
5687

5788
// Encrypting a message from Owner to Heir
@@ -65,7 +96,7 @@ describe("Encryption and Decryption", function () {
6596

6697
// Heir gets encrypt message from Oracle
6798
const decryptedMessageFromOwnetToHeir = await decryptIM(decryptedMessageFromOwnerToOracle, accounts.Heir.privateKey, accounts.Owner.publicKey)
68-
99+
69100
expect(decryptedMessageFromOwnetToHeir).to.equal(message)
70101
})
71-
});
102+
});

test/hub/hub.test.js

100644100755
File mode changed.

test/hub/pubKeyRegistration.test.js

100644100755
+2-2
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ describe("Hub Contract", function () {
5151
it("should register public key for oracle", async function () {
5252
const expectedPublicKey = "0x9876543210FEDCBA"; // Expected public key value by oracle
5353

54-
await hubContract.connect(owner).registerPubKeyOracle(expectedPublicKey);
55-
const registeredPublicKey = await hubContract.registeredKeys(owner.address);
54+
await hubContract.connect(owner).registerPubKeyOracle(oracle.address, expectedPublicKey);
55+
const registeredPublicKey = await hubContract.registeredKeys(oracle.address);
5656

5757
expect(registeredPublicKey.toLowerCase()).to.equal(expectedPublicKey.toLowerCase());
5858
});

0 commit comments

Comments
 (0)