Skip to content

Commit

Permalink
Merge pull request #45 from Jchicode/main
Browse files Browse the repository at this point in the history
feat(web):wallet connect and chain
  • Loading branch information
Jchicode authored Jan 8, 2024
2 parents 06fe2e5 + f8ba6a2 commit c532e06
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 67 deletions.
7 changes: 3 additions & 4 deletions contract/helper-hardhat-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,10 @@ const networkConfig = {
fee: "0.1",
fundAmount: "100", // 100
automationUpdateInterval: "30"
},

}
};
const developmentChains = ["hardhat", "localhost"]
const VERIFICATION_BLOCK_CONFIRMATIONS = 6
const developmentChains = ["hardhat", "localhost"];
const VERIFICATION_BLOCK_CONFIRMATIONS = 6;

module.exports = {
networkConfig,
Expand Down
6 changes: 4 additions & 2 deletions locales/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ common:

"pick a site": Pick a site
"need key tips": Please provider your private key to get message safety.
connect: Connect
connect: Connect Wallet
disconnect: Disconnect Wallet
update: Update
"public key": Public Key
"public key": Nostr Public Key
"wallet address": Wallet Address
"input private key": Input your Private Key
"input private key here": Type your key here

Expand Down
6 changes: 4 additions & 2 deletions locales/zh-CN.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ common:

"pick a site": 选择一个 Site
"need key tips": 请提供你的私钥,用以建立安全通信
connect: 连接
connect: 连接钱包
disconnect: 断开连接钱包
update: 更新
"public key": 公钥
"public key": nostr公钥
"wallet address": 钱包地址
"input private key": 请输入私钥
"input private key here": 在此处输入私钥

Expand Down
12 changes: 6 additions & 6 deletions public/serverConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
"Theme": "light",
"Relay": [
{
"name": "gw105",
"url": "wss://nostr.gw105.oneitfarm.com",
"lat": 34.7732,
"long": 113.722,
"city": "Shanghai",
"flag": "twemoji:flag-china"
"name": "gw106",
"url": "wss://nostr.gw106.oneitfarm.com",
"lat": 1.2929,
"long": 103.8547,
"city": "Singapore",
"flag": "twemoji:flag-singapore"
}
]
}
96 changes: 60 additions & 36 deletions src/components/Modal/account.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,76 @@ const { t } = useLang();
import { getNewNostrPrivateKey, md5 } from "@/utils/shared";
import { getPublicKey } from "nostr-tools";
import { getUserHubContract } from "@/utils/contract/user-hub";
import { handleEtherError, encrypt } from "@/utils/shared";
import { signMessage, getWalletAddres } from "@/utils/contract/web3";
import { decrypt } from "@/utils/shared";
import { handleEtherError, decrypt, encrypt } from "@/utils/shared";
import { useAccountStore } from "@/store/modules/account";
const privateKey = ref("");
const loading = ref(false);
const handleSubmit = async () => {
privateKey.value = getNewNostrPrivateKey();
const publicKey = getPublicKey(privateKey.value);
const address = await getWalletAddres();
const contract = await getUserHubContract();
const data = await contract.getUserInfo(address);
console.log(data);
console.log(address);
console.log(contract);
if (!data[0]) {
// register
privateKey.value = getNewNostrPrivateKey();
console.log(privateKey);
const publicKey = getPublicKey(privateKey.value);
let encryptKey;
try {
loading.value = true;
encryptKey = await signMessage("CloudX3");
} catch (e) {
window.alert(t("user.cancelSign"));
loading.value = false;
return;
}
let encryptKey;
try {
loading.value = true;
encryptKey = await signMessage("CloudX3");
console.log(encryptKey);
} catch (e) {
window.alert(t("user.cancelSign"));
loading.value = false;
return;
}
try {
loading.value = true;
const nostrEncryptKey = encrypt(privateKey.value, encryptKey);
console.log(decrypt(nostrEncryptKey, encryptKey));
console.log(
"用户信息\r\n钱包地址:%s\r\n签名结果:%s\r\nNostr公钥:%s\r\nNostr明文私钥:%s\r\nNostr加密私钥:%s",
await getWalletAddres(),
encryptKey,
publicKey,
privateKey.value,
nostrEncryptKey
);
const transaction = await contract.registerUser(
publicKey,
nostrEncryptKey,
md5(publicKey)
);
try {
loading.value = true;
const nostrEncryptKey = encrypt(privateKey.value, encryptKey);
console.log(nostrEncryptKey);
console.log(decrypt(nostrEncryptKey, encryptKey));
console.log(
"用户信息\r\n钱包地址:%s\r\n签名结果:%s\r\nNostr公钥:%s\r\nNostr明文私钥:%s\r\nNostr加密私钥:%s",
await getWalletAddres(),
encryptKey,
publicKey,
privateKey.value,
nostrEncryptKey
);
const transaction = await contract.registerUser(
publicKey,
nostrEncryptKey,
md5(publicKey)
);
await transaction.wait();
window.location.reload();
} catch (e) {
loading.value = false;
handleEtherError(e);
await transaction.wait();
window.location.reload();
useAccountStore().savePrivateKey(privateKey.value);
} catch (e) {
loading.value = false;
handleEtherError(e);
}
} else {
try {
const chainPrivateKey = data[1].privateKey;
const signKey = await signMessage("CloudX3");
const truePrivateKey = decrypt(chainPrivateKey, signKey);
useAccountStore().savePrivateKey(truePrivateKey);
window.location.reload();
} catch (e) {
window.alert(
"Please sign the message for build security tunnel with server"
);
// handleEtherError(e);
}
}
};
</script>
Expand Down
16 changes: 8 additions & 8 deletions src/config/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ const chains: Chain[] = [

oracle: "0xC1A56c1c85a4D957a513719FdB30eac50a861433",

podContract: "0xC033615137D482f51718106489035bE6CCDcE0DD",
podJobId: "780d3dd1933a4a9d839f4c78d92ab595",
podContract: "0xC7F694fbbc168CE02F2ce9cE84d748957F283336",
podJobId: "c1efad95d1b34cc9b3e2ee06802a2a6d",

deploymentContract: "0x70F4C5425bf1eAcd1fcfdaC5BfF5393EF45cA53E",
deploymentJobId: "68c1dc5cd63841459ff2395a931f042c",
deploymentContract: "0x6Ca351492b3b137aBb94052B847Af891bb067eFe",
deploymentJobId: "d61b61a4e6684116be70d6ec6e58b52e",

nodeContract: "0x1Acc504c02768ff00fBcEB331dB5Eda37fAC006E",
nodeJobId: "aa7198c0190f4cc29d4e4470c08f6391",
Expand All @@ -112,10 +112,10 @@ const chains: Chain[] = [

codContract: "0x754e87664F278f2be89b3f496955752f127fBdf8",

stcMarketContract: "0x6051273DB68F2af68617589f30F8a91f859e5b82",
stcMarketTokenContract: "0x8b321Dde4CAe93848f756895fdb34E889A6c831b",
stcMarketContract: "0xa05D733aE699a1cd3Eb3443ca932FfB69e1e7fFC",
stcMarketTokenContract: "0xe0bf50E501236845580A090c180B218CdAc46204",

userHubContract: "0xE87a986fDc35170c66C3a2449bC4Aee6350cc1F6"
userHubContract: "0x51923dcd6Bb170bd28AD4425A6Dd5cD0456F8e52"
},
{
chainId: "0x15eb",
Expand Down Expand Up @@ -155,7 +155,7 @@ const chains: Chain[] = [
}
];

let currentChain: ChainName = "Optimism Goerli Testnet";
let currentChain: ChainName = "Sepolia";
export function setCurrentChain(name: ChainName) {
currentChain = name;
}
Expand Down
38 changes: 31 additions & 7 deletions src/layout/components/avatar.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
<script setup lang="ts">
import { useAccountStore } from "@/store/modules/account";
import { useLang } from "@/hooks/useLang";
import { storageSession } from "@pureadmin/utils";
import { md5 } from "@/utils/shared";
const { t } = useLang();
const accountStore = useAccountStore();
const disconnectWallet = async () => {
if (accountStore.address) {
const cacheKey = await md5(accountStore.address);
storageSession().removeItem(cacheKey);
window.location.reload();
}
};
</script>

<template>
Expand All @@ -21,19 +30,34 @@ const accountStore = useAccountStore();
class="dropdown-content card w-[400px] shadow bg-primary text-primary-content"
>
<div class="card-body px-4 py-3">
<h2 class="card-title text-sm">{{ t("common.public key") }}</h2>
<h2 class="card-title text-sm">{{ t("common.wallet address") }}</h2>
<span class="text-[10px] break-all">{{ accountStore.address }}</span>

<h2 class="card-title text-sm">{{ t("common.public key") }}</h2>
<span class="text-[10px] break-all">{{ accountStore.publicKey }}</span>
<label for="key-modal" class="btn btn-success btn-sm">{{
t("common.update")
}}</label>
<!-- <label for="key-modal" class="btn btn-success btn-sm">{{-->
<!-- t("common.update")-->
<!-- }}</label>-->
<label
@click="disconnectWallet"
class="btn btn-success btn-sm"
style="text-transform: capitalize"
>
{{ t("common.disconnect") }}</label
>
</div>
</div>
</div>

<label v-else for="key-modal" class="btn btn-primary">{{
t("common.connect")
}}</label>
<label
v-else
for="key-modal"
class="btn btn-primary"
style="text-transform: capitalize"
>
<IconifyIconOnline icon="carbon:wallet" width="30px" height="30px" />
{{ t("common.connect") }}</label
>
</template>

<style scoped></style>
14 changes: 12 additions & 2 deletions src/store/modules/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { accountType } from "./types";
import { getPublicKey } from "nostr-tools";
// import { storageLocal } from "@pureadmin/utils";
import { getUserHubContract } from "@/utils/contract/user-hub";
import { handleEtherError, decrypt } from "@/utils/shared";
import { handleEtherError, decrypt, md5 } from "@/utils/shared";
import { getWalletAddres, signMessage } from "@/utils/contract/web3";
import { storageSession } from "@pureadmin/utils";

export const useAccountStore = defineStore({
id: "account-settings",
state: (): accountType => ({
name: "",
address: "",
publicKey: "",
privateKey: "" //storageLocal().getItem("sk") || ""
}),
Expand All @@ -32,6 +34,9 @@ export const useAccountStore = defineStore({
SET_PRIVATEKEY(key: string) {
this.privateKey = key;
},
SET_ADDRESS(key: string) {
this.address = key;
},
async init(): Promise<any> {
try {
const address = await getWalletAddres();
Expand All @@ -41,8 +46,13 @@ export const useAccountStore = defineStore({

const { privateKey } = data[1];
try {
const signKey = await signMessage("CloudX3");
const cacheKey = await md5(address);
const signKey = storageSession().getItem(cacheKey) as string;
if (!signKey) {
return;
}
const truePrivateKey = decrypt(privateKey, signKey);
this.SET_ADDRESS(address);
this.savePrivateKey(truePrivateKey);
} catch (e) {
window.alert(
Expand Down
1 change: 1 addition & 0 deletions src/store/modules/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type setType = {

export type accountType = {
name: string; // show name
address: string; // show address
publicKey: string; // public key
privateKey: string; // private key
};

0 comments on commit c532e06

Please sign in to comment.