Skip to content

Commit c53ce90

Browse files
committed
added personal_sign in simulator
1 parent 9e88621 commit c53ce90

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

libs/remix-simulator/src/methods/accounts.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export class Web3Accounts {
8282
eth_accounts: this.eth_accounts.bind(this),
8383
eth_getBalance: this.eth_getBalance.bind(this),
8484
eth_sign: this.eth_sign.bind(this),
85+
personal_sign: this.personal_sign.bind(this),
8586
eth_chainId: this.eth_chainId.bind(this),
8687
eth_signTypedData: this.eth_signTypedData_v4.bind(this), // default call is using V4
8788
eth_signTypedData_v4: this.eth_signTypedData_v4.bind(this),
@@ -127,6 +128,20 @@ export class Web3Accounts {
127128
cb(null, signature)
128129
}
129130

131+
personal_sign (payload, cb) {
132+
let message = payload.params[0]
133+
const address = payload.params[1]
134+
135+
const privateKey = this.accountsKeys[toChecksumAddress(address)]
136+
if (!privateKey) {
137+
return cb(new Error('unknown account'))
138+
}
139+
const wallet: Wallet = new ethers.Wallet(privateKey)
140+
const signature = wallet.signMessageSync(toBeArray(message))
141+
142+
cb(null, signature)
143+
}
144+
130145
eth_chainId (_payload, cb) {
131146
if (!this.options.chainId) return cb(null, '0x539') // 0x539 is hex of 1337
132147
const id = (typeof this.options.chainId === 'number') ? intToHex(this.options.chainId) : this.options.chainId

libs/remix-simulator/test/accounts.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ describe('Accounts', () => {
3636
const signer = await ethersProvider.getSigner()
3737
const signature: any = await signer._legacySignMessage('Hello world') // _legacySignMessage uses 'eth_sign' internally
3838
assert.deepEqual(typeof signature === 'string' ? signature.length : signature.signature.length, 132)
39+
assert.deepEqual(signature, "0x4bb5c87f889dcef489ce5965930a33cd4a5a4e20b5c44f9abb948a10f8b5cc5176398e92d9faf9168af3fbf3cb4ab12b99f9c88d34ab91242cc9490f71ca3f751c")
40+
})
41+
})
42+
43+
describe('personal_sign', () => {
44+
it('should sign payloads', async () => {
45+
const signer = await ethersProvider.getSigner()
46+
const signature: any = await signer.signMessage('Hello world') // signMessage uses 'personal_sign' internally
47+
assert.deepEqual(typeof signature === 'string' ? signature.length : signature.signature.length, 132)
48+
assert.deepEqual(signature, "0x4bb5c87f889dcef489ce5965930a33cd4a5a4e20b5c44f9abb948a10f8b5cc5176398e92d9faf9168af3fbf3cb4ab12b99f9c88d34ab91242cc9490f71ca3f751c")
3949
})
4050
})
4151

0 commit comments

Comments
 (0)