cosmos / cosmjs

The Swiss Army knife to power JavaScript based client solutions ranging from Web apps/explorers over browser extensions to server-side clients like faucets/scrapers.
https://cosmos.github.io/cosmjs/
Apache License 2.0
646 stars 331 forks source link

Error while issuing of verifiable credentials. #1419

Open dubeyaditya6232 opened 1 year ago

dubeyaditya6232 commented 1 year ago

Error : "failed to execute message; message index: 0: credential proof could not be verified: verification error : message signer does not match provided did",

const signer: OfflineDirectSigner = await getSignerFromPriKey() const regulator = (await signer.getAccounts())[0].address const pubKey = (await signer.getAccounts())[0].pubkey const algo = (await signer.getAccounts())[0].algo const issuerDID: string = did:cosmos:key:${regulator}

const subjectDID: string = did:cosmos:key:${regulator} const credentialID: string = "regulator-credential/" + subjectDID const vmID: string = ${issuerDID}#${regulator}; const signingClient = await RegulatorSigningStargateClient.connectWithSigner(rpc, signer) let vc: VerifiableCredential = VerifiableCredential.fromPartial({ context: ["https://www.w3.org/TR/vc-data-model/"], id: credentialID, issuer: issuerDID, issuanceDate: new Date, regulatorCred: RegulatorCredentialSubject.create({ id: subjectDID, name: name, country: countryCode }), type: ["verifiableCredential", "RegulatorCredential"], })

const issueVC = async ( vc: VerifiableCredential, pubKey: Uint8Array, sequence: number, chainId: string, signer: OfflineDirectSigner, accountNumber: number, regulator: string, algo: string, vmID: string, signingClient: RegulatorSigningStargateClient, fee: StdFee ): Promise => {

const pubkey = encodePubkey(encodeSecp256k1Pubkey(pubKey))

const vcBytes = VerifiableCredential.encode(vc).finish();

const authInfoBytes = makeAuthInfoBytes(
    [{ pubkey, sequence }],
    fee.amount,
    Number(fee.gas),
    fee.granter,
    fee.payer,
);

vc.proof = undefined
const signDoc = makeSignDoc(sha256(vcBytes), authInfoBytes, chainId, accountNumber)
const { signature: stdSignature } = await signer.signDirect(regulator, signDoc)

const proof: Proof = Proof.fromPartial({
    type: algo,
    created: (new Date).toUTCString(),
    verificationMethod: vmID,
    proofPurpose: "testing",
    signature: Buffer.from(stdSignature.signature).toString('base64')
})

vc.proof = proof
vc = VerifiableCredential.fromPartial(vc)

const txMsg: MsgIssueRegulatorCredentialEncodeObject = {
    typeUrl: msgIssueRegulatorCredentialTypeUrl,
    value: MsgIssueRegulatorCredential.fromPartial({
        credential: vc,
        owner: regulator,
    })
}

const result = await signingClient.signAndBroadcast(
    regulator,
    [txMsg],
    fee,
    "creating vc"
)
return result

}