Commit 4f9a15ca authored by kenshin-samourai's avatar kenshin-samourai
Browse files

code refactoring + improve ux

parent 7c7e1edc
......@@ -22,6 +22,32 @@ function derivePaymentAddresses(pcode, key, idx, network) {
return [p2pkh, p2wpkh, p2sh]
}
function isValidMnemonic(a_mnemonic) {
return bip39.validateMnemonic(a_mnemonic)
}
function isValidPaymentCode(b_pcode_b58, network) {
try {
bip47.fromBase58(b_pcode_b58, network)
return true
} catch(e) {
return false
}
}
function checkMnemonic() {
const a_mnemonic = $('#alice_mnemonic').val()
if (!isValidMnemonic(a_mnemonic))
alert('This is not a valid mnemonic')
}
function checkPaymentCode() {
const b_pcode_b58 = $('#bob_paymentcode').val()
const network = bitcoinjs.networks[$('#network').val()]
if (b_pcode_b58 != '' && !isValidPaymentCode(b_pcode_b58, network))
alert('This is not a valid payment code')
}
function refresh() {
const a_p2pkh_addr = []
const a_p2wpkh_addr = []
......@@ -29,8 +55,18 @@ function refresh() {
const b_p2pkh_addr = []
const b_p2wpkh_addr = []
const b_p2sh_addr = []
let a_addr_list = ''
let b_addr_list = ''
let a_pcode = ''
let b_pcode = ''
let a_notif_privkey = ''
let b_notif_pubkey = ''
let a_notif_addr = ''
let b_notif_addr = ''
//
// Retrieve data from form
//
const network = bitcoinjs.networks[$('#network').val()]
const nb_addr_deriv = parseInt($('#derivation_nb_addresses').val())
const offset_deriv = parseInt($('#derivation_start_index').val())
......@@ -38,72 +74,90 @@ function refresh() {
const a_passphrase = $('#alice_passphrase').val()
const b_pcode_b58 = $('#bob_paymentcode').val()
//
// Clear results
//
$('#alice_paymentcode').html('')
$('#alice_notif_addr').text('')
$('#bob_notif_addr').text('')
$('#alice_payment_addresses').html('')
$('#bob_payment_addresses').html('')
// Check data
if (!bip39.validateMnemonic(a_mnemonic)) {
alert('This is not a valid mnemonic')
return
}
//
// Check validity of input data
//
const hasValidMnemonic = isValidMnemonic(a_mnemonic)
const hasValidPaymentCode = isValidPaymentCode(b_pcode_b58, network)
//
// Compute bip47 derivations
const a_seed = bip39.mnemonicToSeedSync(a_mnemonic, a_passphrase)
const a_pcode = bip47.fromWalletSeed(a_seed, 0, network)
const a_notif_privkey = a_pcode.derive(0).privateKey
const a_notif_addr = a_pcode.getNotificationAddress()
const b_pcode = bip47.fromBase58(b_pcode_b58, network)
const b_notif_pubkey = b_pcode.derive(0).publicKey
const b_notif_addr = b_pcode.getNotificationAddress()
//
if (hasValidMnemonic) {
const a_seed = bip39.mnemonicToSeedSync(a_mnemonic, a_passphrase)
a_pcode = bip47.fromWalletSeed(a_seed, 0, network)
a_notif_privkey = a_pcode.derive(0).privateKey
a_notif_addr = a_pcode.getNotificationAddress()
}
for (let i=0; i<nb_addr_deriv; i++) {
const idx = offset_deriv + i
const a_res = derivePaymentAddresses(a_pcode, b_notif_pubkey, idx, network)
a_p2pkh_addr.push(a_res[0])
a_p2wpkh_addr.push(a_res[1])
a_p2sh_addr.push(a_res[2])
const b_res = derivePaymentAddresses(b_pcode, a_notif_privkey, idx, network)
b_p2pkh_addr.push(b_res[0])
b_p2wpkh_addr.push(b_res[1])
b_p2sh_addr.push(b_res[2])
if (hasValidPaymentCode) {
b_pcode = bip47.fromBase58(b_pcode_b58, network)
b_notif_pubkey = b_pcode.derive(0).publicKey
b_notif_addr = b_pcode.getNotificationAddress()
}
// Prepare ard display results
let a_addr_list = ''
let b_addr_list = ''
if (hasValidMnemonic && hasValidPaymentCode) {
for (let i=0; i<nb_addr_deriv; i++) {
const idx = offset_deriv + i
const a_res = derivePaymentAddresses(a_pcode, b_notif_pubkey, idx, network)
a_p2pkh_addr.push(a_res[0])
a_p2wpkh_addr.push(a_res[1])
a_p2sh_addr.push(a_res[2])
const b_res = derivePaymentAddresses(b_pcode, a_notif_privkey, idx, network)
b_p2pkh_addr.push(b_res[0])
b_p2wpkh_addr.push(b_res[1])
b_p2sh_addr.push(b_res[2])
}
}
for (let i=0; i<nb_addr_deriv; i++) {
//
// Prepare and display the results
//
for (let i=0; i<a_p2pkh_addr.length; i++) {
const idx = offset_deriv + i
a_addr_list += `<br/><br/><b>Address #${idx}</b>`
a_addr_list += `<b>ADDRESS #${idx}</b>`
a_addr_list += `<br/> ${a_p2pkh_addr[i]}`
a_addr_list += `<br/> ${a_p2wpkh_addr[i]}`
a_addr_list += `<br/> ${a_p2sh_addr[i]}`
a_addr_list += `<br/><br/>`
b_addr_list += `<br/><br/><b>Address #${idx}</b>`
b_addr_list += `<b>ADDRESS #${idx}</b>`
b_addr_list += `<br/> ${b_p2pkh_addr[i]}`
b_addr_list += `<br/> ${b_p2wpkh_addr[i]}`
b_addr_list += `<br/> ${b_p2sh_addr[i]}`
b_addr_list += `<br/><br/>`
}
$('#alice_paymentcode').html(a_pcode.toBase58())
$('#alice_notif_addr').text(a_notif_addr)
$('#bob_notif_addr').text(b_notif_addr)
$('#alice_payment_addresses').html(a_addr_list)
$('#bob_payment_addresses').html(b_addr_list)
if (hasValidMnemonic) {
$('#alice_paymentcode').html(a_pcode.toBase58())
$('#alice_notif_addr').text(a_notif_addr)
}
if (hasValidPaymentCode) {
$('#bob_notif_addr').text(b_notif_addr)
}
if (hasValidMnemonic && hasValidPaymentCode) {
$('#alice_payment_addresses').html(a_addr_list)
$('#bob_payment_addresses').html(b_addr_list)
}
}
// On page ready
$(document).ready(function() {
$('#network').change(evt => { refresh() })
$('#alice_mnemonic').change(evt => { refresh() })
$('#alice_passphrase').change(evt => { refresh() })
$('#bob_paymentcode').change(evt => { refresh() })
$('#alice_mnemonic').keyup(evt => { refresh() })
$('#alice_mnemonic').change(evt => { checkMnemonic() })
$('#alice_passphrase').keyup(evt => { refresh() })
$('#bob_paymentcode').keyup(evt => { refresh() })
$('#bob_paymentcode').change(evt => { checkPaymentCode() })
$('#derivation_nb_addresses').change(evt => { refresh() })
$('#derivation_start_index').change(evt => { refresh() })
$('#derivation_start_index').keyup(evt => { refresh() })
})
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment