Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sign and verify messages between two agent is not working #1240

Open
YEASIN49 opened this issue Aug 21, 2024 · 0 comments
Open

Sign and verify messages between two agent is not working #1240

YEASIN49 opened this issue Aug 21, 2024 · 0 comments

Comments

@YEASIN49
Copy link

YEASIN49 commented Aug 21, 2024

Scenario:

In bifold, when two different wallets send messages to each other they use pack and unpack functions for encrypting and decrypting the messages. However, In addition, I am trying to sign message and verify their signature also. For this the approach, e.g. Alice will sign message using his private_key and Bob will verify it using Alice's public_key.

I created a function through which I am trying to sign a message using Alice's private key and then trying to verify it. I tried to sign it and it gives me buffer as response too. However, when I try to verify the signature it gives me error. For testing the sign and verify I created a functions which I am providing below:

Note: Here, for testing only, I am just using the publicKeyBase58 from await agent?.dids.resolveDidDocument(respGetById?.did) . But in future, of course I will use respGetById.theirDid for verifying signature.

const signAndVerify = async (textMessage, connectionId) => {
    const respGetById = await agent?.connections.getById( connectionId )
    const didDocData = await agent?.dids.resolveDidDocument(respGetById?.did)
    const publicKeyBase58 = didDocData?.verificationMethod[0].publicKeyBase58

    const bufferPubKey = TypedArrayEncoder.fromString(publicKeyBase58);

    // preparing params for signing 
    const key = { publicKeyBase58: publicKeyBase58, keyType: KeyType.Ed25519  }
    const bufferMessage = TypedArrayEncoder.fromString(textMessage);
    const signParams: WalletSignOptions = { data: bufferMessage, key }

    console.log("----------------- TEST: Signing... -----------------")
    const messageSignature = await agent?.context.wallet.sign( signParams )
    console.log(messageSignature)

    // Preparing params for veryfing
    key.publicKey = bufferPubKey
    const verifyParams = { data: bufferMessage, key: key, signature: messageSignature }

    console.log("================= TEST: veryfing ================")
    const isSignValid = await agent?.context.wallet.verify(verifyParams) 
    /* 
      This above verify func call gives error ==>  WARN  Possible Unhandled Promise Rejection (id: 11):
      [WalletError: Error verifying signature of data signed with verkey H5pt8hB33Jnn6Bp141sj2Xt4P8hgqFpes9MMB5QCoRcS]
    */


    return isSignValid
  }

FYI

  1. I am testing this code from Chat.tsx
  2. The sample code of verify and sign function can be found in "AskarBaseWallet.js" and "AskarWallet.js" within the @aries-framework of node_modules.
  3. One observation is, to verify signature, we need to pass one additional property "publicKey" and if we use any data format other than buffer, uint8array, or don't pass this property, then the app crashes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant