Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New feature: ?import= allows importing BASE64-encoded Wallet JSON directly #72

Merged
merged 4 commits into from May 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
121 changes: 121 additions & 0 deletions src/client/pages/Home.page.vue
Expand Up @@ -100,6 +100,7 @@

self.loadPoolSettings();
});

},

destroyed() {
Expand All @@ -113,11 +114,131 @@

methods: {
_blockchainStatus(data) {
//console.log("blockchain/status - " + data.message);
if (data.message === "Single Window") {
this.protocolUsedOnMultipleTabs = false;
} else if (data.message === "Multiple Windows Detected") {
this.protocolUsedOnMultipleTabs = true;
} else if (data.message === "Wallet Loaded Successfully"
|| data.message === "Wallet Creating New Wallet") {
// Now that wallet is loaded, check ?import= if we have a new address to add
if (typeof this.$route.query.import === "string") {
try {
this._validateBase64String(this.$route.query.import); // throws error if faulty
let wallet = window.atob(this.$route.query.import);
// No matter if the wallet JSON is technically valid, if it doesn't start with
// the version key, we're not looking at it.
if (wallet.substring(0, 11) === '{"version":') {
let data = JSON.parse(wallet);
if (this._validateWalletObject(data)) { // throws error if faulty
let answer = WebDollar.Blockchain.Wallet.importAddressFromJSON(data);
if (answer.result === true) {
console.log("Query-string Address Import Successful!");
}
}
} else {
throw new Error("Wallet JSON does not start with version");
}
} catch (err) {
console.log("Error decoding wallet-address import querystring: " + err);
}
}
}
},

_validateBase64String(data) {
let i, c, valid;
for (i=0; i < data.length; i++) {
valid = false;
c = data[i];
if (c >= 'A' && c <= 'Z') {
valid = true;
}
if (c >= 'a' && c <= 'z') {
valid = true;
}
if (c >= '1' && c <= '9') {
valid = true;
}
if (c === '0' || c === '+' || c === '/' || c === '=') {
valid = true;
}
if (!valid) {
throw new Error("Invalid base64");
}
}
return true;
},

_validateWalletObject(data) {
let errors = "";

if (typeof data.version === "undefined") {
errors = errors + "missing version/";
} else {
// Validation for version 0.1
if (data.version === "0.1") {
if (typeof data.address === "undefined") {
errors = errors + "missing address/";
} else if (data.address.length !== 40 ||
data.address.substring(0,5) !== 'WEBD$' ||
data.address[39] != '$') {
errors = errors + "malformed address/";
}
if (typeof data.publicKey === "undefined") {
errors = errors + "missing publicKey/";
} else if (data.publicKey.length !== 64 ||
!this._validateHexStream(data.publicKey)) {
errors = errors + "malformed publicKey/";
}
if (typeof data.privateKey === "undefined") {
errors = errors + "missing privateKey/";
} else if (data.privateKey.length !== 138 ||
!this._validateHexStream(data.privateKey)) {
errors = errors + "malformed privateKey/";
}
let allKeys = Object.keys(data);
let i;
for (i=0; i < allKeys.length; i++) {
if (allKeys[i] !== "version" &&
allKeys[i] !== "address" &&
allKeys[i] !== "publicKey" &&
allKeys[i] !== "privateKey") {
errors = errors + "unrecognized key: " + allKeys[i] + "/";
}
}
} else {
errors = errors + "invalid wallet version/";
}
}
if (errors !== "") {
throw new Error(errors);
}
return true;
},

_validateHexStream(data) { // returns true or false, doesn't throw
let i, c, valid;
for (i=0; i < data.length; i++) {
c = data[i];
valid = false;
if (c >= 'A' && c <= 'F') {
valid = true;
}
if (c >= 'a' && c <= 'f') {
valid = true;
}
if (c >= '1' && c <= '9') {
valid = true;
}
if (c === '0') {
valid = true;
}
if (!valid) {
return false;
}
}
return true;
},

_blockchainLogs(data) {
Expand Down