-
Notifications
You must be signed in to change notification settings - Fork 1
/
AddCard.js
54 lines (47 loc) · 1.34 KB
/
AddCard.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const functions = require('@google-cloud/functions-framework');
const crypto = require("crypto");
const Firestore = require("@google-cloud/firestore");
const {
Account,
} = require("@aptos-labs/ts-sdk");
const privateKey = ``;
const db = new Firestore({
projectId: "evault",
keyFilename: "credential.json",
});
const Accounts = db.collection("XXXXXXXXX");
functions.http('helloHttp', async (req, res) => {
try {
const decrypted = decryptText(req.body.data);
const pubKey = req.body.pubKey;
const query = await Accounts.where("publicKey", "==", pubKey).get();
if (query.empty) {
const account = Account.generate();
const cardPublicKey = account.accountAddress.toString();
const cardPrivateKey = '0x'+HexString(account.privateKey.toUint8Array());
await Accounts.doc(pubKey).set({
cardHash: decrypted.toString(),
publicKey: pubKey,
cardPublicKey,
cardPrivateKey
});
res.send(cardPublicKey);
} else {
throw "Bad Request";
}
} catch (e) {
res.send(`Bad Request`);
}
});
// utils
function decryptText(encryptedText) {
return crypto.privateDecrypt(
{
key: privateKey,
},
Buffer.from(encryptedText, "base64")
);
}
function HexString(uint8Array) {
return Array.from(uint8Array, (byte) => byte.toString(16).padStart(2, '0')).join('');
}