Skip to content

Instantly share code, notes, and snippets.

@nnkken
Created November 7, 2019 03:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nnkken/41ce03d54d56c0408c7ba056b74009d7 to your computer and use it in GitHub Desktop.
Save nnkken/41ce03d54d56c0408c7ba056b74009d7 to your computer and use it in GitHub Desktop.
Cosmos SDK transaction signing and verifying
const secp256k1 = require('secp256k1');
const bech32 = require('bech32');
const createHash = require('create-hash');
const jsonStringify = require('fast-json-stable-stringify');
function createSigner(privateKey) {
console.log(`private key: ${privateKey.toString('hex')}`);
const publicKey = secp256k1.publicKeyCreate(privateKey, true);
console.log(`public key: ${publicKey.toString('base64')}`);
const sha256 = createHash('sha256');
const ripemd = createHash('ripemd160');
sha256.update(publicKey);
ripemd.update(sha256.digest());
const rawAddr = ripemd.digest();
const cosmosAddress = bech32.encode('cosmos', bech32.toWords(rawAddr));
console.log(`address: ${cosmosAddress}`);
const sign = (msg) => {
const msgSha256 = createHash('sha256');
msgSha256.update(msg);
const msgHash = msgSha256.digest();
const { signature } = secp256k1.sign(msgHash, privateKey);
console.log(`signature: ${signature.toString('base64')}`);
return { msgHash, signature, publicKey };
}
return { cosmosAddress, sign };
}
const privKey = Buffer.from('0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef', 'hex');
const signer = createSigner(privKey);
const signBytes = Buffer.from(jsonStringify({
"fee": {
"amount": [
{
"denom": "nanolike",
"amount": "44000000"
}
],
"gas": "44000"
},
"msgs": [
{
"type": "cosmos-sdk/MsgSend",
"value": {
"from_address": "cosmos1mnyn7x24xj6vraxeeq56dfkxa009tvhgknhm04",
"to_address": "cosmos1ca0zlqxjqv5gek5qxm602umtkmu88564hpyws4",
"amount": [
{
"denom": "nanolike",
"amount": "123456789"
}
]
}
}
],
"chain_id": "likechain-testnet-taipei-1",
"account_number": "21",
"sequence": "0",
"memo": "",
}), 'utf8');
const { msgHash, signature, publicKey } = signer.sign(signBytes);
// private key: 0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef
// public key: A0ZGrlBHMWtCMNAIbIrOxofwCxzZ0dxjT2yzWKwKmo//
// address: cosmos1mnyn7x24xj6vraxeeq56dfkxa009tvhgknhm04
// signature: yXcQvLVEHZMIzZijEgmcL5S7orusBURZoRjWuG1IpoItt5DhY8P9TUaxx31huxV200l6GcEbUlB/Y7jONuf3Bw==
const secp256k1 = require('secp256k1');
const createHash = require('create-hash');
const jsonStringify = require('fast-json-stable-stringify');
const signature = Buffer.from('yXcQvLVEHZMIzZijEgmcL5S7orusBURZoRjWuG1IpoItt5DhY8P9TUaxx31huxV200l6GcEbUlB/Y7jONuf3Bw==', 'base64');
const publicKey = Buffer.from('A0ZGrlBHMWtCMNAIbIrOxofwCxzZ0dxjT2yzWKwKmo//', 'base64');
const msg = jsonStringify({
"fee": {
"amount": [
{
"denom": "nanolike",
"amount": "44000000"
}
],
"gas": "44000"
},
"msgs": [
{
"type": "cosmos-sdk/MsgSend",
"value": {
"from_address": "cosmos1mnyn7x24xj6vraxeeq56dfkxa009tvhgknhm04",
"to_address": "cosmos1ca0zlqxjqv5gek5qxm602umtkmu88564hpyws4",
"amount": [
{
"denom": "nanolike",
"amount": "123456789"
}
]
}
}
],
"chain_id": "likechain-testnet-taipei-1",
"account_number": "21",
"sequence": "0",
"memo": "",
})
const msgSha256 = createHash('sha256');
msgSha256.update(msg);
const msgHash = msgSha256.digest();
console.log(secp256k1.verify(msgHash, signature, publicKey));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment