Coder Social home page Coder Social logo

lamport-ots's Issues

How to convert public key and signature to strings and back.

Hey! Thanks so much for making this awesome library. It does exactly what's needed without any fluff.
I'm in a situation where I want to give the public key and signature to someone as a string and then allow them to use those strings to verify the message at a later date. Here's the code I have so far:

function isVerified(part, inputText) {
        return lamportSHA256.verify(inputText, stringToUint8Array(part.signature), stringToPublicKey(part.publicKey));
}

function uint8ArrayToString(array) {
    return JSON.stringify(Array.from(array));
}

function stringToUint8Array(arrayString) {
    return new Uint8Array(JSON.parse(arrayString));
}

function publicKeyToString(publicKey) {
    console.log("publicKeyToString");
    const result = []
    publicKey.forEach((keyArray) => {
        result.push(uint8ArrayToString(keyArray));
    })
    return JSON.stringify(result)
}

function stringToPublicKey(pubKeyString) {
    const stringList = JSON.parse(pubKeyString);
    const result = [];
    stringList.forEach((keyString) => {
        result.push(stringToUint8Array(keyString));
    })
    return result;
}

function generateSignature(inputText) {
    const {publicKey, privateKey} = lamportSHA256.keys();
    const signature = lamportSHA256.sign(inputText, privateKey);
    return {publicKey: publicKeyToString(publicKey), signature: uint8ArrayToString(signature)};
}

However, I keep getting this error:

TypeError: readChunk(...).equals is not a function
verify/<
node_modules/lamport-ots/index.js:63

  60 | const verify = (input, sig, key) => {
  61 |   const msg = hash(input)
  62 |   return bitsForBuffer(msg).reduce((valid, bit, idx) => {
> 63 |     return valid && readChunk(key[bit], idx).equals(
     | ^  64 |       hash(readChunk(sig, idx))
  65 |     )
  66 |   }, true)

verify
node_modules/lamport-ots/index.js:62

  59 | 
  60 | const verify = (input, sig, key) => {
  61 |   const msg = hash(input)
> 62 |   return bitsForBuffer(msg).reduce((valid, bit, idx) => {
     | ^  63 |     return valid && readChunk(key[bit], idx).equals(
  64 |       hash(readChunk(sig, idx))
  65 |     )

isVerified
src/utils/cryptography.js:8

   5 | // try {
   6 |     const pk = stringToPublicKey(part.validationData.publicKey)
   7 |     console.log(pk);
>  8 |     return lamportSHA256.verify(inputText, stringToUint8Array(part.validationData.signature), pk);
     | ^   9 | // } catch {
  10 | //     return false;
  11 | // }

I'm not sure what I'm missing. When I switch to not converting to a string and back, it all works seamlessly. Even converting just the signature to a string and back seems to work fine. It's just the public key I'm struggling with. Maybe it's not actually an array of uint8Arrays? Would love some guidance here.

Thanks so much!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.