Skip to content

Commit

Permalink
Add sign task
Browse files Browse the repository at this point in the history
  • Loading branch information
eternauta1337 committed Feb 28, 2024
1 parent 599ca95 commit ef3c37f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
19 changes: 19 additions & 0 deletions packages/ethernaut-signer/src/tasks/sign.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const output = require('common/src/output')
const storage = require('../internal/storage')
const { getWallet } = require('../internal/signers')

require('../scopes/sig')
.task('sign', 'Signs a message with a wallet')
.addOptionalPositionalParam('message', 'The message to sign')
.setAction(async ({ message }) => {
try {
const signers = storage.readSigners()
const signer = signers[signers.activeSigner]

const wallet = getWallet(signer.pk)

output.resultBox(wallet.signMessageSync(message))
} catch (err) {
return output.errorBox(err)
}
})
41 changes: 41 additions & 0 deletions packages/ethernaut-signer/test/tasks/sign.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const { Terminal } = require('common/src/terminal')
const storage = require('../../src/internal/storage')
const { getWallet } = require('../../src/internal/signers')

describe('info', function () {
const terminal = new Terminal()

const demoSig = {
address: '0x23618e81E3f5cdF7f54C3d65f7FBc0aBf5B21E8f',
pk: '0xdbda1821b80551c9d65939329250298aa3472ba22feea921c0cf5d620ea67b97',
}

describe('when queryig info about a signer', function () {
before('add test signers', async function () {
const signers = storage.readSigners()
if (!('test__3' in signers)) signers.test__3 = demoSig
signers.activeSigner = 'test__3'
storage.storeSigners(signers)
})

after('remove test signers', async function () {
const signers = storage.readSigners()
if ('test__3' in signers) delete signers.test__3
storage.storeSigners(signers)
})

describe('when signing a message', function () {
before('sign', async function () {
await terminal.run('npx hardhat sig sign "hello"')
})

it('prints the expected signature', async function () {
terminal.has('Result')

const wallet = getWallet(demoSig.pk)
const sig = wallet.signMessageSync('hello')
terminal.has(sig.slice(0, 15))
})
})
})
})

0 comments on commit ef3c37f

Please sign in to comment.