Skip to content

Instantly share code, notes, and snippets.

@akeem
Created February 14, 2020 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akeem/94c02d73f5f03fcd3aa2a9b2eec78eec to your computer and use it in GitHub Desktop.
Save akeem/94c02d73f5f03fcd3aa2a9b2eec78eec to your computer and use it in GitHub Desktop.
Rough POC generating wallet material
<!DOCTYPE html>
<html>
<head>
<title>Wallet Gen</title>
<meta charset="utf-8" />
<meta
name="google-signin-client_id"
content="add-client-id-here"
/>
</head>
<body>
<p>Wallet Gen</p>
<div class="g-signin2" data-onsuccess="updateSigninStatus"></div>
<a href="#" onclick="signOut();">Sign out</a>
<script
src="https://cdn.ethers.io/scripts/ethers-v4.min.js"
charset="utf-8"
type="text/javascript"
></script>
<script src="https://apis.google.com/js/platform.js" async defer></script>
<script async defer src="./lib.js"></script>
<script
async
defer
src="https://apis.google.com/js/api.js"
onload="this.onload=function(){};handleClientLoad()"
onreadystatechange="if (this.readyState === 'complete') this.onload()"
></script>
</body>
</html>
var CLIENT_ID = "CLIENT_ID";
var API_KEY = "API_KEY";
var DISCOVERY_DOCS = [
"https://www.googleapis.com/discovery/v1/apis/drive/v3/rest"
];
var SCOPES =
"https://www.googleapis.com/auth/drive.appdata https://www.googleapis.com/auth/drive.metadata.readonly";
function handleClientLoad() {
gapi.load("client:auth2", initClient);
}
/* Currently the assumption is that ethers is in scope */
function generateWallet() {
return ethers.Wallet.createRandom();
}
async function updateSigninStatus(isSignedIn) {
if (isSignedIn) {
signedInHandler("wallet.json");
}
}
async function signedInHandler(fileName) {
let filePresent = await applicationFilePresent(fileName);
if (!filePresent) {
let data = generateWallet().signingKey;
createFile(fileName, data);
}
}
async function applicationFilePresent(fileName) {
let response = await getAppFile();
let matchedFile = response.result.files.find(file => file.name == fileName);
return !!matchedFile;
}
function getAppFile() {
return gapi.client.drive.files.list({
spaces: "appDataFolder",
fields: "nextPageToken, files(id, name)",
pageSize: 100
});
}
function signOut() {
var auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then(function() {
console.log("User signed out.");
});
}
async function initClient() {
try {
await gapi.client.init({
apiKey: API_KEY,
clientId: CLIENT_ID,
discoveryDocs: DISCOVERY_DOCS,
scope: SCOPES
});
// Listen for sign-in state changes.
gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);
// Handle the initial sign-in state.
updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
} catch (e) {
console.log("error");
}
}
function createFile(name, data, callback) {
const boundary = "-------314159265358979323846";
const delimiter = "\r\n--" + boundary + "\r\n";
const close_delim = "\r\n--" + boundary + "--";
const contentType = "application/json";
let metadata = {
name: name,
mimeType: contentType,
parents: ["appDataFolder"]
};
let multipartRequestBody =
delimiter +
"Content-Type: application/json\r\n\r\n" +
JSON.stringify(metadata) +
delimiter +
"Content-Type: " +
contentType +
"\r\n\r\n" +
data +
close_delim;
var request = gapi.client.request({
path: "/upload/drive/v3/files",
method: "POST",
params: { uploadType: "multipart" },
headers: {
"Content-Type": 'multipart/related; boundary="' + boundary + '"'
},
body: multipartRequestBody
});
if (!callback) {
callback = function(file) {
console.log(file);
};
}
request.execute(callback);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment