Skip to content

Commit

Permalink
Add support for signing multiple datas
Browse files Browse the repository at this point in the history
  • Loading branch information
webmaster128 committed Jul 21, 2021
1 parent 8ec98a1 commit 06da6b8
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 9 deletions.
55 changes: 55 additions & 0 deletions packages/stargate/src/signingstargateclient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,49 @@ describe("SigningStargateClient", () => {
memo: "",
});
});

it("works for multiple datas", async () => {
const wallet = await Secp256k1HdWallet.fromMnemonic(faucet.mnemonic);
const client = await SigningStargateClient.offline(wallet);
const [firstAccount] = await wallet.getAccounts();

const data1 = toAscii("Hello");
const data2 = toAscii("World");
const signed = await client.experimentalAdr36Sign(firstAccount.address, [data1, data2]);
expect(signed).toEqual({
msg: [
{
type: "sign/MsgSignData",
value: {
signer: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
data: "SGVsbG8=", // echo -n "Hello" | base64
},
},
{
type: "sign/MsgSignData",
value: {
signer: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
data: "V29ybGQ=", // echo -n "World" | base64
},
},
],
fee: {
amount: [],
gas: "0",
},
signatures: [
{
pub_key: {
type: "tendermint/PubKeySecp256k1",
value: "A08EGB7ro1ORuFhjOnZcSgwYlpe0DSFjVNUIkNNQxwKQ",
},
signature:
"KvN9FM/WSfsJERv4PS91Ey7SUrnVJ/XHpHmMDh0sC94Niz2JLfF9KKE1QMfL5KtVFSRdMkJJsMtgl+aCaUyOCw==",
},
],
memo: "",
});
});
});

describe("experimentalAdr36Verify", () => {
Expand All @@ -680,5 +723,17 @@ describe("SigningStargateClient", () => {
const ok = await SigningStargateClient.experimentalAdr36Verify(signed);
expect(ok).toEqual(true);
});

it("works with multiple datas", async () => {
const wallet = await Secp256k1HdWallet.fromMnemonic(faucet.mnemonic);
const client = await SigningStargateClient.offline(wallet);
const [firstAccount] = await wallet.getAccounts();

const data1 = toAscii("Hello");
const data2 = toAscii("World");
const signed = await client.experimentalAdr36Sign(firstAccount.address, [data1, data2]);
const ok = await SigningStargateClient.experimentalAdr36Verify(signed);
expect(ok).toEqual(true);
});
});
});
22 changes: 13 additions & 9 deletions packages/stargate/src/signingstargateclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ export class SigningStargateClient extends StargateClient {
: this.signAmino(signerAddress, messages, fee, memo, signerData);
}

public async experimentalAdr36Sign(signerAddress: string, data: Uint8Array): Promise<StdTx> {
public async experimentalAdr36Sign(signerAddress: string, data: Uint8Array | Uint8Array[]): Promise<StdTx> {
const accountNumber = 0;
const sequence = 0;
const chainId = "";
Expand All @@ -367,13 +367,17 @@ export class SigningStargateClient extends StargateClient {
};
const memo = "";

const msg: MsgSignData = {
type: "sign/MsgSignData",
value: {
signer: signerAddress,
data: toBase64(data),
},
};
const datas = Array.isArray(data) ? data : [data];

const msgs: MsgSignData[] = datas.map(
(d): MsgSignData => ({
type: "sign/MsgSignData",
value: {
signer: signerAddress,
data: toBase64(d),
},
}),
);

assert(!isOfflineDirectSigner(this.signer));
const accountFromSigner = (await this.signer.getAccounts()).find(
Expand All @@ -382,7 +386,7 @@ export class SigningStargateClient extends StargateClient {
if (!accountFromSigner) {
throw new Error("Failed to retrieve account from signer");
}
const signDoc = makeSignDocAmino([msg], fee, chainId, memo, accountNumber, sequence);
const signDoc = makeSignDocAmino(msgs, fee, chainId, memo, accountNumber, sequence);
const { signature, signed } = await this.signer.signAmino(signerAddress, signDoc);
if (!equals(signDoc, signed)) {
throw new Error(
Expand Down

0 comments on commit 06da6b8

Please sign in to comment.