diff --git a/src/signing/index.ts b/src/signing/index.ts index 975f7dba..8c7474bd 100644 --- a/src/signing/index.ts +++ b/src/signing/index.ts @@ -1,4 +1,5 @@ import { ApiPromise } from '@polkadot/api' +import { hexAddPrefix } from '@polkadot/util' import { Signer } from '../keys/types/internal' import { defaultAdapters } from './adapters/default' import { Adapter } from './adapters/types' @@ -364,8 +365,15 @@ export default class SignatureRequestManager { // define keygroups see issue#380 https://github.com/entropyxyz/sdk/issues/380 this.#keyGroups[i] = keyGroup // omg polkadot type gen is a head ache + // + // If the Message being signed is too long the sigRequestHash is then converted to a number + // so large that the resulting parsed value of parseInt(sigRequest, 16) would return Infinity. + // Using BigInt instead solves the Infinity issue, and now allows messages of any length to be + // signed. + // + const sigToConvert = hexAddPrefix(sigRequest) // @ts-ignore: next line - const index = parseInt(sigRequest, 16) % keyGroup.unwrap().length + const index = Number(BigInt(sigToConvert) % BigInt(keyGroup.unwrap().length)) if (isNaN(index)) { throw new Error(`when calculating the index for choosing a validator got: NaN`) } diff --git a/tests/sign.test.ts b/tests/sign.test.ts index fa4eab06..cb117744 100644 --- a/tests/sign.test.ts +++ b/tests/sign.test.ts @@ -60,6 +60,32 @@ test('Sign', async (t) => { t.end() }) +test('Sign: Long Message', async t => { + const { run, entropy } = await setupTest(t) + + const dummyLongMessage = `Deep Divide Have Grass Blessed Greater Replenish + Tree Days You're Seed Earth Above + Blessed beginning god give air above green. God have. Midst. Moved made divided seasons light in be place years above gathered. Days which day waters. + + Seasons + Given one moving darkness appear. You Lesser moving saw. They're divided rule air his seasons. Fifth lights be gathering upon gathering gathering darkness, over. Whales. + + Adding some symbols for good measure: #$@%@#$@#%#@`; + + const signature = await run( + 'sign', + entropy.signWithAdaptersInOrder({ + msg: { msg: dummyLongMessage }, + order: ['deviceKeyProxy'], + }) + ) + + t.true(signature && signature.length > 32, 'signature has some body!') + signature && console.log(signature) + + t.end() +}) + test('Sign: custom signatureVerifyingKey', async (t) => { const run = promiseRunner(t) @@ -171,4 +197,4 @@ test('Sign:issue#380', async (t) => { t.end() -}) +}) \ No newline at end of file