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

implement signArbitrary for MockKeplr #1066

Open
0xLaurenzo opened this issue Feb 28, 2024 · 3 comments
Open

implement signArbitrary for MockKeplr #1066

0xLaurenzo opened this issue Feb 28, 2024 · 3 comments

Comments

@0xLaurenzo
Copy link

Is your feature request related to a problem? Please describe.
Currently, MockKeplr supports signDirect and signAmino, but throws a "Not implemented" error for signArbitrary and verifyArbitrary, making the testing of certain features that want to use adr-36 more difficult, especially in CI/CD environments

Describe the solution you'd like
Implement signArbitrary and verifyArbitrary for MockKeplr

Describe alternatives you've considered
Inlining signArbitrary and signing using MockKeplr through signAmino does not work due to type constraints. signArbitrary internally calls appears to call serializeSignDoc internally. This behaviour is also seen in the verification functions of verifyADR36AminoSignDoc

Additional context
I believe most code can be copy pasted from pre-existing adr36 code already within keplr, such as the verification funtions

@0xLaurenzo
Copy link
Author

I believe this would almost completely suffice: 222bd16, except for the bech32 prefix in the verification function

@HeesungB
Copy link
Collaborator

HeesungB commented Mar 7, 2024

@LaurensKubat Hello. I'll check about it! Thank you.

@HeesungB
Copy link
Collaborator

HeesungB commented May 9, 2024

Hello.

It would be nice if it could be changed to something like this, please check.

import {
  encodeSecp256k1Pubkey,
  makeADR36AminoSignDoc,
  verifyADR36Amino,
} from "@keplr-wallet/cosmos";

async signArbitrary(
    chainId: string,
    signer: string,
    data: string | Uint8Array
  ): Promise<StdSignature> {
    const wallet = await this.getWallet(chainId);

    const key = await this.getKey(chainId);
    if (signer !== key.bech32Address) {
      throw new Error("Unmatched signer");
    }

    const signature = wallet.signDigest32(
      Hash.sha256(serializeSignDoc(makeADR36AminoSignDoc(signer, data)))
    );

    return {
      pub_key: encodeSecp256k1Pubkey(wallet.getPubKey().toBytes()),
      signature: encodeSecp256k1Signature(
        wallet.getPubKey().toBytes(),
        new Uint8Array([...signature.r, ...signature.s])
      ).signature,
    };
  }

  async verifyArbitrary(
    chainId: string,
    signer: string,
    data: string | Uint8Array,
    signature: StdSignature
  ): Promise<boolean> {
    const wallet = await this.getWallet(chainId);

    const key = await this.getKey(chainId);
    if (signer !== key.bech32Address) {
      throw new Error("Unmatched signer");
    }

    return verifyADR36Amino(
      this.chainInfos.find((c) => c.chainId === chainId)!.bech32Config
        .bech32PrefixAccAddr,
      signer,
      data,
      wallet.getPubKey().toBytes(),
      Buffer.from(signature.signature, "base64")
    );
  }

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

2 participants