Skip to content

Commit

Permalink
[NayNay] Signing bug for validators
Browse files Browse the repository at this point in the history
- updated signing with adapters to retry the signing request if a specific error is hit as defined in this issue entropyxyz/sdk#367
  • Loading branch information
rh0delta committed Jun 3, 2024
1 parent 96150d6 commit cc81daa
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 43 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"dependencies": {
"@entropyxyz/sdk": "^0.1.5-5",
"@ethereumjs/util": "^9.0.2",
"@polkadot/util": "^12.6.2",
"@types/node": "^20.12.12",
"alchemy-sdk": "^3.1.2",
"ansi-colors": "^4.1.3",
Expand Down
122 changes: 79 additions & 43 deletions src/flows/sign/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,85 @@
import inquirer from "inquirer"
import { u8aToHex } from '@polkadot/util'
import { initializeEntropy } from "../../common/initializeEntropy"
import { debug, getSelectedAccount, print } from "../../common/utils"

let msg: string
let signingAttempts: number = 0

async function signWithAdapter (entropy, signingData?: { msg: { msg: string }, order: string[] }) {
try {
const messageQuestion = {
type: 'list',
name: 'messageAction',
message: 'Please choose how you would like to input your message to sign:',
choices: [
'Text Input',
// 'From a File',
],
}
const userInputQuestion = {
type: "editor",
name: "userInput",
message: "Enter the message you wish to sign (this will open your default editor):",
}
// const pathToFileQuestion = {
// type: 'input',
// name: 'pathToFile',
// message: 'Enter the path to the file you wish to sign:',
// }
let messageAction
if (!msg) {
({ messageAction } = await inquirer.prompt([messageQuestion]))
switch (messageAction) {
case 'Text Input': {
const { userInput } = await inquirer.prompt([userInputQuestion])
msg = Buffer.from(userInput).toString('hex')
break
}
// case 'From a File': {
// break
// }
default: {
console.error('Unsupported Action')
return
}
}
}
debug('msg', msg);
const msgParam = { msg }
if (!signingData) {
signingData = {
msg: msgParam,
order: ['deviceKeyProxy', 'noop'],
}
}
const signature = await entropy.signWithAdaptersInOrder(signingData)
const signatureHexString = u8aToHex(signature)
print('signature:', signatureHexString)
print('verifying key:', entropy.signingManager.verifyingKey)
} catch (error) {
signingAttempts++
const { message } = error
// See https://github.com/entropyxyz/sdk/issues/367 for reasoning behind adding this retry mechanism
if (message.includes('Invalid Signer') || message.includes('Invalid Signer in Signing group')) {
if (signingAttempts <= 1) {
const msgParam = { msg }
signingData = {
msg: msgParam,
order: ['noop', 'deviceKeyProxy']
}
// Recursively retries signing with a reverse order in the subgroups list
await signWithAdapter(entropy, signingData)
} else {
console.error(message)
}
} else {
console.error(message)
}
return
}
}

export async function sign ({ accounts, endpoints, selectedAccount: selectedAccountAddress }, options) {
const endpoint = endpoints[options.ENDPOINT]
const actionChoice = await inquirer.prompt([
Expand Down Expand Up @@ -51,49 +129,7 @@ export async function sign ({ accounts, endpoints, selectedAccount: selectedAcco
// return
// }
case 'Sign With Adapter': {
const messageQuestion = {
type: 'list',
name: 'messageAction',
message: 'Please choose how you would like to input your message to sign:',
choices: [
'Text Input',
// 'From a File',
],
}
const userInputQuestion = {
type: "editor",
name: "userInput",
message: "Enter the message you wish to sign (this will open your default editor):",
}
// const pathToFileQuestion = {
// type: 'input',
// name: 'pathToFile',
// message: 'Enter the path to the file you wish to sign:',
// }
const { messageAction } = await inquirer.prompt([messageQuestion])
let msg: string
switch (messageAction) {
case 'Text Input': {
const { userInput } = await inquirer.prompt([userInputQuestion])
msg = Buffer.from(userInput).toString('hex')
break
}
// case 'From a File': {
// break
// }
default: {
console.error('Unsupported Action')
return
}
}
debug('msg', msg);
const msgParam = { msg }
const signature = await entropy.signWithAdaptersInOrder({
msg: msgParam,
order: ['deviceKeyProxy', 'noop'],
})

print('signature:', signature)
await signWithAdapter(entropy)
return
}
default:
Expand Down

0 comments on commit cc81daa

Please sign in to comment.