Skip to content

Commit fb5ff71

Browse files
committedMar 5, 2023
add decryption, mostly
1 parent f1687b6 commit fb5ff71

File tree

2 files changed

+91
-10
lines changed

2 files changed

+91
-10
lines changed
 

‎src/pages/api/postDecrypted.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,27 @@ import Simple from "../../contractInfo/contract-addressBdl.json";
22
import Web3 from "web3";
33
import axios from "axios";
44
import LitJsSdk from "lit-js-sdk";
5+
import {Blob} from 'node:buffer';
56

67
const chain = "mumbai"
78

89
function b64toBlob(dataURI) {
910

10-
return Buffer.from("H2gmAteQletDqRQqUMZ+N92H+ucVOXXRzqGtNzfIjBM=", "base64");
11+
//return Buffer.from("H2gmAteQletDqRQqUMZ+N92H+ucVOXXRzqGtNzfIjBM=", "base64");
1112

12-
/* var byteString = atob();
13+
var byteString = atob(dataURI);
1314
var ab = new ArrayBuffer(byteString.length);
1415
var ia = new Uint8Array(ab);
1516

1617
for (var i = 0; i < byteString.length; i++) {
1718
ia[i] = byteString.charCodeAt(i);
1819
}
19-
return new Blob([ab], { type: 'application/octet-stream' }); */
20+
return new Blob([ab], { type: 'application/octet-stream' });
2021

2122
}
2223

2324
const litSDK = new LitJsSdk.LitNodeClient();
24-
litSDK.connect();
25+
litSDK.connect();
2526

2627
export default async function handler(req, res) {
2728
//const postId = req.query.postId;
@@ -32,7 +33,7 @@ export default async function handler(req, res) {
3233
);
3334
const contract = new web3.eth.Contract(contractAbi.abi, Simple.Simple);
3435

35-
36+
3637

3738
// create a Web3 instance
3839

@@ -63,20 +64,19 @@ export default async function handler(req, res) {
6364

6465

6566

66-
67-
const decrypted64 = b64toBlob("MQ1jImwxXBx7L…O/NaIvP7LID1wuuhVWIdPrQpZPZg65GDKDegDTSX+xH+6Aw==")
67+
const decrypted64 = "MQ1jImwxXBx7L…O/NaIvP7LID1wuuhVWIdPrQpZPZg65GDKDegDTSX+xH+6Aw=="
6868

6969
const blobContent = await b64toBlob(decrypted64);
7070

71-
const symmetricKey = await litSDK.getEncryptionKey({
71+
const symmetricKey = await litSDK.current.getEncryptionKey({
7272
accessControlConditions,
7373
toDecrypt: "58700c72e794465b6882676b4726108728d717e8c645d6d4350230336c63d2aceaf577b7626f6a85069d66785d2e7b0ae22f4a871d0226ac7e93467a5b2aa1f97ede1297ed79f5d67bbfdf7624522d39230a6ebe0bebdfe98c3d29629b7006a0178b7884bc3df7c2a883b1f5ddbfa9a224ec5303b2b7023c29ed54fb8b10e26b00000000000000209b45a957a15db446f862707a6baabfce86f7d09679dea86b07df1a8fda637b9bddc861726c33362297c67414073b1db9",
74-
chain,
74+
chain: 'mumbai',
7575
authSig,
7676
});
7777

7878
const decryptedString = await LitJsSdk.decryptString(
79-
decrypted64,
79+
b64toBlob(decrypted64),
8080
symmetricKey
8181
);
8282

‎src/pages/decrypt.js

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
2+
import LitJsSdk from "lit-js-sdk";
3+
import { useEffect, useRef, useState } from "react";
4+
5+
6+
const litSDK = new LitJsSdk.LitNodeClient();
7+
litSDK.connect();
8+
9+
10+
11+
const DPage = () => {
12+
const litRef = useRef()
13+
const [s, setS] = useState();
14+
15+
useEffect(() => {
16+
if (litRef.current) return;
17+
const litSDK = new LitJsSdk.LitNodeClient();
18+
litSDK.connect();
19+
litRef.current = litSDK
20+
21+
const dec = async () => {
22+
setTimeout(async () => {
23+
24+
25+
const decrypted64 = 'data:application/octet-stream;base64,hPwX+Go0DRsz6HaYmlv2io3nAhfoL/JnwPYHc4WqCJc=';
26+
27+
const base64Response = await fetch(decrypted64);
28+
29+
const myBlob = await base64Response.blob();
30+
31+
32+
const symmetricKey = await litRef.current.getEncryptionKey({
33+
accessControlConditions,
34+
toDecrypt: "58700c72e794465b6882676b4726108728d717e8c645d6d4350230336c63d2aceaf577b7626f6a85069d66785d2e7b0ae22f4a871d0226ac7e93467a5b2aa1f97ede1297ed79f5d67bbfdf7624522d39230a6ebe0bebdfe98c3d29629b7006a0178b7884bc3df7c2a883b1f5ddbfa9a224ec5303b2b7023c29ed54fb8b10e26b00000000000000209b45a957a15db446f862707a6baabfce86f7d09679dea86b07df1a8fda637b9bddc861726c33362297c67414073b1db9",
35+
chain: 'mumbai',
36+
authSig,
37+
});
38+
39+
const decryptedString = await LitJsSdk.decryptString(
40+
myBlob,
41+
symmetricKey
42+
);
43+
44+
console.log(12, s)
45+
46+
setS(decryptedString);
47+
48+
49+
}, 1000)
50+
}
51+
dec()
52+
}, [])
53+
54+
const accessControlConditions = [
55+
{
56+
contractAddress: '',
57+
standardContractType: '',
58+
chain: 'mumbai',
59+
method: 'eth_getBalance',
60+
parameters: [':userAddress', 'latest'],
61+
returnValueTest: {
62+
comparator: '>=',
63+
value: '0',
64+
},
65+
},
66+
];
67+
68+
const authSig = {
69+
"sig": "0x5322d8c8cbfc654e9461ecd3ca101a4f55cede22324c90cb0076538a2fe782cd20706f5abf58d678daee701f7e5ce183e73f37985006815907031513b7c768521b",
70+
"derivedVia": "web3.eth.personal.sign",
71+
"signedMessage": "localhost:3000 wants you to sign in with your Ethereum account:\n0x6f3c67DA2827aaE123d1379939E2E7294d62C06B\n\n\nURI: http://localhost:3000\nVersion: 1\nChain ID: 80001\nNonce: M4bD84RbTz8TD9VyA\nIssued At: 2023-03-05T08:02:30.254Z",
72+
"address": "0x6f3c67da2827aae123d1379939e2e7294d62c06b"
73+
}
74+
75+
return <div>{s}</div>
76+
77+
78+
79+
}
80+
81+
export default DPage;

0 commit comments

Comments
 (0)
Please sign in to comment.