Skip to content

Instantly share code, notes, and snippets.

@msinkec
Created February 13, 2024 10:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save msinkec/025c7ff90f8945dfb5ce4f93af1af1a3 to your computer and use it in GitHub Desktop.
Save msinkec/025c7ff90f8945dfb5ce4f93af1af1a3 to your computer and use it in GitHub Desktop.
Create Sig and PubKey with unknown PrivKey
import { bsv } from 'scrypt-ts'
async function main() {
const m = bsv.PrivateKey.fromRandom().bn
const hBuff = bsv.crypto.Hash.sha256(m.toBuffer())
const h = bsv.crypto.BN.fromBuffer(hBuff)
const G = bsv.crypto.Point.getG()
const n = bsv.crypto.Point.getN()
const s = bsv.PrivateKey.fromRandom().bn
let r: bsv.crypto.BN = new bsv.crypto.BN(0)
let R: bsv.crypto.Point = G
let isValidR = false
while (!isValidR) {
r = bsv.PrivateKey.fromRandom().bn
try {
R = bsv.crypto.Point.fromX(false, r)
isValidR = true
} catch (e) {
isValidR = false
}
}
const sR = R.mul(s)
const hG = G.mul(h)
const sR_hG = sR.add(hG.neg())
const inv_r = r.invm(n)
const Y = sR_hG.mul(inv_r)
const pubKey = new bsv.PublicKey(Y)
const sig = new bsv.crypto.Signature(r, s)
const valid = bsv.crypto.ECDSA.verify(
hBuff,
sig,
pubKey
)
console.log(pubKey.toString())
console.log(sig.toString())
console.log(valid)
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment