I have most of this figured out I think. Originally I could create a signature and successfully authenticate using the generated URL, but it wasn't for the correct account. I had since learned a lot about what I need to do to get the correct account at the right derivation path but in that process I lost the original code.
My current code is as follows, but does not successfully authenticate. I am using SN to test. I am looking for any successful authentication to SN, regardless of if its the correct from the derivation path or not.
I have NO IDEA what my original code did to work and its driving me crazy.
const bitcoin = require('bitcoinjs-lib')
const bitcoinmsg = require('bitcoinjs-message')
const bs58 = require('bs58')
const ecc = require('tiny-secp256k1')
const secp256k1 = require('secp256k1')
const { BIP32Factory } = require('bip32')
const bip32 = BIP32Factory(ecc)
const bip39 = require('bip39')
const {bech32} = require("bech32")
const {ECPairFactory} = require('ecpair')
const ECPair = ECPairFactory(ecc)
const crypto = require('crypto')
function getqueryparams(url) {
const paramarr = url.slice(url.indexOf('?') + 1).split('&')
const params = {}
paramarr.map(param => {
const [key, val] = param.split('=')
params[key] = decodeURIComponent(val)
})
return params;
}
function getbaseurl(url) {
return url.slice(0, url.indexOf('?'))
}
function bech32decode(lnurl) {
const { prefix: hrp, words: datapart } = bech32.decode(lnurl, 2000)
const bytearray = bech32.fromWords(datapart)
return Buffer.from(bytearray).toString()
}
const testkey = crypto.generateKeyPairSync('ec', {namedCurve: 'secp256k1',}).privateKey
const phrase = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon"
const lnurl = ""
const decodedlnurl = bech32decode(lnurl)
const baseurl = getbaseurl(decodedlnurl)
const k1 = getqueryparams(decodedlnurl)['k1']
const k1hash = bitcoin.crypto.sha256(Buffer.from(k1, 'hex'))
const seed = bip39.mnemonicToSeedSync(phrase)
const root = bip32.fromSeed(seed)
const child = root.derivePath("m/138'/0'/0'/0/0") // will get the right path later
const keyPair = ECPair.fromPrivateKey(child.privateKey)
const signature = keyPair.sign(k1hash)
const derSignature = bitcoin.script.signature.encode(signature, bitcoin.Transaction.SIGHASH_ALL)
const verify = keyPair.verify(k1hash, signature);
const domaintest = 'stacker.news'
const url = `${decodedlnurl}&sig=${derSignature.toString('hex')}&key=${child.publicKey.toString('hex')}`
console.log(url)