0

I'm working with bitcoinjs, but is there a litecoinjs equivalent? I want to be able to sign and verify messages with my litecoin private key and also generate litecoin addresses from a string. How would one go about doing these functions with a javascript library if one exists?

  • This seems like an X/Y problem. What are you trying to accomplish? – user48462 Jun 15 '17 at 18:14
  • trying to generate a ltc address using a string and then use that ltc private key to sign a message. – Patoshi パトシ Jun 15 '17 at 18:26
  • Generating an address from a string? That's called a brainwallet, but is HORRIBLY INSECURE if you don't know what you're doing. – user48462 Jun 16 '17 at 10:12
  • It's insecure if your not using another hash algorithm to strengthen it like scrypt that is used in the warp wallet generator. It's more secure than trying to write down your private key or seed somewhere. You can easily remember your brain wallet if done correctly. It's a silly misconception that ALL brain wallets are insecure, which is not true at all. – Patoshi パトシ Jun 16 '17 at 22:59
  • Indeed, a properly generated brainwallet might be secure, but I just wanted to warn you. – user48462 Jun 17 '17 at 8:57
4
+50

bitcoinjs already has support for litecoin, a quick look at /src/network.js and the README reveals this. https://github.com/bitcoinjs/bitcoinjs-lib/blob/d853806/test/integration/basic.js#L30

See how they set the network: litecoin variable, that is probably used consistently through the code (atleast for address generation).

Nothing really major changes for signing to be honest, take a look at https://github.com/bitcoinjs/bitcoinjs-message


WIF and address generation Do not take the warning about the cryptographically secure random number generator lightly. If you do not change it then you will constantly generate the same address and it will be horribly insecure.

//import bitcoinjs libs
var bitcoin = require('bitcoinjs-lib') // v2.x.x
var bitcoinMessage = require('bitcoinjs-message')

//set a litecoin variable equal to its network type, which we'll use throughout the example
var litecoin = bitcoin.networks.litecoin

//let's generate a litecoin keypair from a string
//WARNING: YOU MUST REPLACE rng() WITH A FUNCTION THAT ACTUALLY RETURNS CRYPTOGRAPHICALLY SECURE RANDOM DATA!
function rng () { return Buffer.from('MAKE SURE THIS IS NEW RANDOM DATA EACH TIME RNG() IS CALLED') }

var keyPair = bitcoin.ECPair.makeRandom({ network: litecoin, rng: rng })


var wif = keyPair.toWIF()
//You should store the output of the variable wif here, this is the actual key to the litecoin address (contains the private key) and is crucial to signing messages, do not share this and handle with care.

var address = keyPair.getAddress()

console.log("wif: " + wif + "\n")
console.log("address: " + address + "\n")

Message signing using WIF from example1

This takes your WIF and then signs a message.

//import bitcoinjs libs
var bitcoin = require('bitcoinjs-lib') // v2.x.x
var bitcoinMessage = require('bitcoinjs-message')

//set a litecoin variable equal to its network type, which we'll use throughout the example
var litecoin = bitcoin.networks.litecoin

// add the wif key you stored
var wif = 'WIF key goes here'

var keyPair = bitcoin.ECPair.fromWIF(wif, litecoin)
var privateKey = keyPair.d.toBuffer(32)
var message = 'This is an example of a signed message.'
var messagePrefix = litecoin.messagePrefix

var signature = bitcoinMessage.sign(message, messagePrefix, privateKey, keyPair.compressed)
console.log(signature.toString('base64'))

Replace the WIF by the one you generated in the previous example.

Message verification using the address from example1, and signature output from example2

//import bitcoinjs libs
var bitcoin = require('bitcoinjs-lib') // v2.x.x
var bitcoinMessage = require('bitcoinjs-message')

//set a litecoin variable equal to its network type, which we'll use throughout the example
var litecoin = bitcoin.networks.litecoin

var address = 'THE ADDRESS FROM EXAMPLE1'
var signature = 'THE OUTPUT OF CONSOLE LOG OF EXAMPLE2'
var message = 'This is an example of a signed message.'
var messagePrefix = litcoin.messagePrefix

console.log(bitcoinMessage.verify(message, messagePrefix, address, signature))
  • any good example code I can copy and just modify? – Patoshi パトシ Jun 15 '17 at 3:30
  • 1
    Hi duckx, The above example is almost complete, I'll add address generation and edit the main post. – Penquin Jun 15 '17 at 8:30
  • 1
    I've added everything to it: address generation, message signing and message verification. – Penquin Jun 15 '17 at 9:03
  • 1
    I've fully based this code upon the example provided in the test suite. makeRandom does take the rng argument: github.com/bitcoinjs/bitcoinjs-lib/blob/… Which then in turn returns a new ECPair that takes the network argument. github.com/bitcoinjs/bitcoinjs-lib/blob/… – Penquin Jun 17 '17 at 11:10
  • 1
    @EvilJordan it remains a good observation to make however. (re: deterministic signatures) – Penquin Feb 16 '18 at 20:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.