diff --git a/packages/bcp/src/cosmwasmconnection.spec.ts b/packages/bcp/src/cosmwasmconnection.spec.ts index 6272c09a..2b867e6d 100644 --- a/packages/bcp/src/cosmwasmconnection.spec.ts +++ b/packages/bcp/src/cosmwasmconnection.spec.ts @@ -1,4 +1,8 @@ -import { CosmosAddressBech32Prefix, decodeSignature } from "@cosmwasm/sdk"; +import { + CosmosAddressBech32Prefix, + decodeSignature, + makeSecp256k1SignatureFromFixedLength, +} from "@cosmwasm/sdk"; import { Account, Address, @@ -15,7 +19,7 @@ import { TransactionId, TransactionState, } from "@iov/bcp"; -import { Random, Secp256k1, Secp256k1Signature, Sha256 } from "@iov/crypto"; +import { Random, Secp256k1, Sha256 } from "@iov/crypto"; import { Bech32, Encoding } from "@iov/encoding"; import { HdPaths, Secp256k1HdWallet, UserProfile } from "@iov/keycontrol"; import { assert } from "@iov/utils"; @@ -473,7 +477,7 @@ describe("CosmWasmConnection", () => { const { pubkey, signature } = decodeSignature(encodeFullSignature(signatures[0])); const prehashed = new Sha256(signBytes).digest(); const valid = await Secp256k1.verifySignature( - new Secp256k1Signature(signature.slice(0, 32), signature.slice(32, 64)), + makeSecp256k1SignatureFromFixedLength(signature), prehashed, pubkey, ); diff --git a/packages/sdk/package.json b/packages/sdk/package.json index a45a3bee..516a50f8 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -41,9 +41,11 @@ "@iov/crypto": "^2.0.2", "@iov/encoding": "^2.0.2", "@iov/utils": "^2.0.2", - "axios": "^0.19.0" + "axios": "^0.19.0", + "bn.js": "^5.1.1" }, "devDependencies": { + "@types/bn.js": "^4.11.6", "readonly-date": "^1.0.0" } } diff --git a/packages/sdk/src/index.ts b/packages/sdk/src/index.ts index 371c4cbe..bd5a46fd 100644 --- a/packages/sdk/src/index.ts +++ b/packages/sdk/src/index.ts @@ -23,5 +23,9 @@ export { encodeSecp256k1Pubkey, } from "./pubkey"; export { findSequenceForSignedTx } from "./sequence"; -export { encodeSecp256k1Signature, decodeSignature } from "./signature"; +export { + encodeSecp256k1Signature, + decodeSignature, + makeSecp256k1SignatureFromFixedLength, +} from "./signature"; export { SigningCallback, SigningCosmWasmClient, ExecuteResult } from "./signingcosmwasmclient"; diff --git a/packages/sdk/src/pen.spec.ts b/packages/sdk/src/pen.spec.ts index 57c71c78..68a514c2 100644 --- a/packages/sdk/src/pen.spec.ts +++ b/packages/sdk/src/pen.spec.ts @@ -1,8 +1,8 @@ -import { Secp256k1, Secp256k1Signature, Sha256 } from "@iov/crypto"; +import { Secp256k1, Sha256 } from "@iov/crypto"; import { Encoding } from "@iov/encoding"; import { Secp256k1Pen } from "./pen"; -import { decodeSignature } from "./signature"; +import { decodeSignature, makeSecp256k1SignatureFromFixedLength } from "./signature"; const { fromHex } = Encoding; @@ -37,7 +37,7 @@ describe("Sec256k1Pen", () => { const { pubkey, signature } = decodeSignature(await pen.sign(data)); const valid = await Secp256k1.verifySignature( - new Secp256k1Signature(signature.slice(0, 32), signature.slice(32, 64)), + makeSecp256k1SignatureFromFixedLength(signature), new Sha256(data).digest(), pubkey, ); diff --git a/packages/sdk/src/sequence.ts b/packages/sdk/src/sequence.ts index 8a53e897..9c2ef647 100644 --- a/packages/sdk/src/sequence.ts +++ b/packages/sdk/src/sequence.ts @@ -1,7 +1,7 @@ -import { Secp256k1, Secp256k1Signature, Sha256 } from "@iov/crypto"; +import { Secp256k1, Sha256 } from "@iov/crypto"; import { makeSignBytes } from "./encoding"; -import { decodeSignature } from "./signature"; +import { decodeSignature, makeSecp256k1SignatureFromFixedLength } from "./signature"; import { CosmosSdkTx } from "./types"; /** @@ -26,7 +26,7 @@ export async function findSequenceForSignedTx( if (!firstSignature) throw new Error("Signature missing in tx"); const { pubkey, signature } = decodeSignature(firstSignature); - const secp256keSignature = new Secp256k1Signature(signature.slice(0, 32), signature.slice(32, 64)); + const secp256keSignature = makeSecp256k1SignatureFromFixedLength(signature); for (let s = min; s < upperBound; s++) { // console.log(`Trying sequence ${s}`); diff --git a/packages/sdk/src/signature.ts b/packages/sdk/src/signature.ts index d31eb119..daa2750c 100644 --- a/packages/sdk/src/signature.ts +++ b/packages/sdk/src/signature.ts @@ -1,4 +1,6 @@ +import { Secp256k1Signature } from "@iov/crypto"; import { Encoding } from "@iov/encoding"; +import BN from "bn.js"; import { encodeSecp256k1Pubkey } from "./pubkey"; import { pubkeyType, StdSignature } from "./types"; @@ -37,3 +39,10 @@ export function decodeSignature( throw new Error("Unsupported pubkey type"); } } + +// TODO: use Secp256k1Signature.fromFixedLength once this is published https://github.com/iov-one/iov-core/pull/1401 +export function makeSecp256k1SignatureFromFixedLength(signature: Uint8Array): Secp256k1Signature { + const unpaddedR = Uint8Array.from(new BN(signature.slice(0, 32)).toArray()); + const unpaddedS = Uint8Array.from(new BN(signature.slice(32, 64)).toArray()); + return new Secp256k1Signature(unpaddedR, unpaddedS); +} diff --git a/packages/sdk/types/index.d.ts b/packages/sdk/types/index.d.ts index 4b2c4d8d..01ee1519 100644 --- a/packages/sdk/types/index.d.ts +++ b/packages/sdk/types/index.d.ts @@ -22,5 +22,9 @@ export { encodeSecp256k1Pubkey, } from "./pubkey"; export { findSequenceForSignedTx } from "./sequence"; -export { encodeSecp256k1Signature, decodeSignature } from "./signature"; +export { + encodeSecp256k1Signature, + decodeSignature, + makeSecp256k1SignatureFromFixedLength, +} from "./signature"; export { SigningCallback, SigningCosmWasmClient, ExecuteResult } from "./signingcosmwasmclient"; diff --git a/packages/sdk/types/signature.d.ts b/packages/sdk/types/signature.d.ts index c23e7167..16c7e432 100644 --- a/packages/sdk/types/signature.d.ts +++ b/packages/sdk/types/signature.d.ts @@ -1,3 +1,4 @@ +import { Secp256k1Signature } from "@iov/crypto"; import { StdSignature } from "./types"; /** * Takes a binary pubkey and signature to create a signature object @@ -12,3 +13,4 @@ export declare function decodeSignature( readonly pubkey: Uint8Array; readonly signature: Uint8Array; }; +export declare function makeSecp256k1SignatureFromFixedLength(signature: Uint8Array): Secp256k1Signature;