openwallet-foundation/bifold-wallet

Sign and verify messages between two agent is not working

YEASIN49 opened this issue · 0 comments

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.