@ssc-half-light/util
v0.11.5
Published
Utility functions related to the webcrypto API
Downloads
107
Readme
util
Utility functions
Depends on uint8arrays
and @oddjs/odd
.
install
npm i @ssc-half-light/util
API
verify
Works in node or browsers.
async (did:string, sig:string, msg:string):Promise<boolean>
example — verify
import { verify } from '@ssc-half-light/util'
const DID = 'did:key:z13V3Sog2Ya...'
const sig = 'X8iGF4Lz4erw4UE...'
const isValid = await verify(DID, sig, 'my message')
sign
(keystore:KeyStore, msg:string):Promise<Uint8Array>
example — sign
This depends on a keystore
instance.
import { sign, toString } from '@ssc-half-light/util'
import * as odd from '@oddjs/odd'
const program = await odd.program({
namespace: { creator: 'test', name: 'testing' },
debug: true
})
const { keystore } = program.components.crypto
const sig = toString(await sign(keystore, 'my message'))
toString
Return a 'base64url'
string. Good for getting a string version of a signature.
function toString (arr:Uint8Array):string
example — toString
test('toString', t => {
const myString = toString(myUint8Array)
t.equal(typeof myString, 'string', 'should conver a Uint8Array to string')
})
writeKeyToDid
Convert a given public key to a DID string.
async function writeKeyToDid (crypto:Crypto.Implementation):Promise<DID>
example
import { writeKeyToDid } from '@ssc-half-light/util'
test('writeKeyToDid', async t => {
const crypto = program.components.crypto
const did = await writeKeyToDid(crypto)
console.log('**did**', did)
t.equal(typeof did, 'string', 'should create string')
t.ok(did.includes('did:key:'), 'should return the right format string')
})
didToPublicKey
Convert a DID string to a Uint8Array
and type
string.
function didToPublicKey (did:string):({
publicKey:Uint8Array,
type: 'rsa' | 'ed25519' | 'bls12-381'
})
example — didToPublicKey
Get the DID version of the key used to sign messages.
test('didToPublicKey', t => {
const did = 'did:key:z13...'
const pubKey = didToPublicKey(did)
t.equal(pubKey.type, 'rsa', 'should return the right key type')
t.ok(pubKey.publicKey instanceof Uint8Array,
'should return the public key as a Uint8Array')
})