Skip to content

Commit

Permalink
[NayNay] Signing Fix: Message too long error (#412)
Browse files Browse the repository at this point in the history
  • Loading branch information
frankiebee authored Aug 26, 2024
2 parents e69d029 + 4ced232 commit dae4cb0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/signing/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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`)
}
Expand Down
28 changes: 27 additions & 1 deletion tests/sign.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -171,4 +197,4 @@ test('Sign:issue#380', async (t) => {


t.end()
})
})

0 comments on commit dae4cb0

Please sign in to comment.