message

Create and verify signed messages with the webcrypto API.
Contents
install
npm i -S @bicycle-codes/message
example
create a message
import { Keys } from '@bicycle-codes/keys'
import { create } from '@bicycle-codes/message'
const alicesKeys = await Keys.create()
const req = await create(alicesKeys, { hello: 'world' })
The returned object has a format like
{
author: 'did:key:...',
signature: '123abc',
...message
}
[!NOTE]
The message will have the fields author
and signature
appended to it.
author
is the DID that was used to sign this message. It is read
by verify(message)
.
import { test } from '@substrate-system/tapzero'
import { Keys } from '@bicycle-codes/keys'
import { create } from '@bicycle-codes/message'
let req:SignedMessage<{ hello: 'world' }>
const alicesKeys = await Keys.create()
test('create a message', async t => {
req = await create(alicesKeys, { hello: 'world' })
t.ok(req, 'request was created')
t.equal(typeof req.signature, 'string', 'should have a signature')
t.ok(req.author.includes('did:key:'), 'should have an author field')
t.equal(req.hello, 'world', 'should have the properties we passed in')
})
verify a message
import { test } from '@nichoth/tapzero'
import { verify } from '@bicycle-codes/message'
test('verify a message', async t => {
const isOk = await verify(req)
t.equal(isOk, true, 'should return true for a valid message')
})