Skip to content

substrate-system/message

Repository files navigation

message

tests types module install size GZip size license

Create and verify signed messages with the webcrypto API.

Contents

Install

npm i -S @substrate-system/message

Example

Create a message

import { EccKeys as Keys } from '@substrate-system/keys'
import { create } from '@substrate-system/message'

const alicesKeys = await Keys.create()
const req = await create(alicesKeys.writeKey, { 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 '@substrate-system/keys'
import { create } from '@substrate-system/message'

let req:SignedMessage<{ hello: 'world' }>
const alicesKeys = await Keys.create()
test('create a message', async t => {
    req = await create(alicesKeys.writeKey, { 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 '@substrate-system/tapzero'
import { verify } from '@substrate-system/message'

test('verify a message', async t => {
    // `req` is the message we created above
    const isOk = await verify(req)
    t.equal(isOk, true, 'should return true for a valid message')
})

About

Create and verify signed messages

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •