https://github.com/substrate-system/message
Create and verify signed messages
https://github.com/substrate-system/message
Last synced: 5 months ago
JSON representation
Create and verify signed messages
- Host: GitHub
- URL: https://github.com/substrate-system/message
- Owner: substrate-system
- License: other
- Created: 2023-04-21T20:40:59.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2025-09-08T03:38:43.000Z (10 months ago)
- Last Synced: 2025-09-21T10:52:17.661Z (9 months ago)
- Language: TypeScript
- Homepage:
- Size: 208 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# message
[](https://github.com/substrate-system/message/actions/workflows/nodejs.yml)
[](README.md)
[](README.md)
[](https://packagephobia.com/result?p=@substrate-system/message)
[](https://bundlephobia.com/package/@substrate-system/message)
[](LICENSE)
Create and verify signed messages with [the webcrypto API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API).
## Contents
- [Install](#install)
- [Example](#example)
* [Create a message](#create-a-message)
* [Verify a message](#verify-a-message)
## Install
```sh
npm i -S @substrate-system/message
```
## Example
### Create a message
```js
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
```js
{
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)`.
>
```js
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
```js
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')
})
```