Genesis commit
This commit is contained in:
75
lib/commands/add-lock.js
Normal file
75
lib/commands/add-lock.js
Normal file
@@ -0,0 +1,75 @@
|
||||
const persistence = require("../core/persistence/persistence");
|
||||
const cryptoUtil = require("../core/cryptoUtil");
|
||||
|
||||
module.exports.description = "Add lock which we will use our keys on."
|
||||
|
||||
module.exports.builder = (yargs) => {
|
||||
return yargs
|
||||
.usage(`Usage: $0 add-lock [options]`)
|
||||
.help(false)
|
||||
.version(false)
|
||||
.option('lock-definition', {
|
||||
describe: 'New lock to add to our system',
|
||||
type: 'string'
|
||||
})
|
||||
.demandOption(['lock-definition'])
|
||||
.argv;
|
||||
}
|
||||
|
||||
module.exports.handler = (argv) => {
|
||||
console.log("Add lock which we will use our keys on.");
|
||||
console.log("Lock Definition: ", argv["lock-definition"]);
|
||||
|
||||
const lockDefintion = argv["lock-definition"] || argv["lock-definition"].js;
|
||||
const challenge = JSON.parse(lockDefintion);
|
||||
const registerationMessage = JSON.parse(challenge.message);
|
||||
|
||||
// TODO: Validate lock-definition is of the correct format...
|
||||
persistence().LoadDB()
|
||||
.then(db => {
|
||||
// TODO validate lock definition has all the parameters we need
|
||||
// TODO: Verify challenge comes from a service we know...
|
||||
return db.ExtendedPublicKey.findOne({
|
||||
where: {
|
||||
xpub: challenge.xpub
|
||||
},
|
||||
include: [
|
||||
{
|
||||
association: db.ExtendedPublicKey.Key // TODO: update this to wallet
|
||||
}
|
||||
]
|
||||
}).then(extendedPublicKey => {
|
||||
if (extendedPublicKey) {
|
||||
const encryptedKey = extendedPublicKey.key.encryptedPrivateKey;
|
||||
const password = "vanished";
|
||||
const walletXpriv = cryptoUtil.decrypt(encryptedKey, password);
|
||||
|
||||
var challengeDerivationPath = `${extendedPublicKey.derivationPath}/${challenge.derivationPath.split("c/")[1]}`;
|
||||
|
||||
const signature = cryptoUtil.signMessage(walletXpriv, challengeDerivationPath, challenge.message)
|
||||
|
||||
|
||||
// TODO: save lock
|
||||
return db.Lock.create({
|
||||
userIdentifier: registerationMessage.userIdentifier,
|
||||
url: registerationMessage.url,
|
||||
signature: signature.toString('hex'),
|
||||
message: challenge.message,
|
||||
extendedPublicKeyId: extendedPublicKey.id
|
||||
})
|
||||
} else {
|
||||
console.error("Sorry we can't create a lock with xpub: ", challenge.xpub);
|
||||
return null;
|
||||
}
|
||||
}).then(lock => {
|
||||
if(lock) {
|
||||
console.log("Lock: ", lock.id);
|
||||
console.log("Signature: ", lock.signature);
|
||||
} else {
|
||||
console.error("Failed to create the lock.");
|
||||
}
|
||||
|
||||
})
|
||||
})
|
||||
|
||||
}
|
||||
73
lib/commands/authenticate-challenge.js
Normal file
73
lib/commands/authenticate-challenge.js
Normal file
@@ -0,0 +1,73 @@
|
||||
const persistence = require("../core/persistence/persistence");
|
||||
const cryptoUtil = require("../core/cryptoUtil");
|
||||
|
||||
module.exports.description = "Sign a challenge to authenticate papao"
|
||||
|
||||
module.exports.builder = (yargs) => {
|
||||
return yargs
|
||||
// .usage(`Usage: $0 add-lock [options]`)
|
||||
.help(false)
|
||||
.version(false)
|
||||
.option('challenge', {
|
||||
describe: 'challenge that needs to be signed',
|
||||
type: 'string'
|
||||
})
|
||||
.option('domain-url', {
|
||||
describe: 'override domain url belong to the service making the challenge',
|
||||
type: 'string'
|
||||
})
|
||||
.option('user-identifier', {
|
||||
describe: 'override user identifier which will be used to sign this challenge',
|
||||
type: 'string'
|
||||
})
|
||||
.demandOption(['challenge'])
|
||||
.argv;
|
||||
}
|
||||
|
||||
module.exports.handler = (argv) => {
|
||||
const challenge = JSON.parse(argv.challenge);
|
||||
// TODO get userIdentifier and domainUrl from challenge
|
||||
const loginRequest = JSON.parse(challenge.message);
|
||||
const domainUrl = argv.domainUrl || loginRequest.domainUrl;
|
||||
const userIdentifier = argv.userIdentifier || loginRequest.userIdentifier;
|
||||
persistence().LoadDB()
|
||||
.then(db => {
|
||||
console.log("Looking for");
|
||||
console.log("url: ", domainUrl);
|
||||
console.log("userIdentifier: ", userIdentifier);
|
||||
|
||||
return db.Lock.findOne({
|
||||
where: {
|
||||
url: domainUrl,
|
||||
userIdentifier: userIdentifier
|
||||
},
|
||||
include: [
|
||||
{
|
||||
association: db.Lock.ExtendedPublicKey,
|
||||
require: true,
|
||||
include: [
|
||||
{
|
||||
association: db.ExtendedPublicKey.Key, // TODO rename key to wallet
|
||||
required: true
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
})
|
||||
.then(lock => {
|
||||
if(lock) {
|
||||
// Sign the message with the key that corresponds with this lock...
|
||||
const encryptedKey = lock.extendedPublicKey.key.encryptedPrivateKey;
|
||||
const password = "vanished";
|
||||
const walletXpriv = cryptoUtil.decrypt(encryptedKey, password);
|
||||
|
||||
var challengeDerivationPath = `${lock.extendedPublicKey.derivationPath}/${challenge.derivationPath.split("c/")[1]}`;
|
||||
|
||||
const signature = cryptoUtil.signMessage(walletXpriv, challengeDerivationPath, challenge.message)
|
||||
console.log("Signature: ", signature.toString('hex'));
|
||||
} else {
|
||||
console.error("Failed to find a lock with these parameters");
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
41
lib/commands/create-keys.js
Normal file
41
lib/commands/create-keys.js
Normal file
@@ -0,0 +1,41 @@
|
||||
const persistence = require("../core/persistence/persistence");
|
||||
const cryptoUtil = require("../core/cryptoUtil");
|
||||
|
||||
const bip39 = require('bip39');
|
||||
const bip32 = require('bip32');
|
||||
|
||||
module.exports.description = "Create keys using a randomly generated seed";
|
||||
|
||||
module.exports.builder = (yargs) => {
|
||||
|
||||
}
|
||||
|
||||
module.exports.handler = (argv) => {
|
||||
console.log("Create keys using a randomly generated seed");
|
||||
persistence().LoadDB()
|
||||
.then(db => {
|
||||
const mnemonic = bip39.generateMnemonic();
|
||||
console.log("Backup generated Mnemonic: ", mnemonic);
|
||||
|
||||
// TODO: validate user backed up mnemonic
|
||||
const seed = bip39.mnemonicToSeedSync(mnemonic);
|
||||
console.log("Seed: ", seed.toString('hex'))
|
||||
const node = bip32.fromSeed(seed);
|
||||
console.log("xpriv: ", node.toBase58());
|
||||
|
||||
// TODO: Get user key password
|
||||
const password = "vanished";
|
||||
// TODO: Encrypt text securely...
|
||||
const encryptedPrivateKey = cryptoUtil.encrypt(node.toBase58(), password);
|
||||
|
||||
console.log("Encrypted Private Key: ", encryptedPrivateKey);
|
||||
|
||||
// Check if a key exist...
|
||||
db.Key.create({
|
||||
encryptedPrivateKey: encryptedPrivateKey,
|
||||
name: "FirstKey"
|
||||
}).then(key => {
|
||||
console.log("Successfully created: ", key.name)
|
||||
})
|
||||
})
|
||||
}
|
||||
9
lib/commands/drop-keys.js
Normal file
9
lib/commands/drop-keys.js
Normal file
@@ -0,0 +1,9 @@
|
||||
module.exports.description = "Drop the keys we have instantiated"
|
||||
|
||||
module.exports.builder = (yargs) => {
|
||||
|
||||
}
|
||||
|
||||
module.exports.handler = (argv) => {
|
||||
console.log("Drop the keys we have instantiated");
|
||||
}
|
||||
59
lib/commands/random-xpub.js
Normal file
59
lib/commands/random-xpub.js
Normal file
@@ -0,0 +1,59 @@
|
||||
const persistence = require("../core/persistence/persistence");
|
||||
const cryptoUtil = require("../core/cryptoUtil");
|
||||
|
||||
const bip32 = require('bip32');
|
||||
|
||||
module.exports.description = "Get random xpub to use on a service"
|
||||
|
||||
module.exports.builder = (yargs) => {
|
||||
return yargs.argv;
|
||||
}
|
||||
|
||||
module.exports.handler = (argv) => {
|
||||
console.log("Get random xpub to use on a service");
|
||||
persistence().LoadDB()
|
||||
.then(db => {
|
||||
db.Key.findOne({
|
||||
where: {
|
||||
name: "FirstKey"
|
||||
}
|
||||
})
|
||||
.then(key => {
|
||||
// TODO: Load password from config...
|
||||
if(key) {
|
||||
const password = "vanished";
|
||||
|
||||
console.log("Encrypted: ", key.encryptedPrivateKey);
|
||||
|
||||
const decryptedPrivateKey = cryptoUtil.decrypt(key.encryptedPrivateKey, password);
|
||||
console.log("Decrypted: ", decryptedPrivateKey);
|
||||
const masterNode = bip32.fromBase58(decryptedPrivateKey);
|
||||
|
||||
console.log("Node: ", masterNode.toBase58());
|
||||
|
||||
const derivationPath = `m/${cryptoUtil.randomDerivationPath(true)}`
|
||||
|
||||
const xpubNode = masterNode.derivePath(derivationPath);
|
||||
console.log("Xpub: ", xpubNode.neutered().toBase58());
|
||||
|
||||
// TODO save xpub and derivation path in db...
|
||||
return db.ExtendedPublicKey.create({
|
||||
xpub: xpubNode.neutered().toBase58(),
|
||||
derivationPath: derivationPath,
|
||||
keyId: key.id // TODO rename this to walletID
|
||||
// TODO add name if availabe...
|
||||
}).catch(error => {
|
||||
console.error("Failed to create extended public key: ", error);
|
||||
})
|
||||
} else {
|
||||
console.error("Couldn't create xpub without key")
|
||||
}
|
||||
})
|
||||
.then(extendedPublicKey => {
|
||||
console.log("Extened public key: ", extendedPublicKey.xpub);
|
||||
})
|
||||
.catch(error => {
|
||||
console.error("Failed to find key: ", error);
|
||||
})
|
||||
})
|
||||
}
|
||||
9
lib/commands/restore-keys.js
Normal file
9
lib/commands/restore-keys.js
Normal file
@@ -0,0 +1,9 @@
|
||||
module.exports.description = "Restore keys using a seed if keys haven't been in"
|
||||
|
||||
module.exports.builder = (yargs) => {
|
||||
|
||||
}
|
||||
|
||||
module.exports.handler = (argv) => {
|
||||
console.log("Restore keys using a seed if keys haven't been in");
|
||||
}
|
||||
18
lib/commands/show-keys.js
Normal file
18
lib/commands/show-keys.js
Normal file
@@ -0,0 +1,18 @@
|
||||
const persistence = require("../core/persistence/persistence");
|
||||
|
||||
module.exports.description = "Show the keys we have instantiate"
|
||||
|
||||
module.exports.builder = (yargs) => {
|
||||
|
||||
}
|
||||
|
||||
module.exports.handler = (argv) => {
|
||||
console.log("Show the keys we have instantiate");
|
||||
persistence().LoadDB()
|
||||
.then(db => {
|
||||
db.Key.findAll()
|
||||
.then(keys => {
|
||||
console.log("Keys: ", keys.map(k => k.name));
|
||||
})
|
||||
})
|
||||
}
|
||||
19
lib/commands/show-locks.js
Normal file
19
lib/commands/show-locks.js
Normal file
@@ -0,0 +1,19 @@
|
||||
const persistence = require("../core/persistence/persistence");
|
||||
|
||||
module.exports.description = "Show all locks our keys have access to"
|
||||
module.exports.builder = (yargs) => {
|
||||
|
||||
}
|
||||
|
||||
module.exports.handler = (argv) => {
|
||||
console.log("Show all locks our keys have access to.");
|
||||
persistence().LoadDB()
|
||||
.then(db => {
|
||||
db.Lock.findAll()
|
||||
.then(locks => {
|
||||
console.log("locks: ", locks.map(l => {
|
||||
return l
|
||||
}));
|
||||
})
|
||||
})
|
||||
}
|
||||
25
lib/commands/show-xpubs.js
Normal file
25
lib/commands/show-xpubs.js
Normal file
@@ -0,0 +1,25 @@
|
||||
const persistence = require("../core/persistence/persistence");
|
||||
|
||||
module.exports.description = "Show the ExtendedPublicKeys we have created"
|
||||
|
||||
module.exports.builder = (yargs) => {
|
||||
|
||||
}
|
||||
|
||||
module.exports.handler = (argv) => {
|
||||
console.log("Show the ExtendedPublicKeys we have created");
|
||||
persistence().LoadDB()
|
||||
.then(db => {
|
||||
db.ExtendedPublicKey.findAll()
|
||||
.then(extendedPublicKey => {
|
||||
console.log("Keys: ", extendedPublicKey.map(k => {
|
||||
return {
|
||||
id: k.id,
|
||||
name: k.name,
|
||||
xpub: k.xpub,
|
||||
derivationPath: k.derivationPath
|
||||
};
|
||||
}));
|
||||
})
|
||||
})
|
||||
}
|
||||
9
lib/commands/verify-message.js
Normal file
9
lib/commands/verify-message.js
Normal file
@@ -0,0 +1,9 @@
|
||||
module.exports.description = "Verify a message was signed by a specific key"
|
||||
|
||||
module.exports.builder = (yargs) => {
|
||||
|
||||
}
|
||||
|
||||
module.exports.handler = (argv) => {
|
||||
console.log("Verify message using one of the keys");
|
||||
}
|
||||
Reference in New Issue
Block a user