Created
June 9, 2024 22:16
-
-
Save WietseWind/3c030263c6362549a0996f66821d0140 to your computer and use it in GitHub Desktop.
DH secp256k1 nodejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createECDH } from 'crypto' | |
import elliptic from 'elliptic' | |
import { deriveAddress } from 'ripple-keypairs' | |
const curve = 'secp256k1' | |
const secp256k1 = elliptic.ec(curve) | |
const bytesToHex = a => a.map(byteValue => ('0' + byteValue.toString(16)).slice(-2)).join('') | |
const getPublic = echdObj => bytesToHex(secp256k1.keyFromPrivate(echdObj.getPrivateKey()).getPublic().encodeCompressed()) | |
const alice = {} | |
alice.privateKey = '000762EED5BA4F378FFA60621C6DEF72F4A0A579112ADA5F5D6B2A35EC27E893A5' | |
alice.ecdh = createECDH(curve).setPrivateKey(Buffer.from(alice.privateKey, 'hex')) | |
alice.publicKey = getPublic(alice.ecdh) | |
alice.address = deriveAddress(alice.publicKey) | |
const bob = {} | |
bob.privateKey = '001ACAAEDECE405B2A958212629E16F2EB46B153EEE94CDD350FDEFF52795525B7' | |
bob.ecdh = createECDH(curve).setPrivateKey(Buffer.from(bob.privateKey, 'hex')) | |
bob.publicKey = getPublic(bob.ecdh) | |
bob.address = deriveAddress(bob.publicKey) | |
alice.sharedSecret = alice.ecdh.computeSecret(Buffer.from(bob.publicKey, 'hex'), null, 'hex') | |
bob.sharedSecret = bob.ecdh.computeSecret(Buffer.from(alice.publicKey, 'hex'), null, 'hex') | |
console.log({ alice, bob }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment