Skip to content

Commit

Permalink
wrapper: enforce message to sign is string (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
Schwartz10 authored and sohkai committed Apr 16, 2019
1 parent 2c195e4 commit 94ec00a
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/APP.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ Perform a read-only call on the app's smart contract.

Returns **Observable** An [RxJS observable](http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html) that emits the result of the call.

### requestMessageSign
### requestSignMessage

Perform a signature using the [personal_sign](https://web3js.readthedocs.io/en/1.0/web3-eth-personal.html#sign) method.

Expand Down
3 changes: 3 additions & 0 deletions packages/aragon-wrapper/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,9 @@ export default class Aragon {
* @return {Promise<string>} signature hash
*/
signMessage (message, requestingApp) {
if (typeof message !== 'string') {
return Promise.reject(new Error('Message to sign must be a string'))
}
return new Promise((resolve, reject) => {
this.signatures.next({
message,
Expand Down
98 changes: 98 additions & 0 deletions packages/aragon-wrapper/src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,104 @@ test('should send notifications correctly', async (t) => {
})
})

test('should emit an intent when requesting message signing', async (t) => {
const { Aragon } = t.context
const messageToSign = 'test message'
const requestingApp = '0x123'

t.plan(2)
// arrange
const instance = new Aragon()
instance.signatures = new Subject()

// act
instance.signatures.subscribe(intent => {
t.is(intent.message, messageToSign)
t.is(intent.requestingApp, requestingApp)
})

instance.signMessage(messageToSign, requestingApp)
})

test('should be able to resolve intent when requesting message signing', async (t) => {
const { Aragon } = t.context
const messageToSign = 'test message'
const requestingApp = '0x123'

t.plan(2)
// arrange
const instance = new Aragon()
instance.signatures = new Subject()

// act
let counter = 0
instance.signatures.subscribe(intent => {
intent.resolve(counter++)
})

return Promise.all([
instance.signMessage(messageToSign, requestingApp).then(val => t.is(val, 0)),
instance.signMessage(messageToSign, requestingApp).then(val => t.is(val, 1))
])
})

test('should be able to reject intent when requesting message signing', async (t) => {
const { Aragon } = t.context
const messageToSign = 'test message'
const requestingApp = '0x123'

t.plan(2)
// arrange
const instance = new Aragon()
instance.signatures = new Subject()

// act
let counter = 0
instance.signatures.subscribe(intent => {
if (counter === 0) {
intent.reject()
} else {
intent.reject(new Error('custom error'))
}
counter++
})

return Promise.all([
t.throwsAsync(
instance.signMessage(messageToSign, requestingApp),
{
instanceOf: Error,
message: 'The message was not signed'
}
),
t.throwsAsync(
instance.signMessage(messageToSign, requestingApp),
{
instanceOf: Error,
message: 'custom error'
}
)
])
})

test('should reject non-string message when requesting message signature', async (t) => {
const { Aragon } = t.context
const messageToSign = { key: 'this is not a string' }
const requestingApp = '0x123'

t.plan(1)
// arrange
const instance = new Aragon()

// act
return t.throwsAsync(instance.signMessage(messageToSign, requestingApp),
{
instanceOf: Error,
message: 'Message to sign must be a string'
}
)
})

test('should emit an intent when performing transaction path', async (t) => {
const { Aragon } = t.context
const initialAddress = '0x123'
Expand Down

0 comments on commit 94ec00a

Please sign in to comment.