How can I sign a message with a litecoin private key and also generate a litecoin address from a string?

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?

Patoshi パトシ

Posted 2017-06-12T15:10:25.553

Reputation: 8 911

This seems like an X/Y problem. What are you trying to accomplish? – None – 2017-06-15T18:14:32.693

trying to generate a ltc address using a string and then use that ltc private key to sign a message.Patoshi パトシ 2017-06-15T18:26:24.380

Generating an address from a string? That's called a brainwallet, but is HORRIBLY INSECURE if you don't know what you're doing. – None – 2017-06-16T10:12:43.347

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 パトシ 2017-06-16T22:59:41.180

Indeed, a properly generated brainwallet might be secure, but I just wanted to warn you. – None – 2017-06-17T08:57:33.550

Answers

4

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))

Penquin

Posted 2017-06-12T15:10:25.553

Reputation: 609

any good example code I can copy and just modify?Patoshi パトシ 2017-06-15T03:30:21.653

1Hi duckx,

The above example is almost complete, I'll add address generation and edit the main post. – Penquin 2017-06-15T08:30:46.333

1I've added everything to it: address generation, message signing and message verification.Penquin 2017-06-15T09:03:54.010

function rng () { return Buffer.from('MAKE SURE THIS IS RANDOM DATA') }; var keyPair = bitcoin.ECPair.makeRandom({ network: litecoin, rng: rng }) --- Those 2 lines do not work as makeRandom does not take the network and rng arguments as I looked up the function definition.Patoshi パトシ 2017-06-16T23:01:20.213

1

I've fully based this code upon the example provided in the test suite.

makeRandom does take the rng argument: https://github.com/bitcoinjs/bitcoinjs-lib/blob/d853806d0df9a6a4a18878b973528a2f5c80a29e/src/ecpair.js#L87

Which then in turn returns a new ECPair that takes the network argument. https://github.com/bitcoinjs/bitcoinjs-lib/blob/d853806d0df9a6a4a18878b973528a2f5c80a29e/src/ecpair.js#L15

Penquin 2017-06-17T11:10:07.707

It seems like the sign function will generate the same signature every time, if one passes the same private key and the same message to be signed. That... isn't normal. Am I doing something wrong?EvilJordan 2018-02-08T04:58:36.517

@EvilJordan I just tested it in bitcoin-core and it generates the same signature over and over again for the same address(private key) & the same message.Penquin 2018-02-10T15:39:24.487

@Penquin The code is correct. The implementation I was using prior to this was non-standard, using a RNG and generating non-deterministic, but valid, results.EvilJordan 2018-02-10T20:06:33.137

1@EvilJordan it remains a good observation to make however. (re: deterministic signatures)Penquin 2018-02-16T20:18:11.053