75 lines
2.9 KiB
JavaScript
75 lines
2.9 KiB
JavaScript
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.");
|
|
|
|
const lockDefintion = argv["lock-definition"].js || argv["lock-definition"];
|
|
|
|
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.");
|
|
}
|
|
|
|
})
|
|
})
|
|
|
|
} |