0.8.27-patch.18 • Published 10 days ago

@canvas-js/signed-cid v0.8.27-patch.18

Weekly downloads
-
License
-
Repository
-
Last release
10 days ago

@canvas-js/signed-cid

This package implements a tiny signed data format for IPLD values. Any CID can be signed, and the resulting Signature can be passed around as an independent value on its own.

import type { CID } from "multiformats/cid"

export type SignatureType = "ed25519" | "secp256k1"

export type Signature = {
	type: SignatureType
	publicKey: Uint8Array // redundant for secp256k1 but still always included
	signature: Uint8Array
	cid: CID
}

The signature signs the bytes of the CID, which carries metadata about the encoding format and hashing algorithm, plus the hash of the encoded value itself. This allows Signature values to be redistributed via different codecs without breaking signature validation.

ed25519 and secp256k1 signatures are supported, using the audited @noble/curves library. secp256k1 public keys are always compressed.

Only the dag-json and dag-cbor IPLD codecs are included by default, but others can be used by implementing the Codec interface. Similarly, only sha2-256, blake3-256, and blake3-128 multihash digests are included by default, but other can be used by implementing the Digest interface.

Usage

import { createSignature, verifySignature } from "@canvas-js/signed-cid"
import { ed25519 } from "@noble/curves"

const privateKey = ed25519.utils.randomPrivateKey()

const value = { foo: "hello world", bar: [1, 2, 3] }
const signature = createSignature("ed25519", privateKey, value)
console.log(signature)
// {
//   type: 'ed25519',
//   publicKey: Uint8Array(32) [ ... ],
//   signature: Uint8Array(64) [ ... ],
//   cid: CID(bafyreibqke43yd2rqll4nwlrbfjfqferamxp3sdia36a6awqvcae3cmm7a)
// }

verifySignature(signature, value) // throws an error if the signature is invalid

API

Sign

declare function createSignature(
	type: "ed25519" | "secp256k1",
	privateKey: Uint8Array,
	value: any,
	options: { codec?: string | Codec; digest?: string | Digest } = {}
): Signature

Defaults to dag-cbor and sha2-256 if options.codec or options.digest are not provided, respectively.

Verify

declare function verifySignature(
	{ type, publicKey, signature, cid }: Signature,
	value: any,
	options: { codecs?: Codec[]; digests?: Digest[] } = {}
): void

Utility types

type Digest = { name: string; code: number; digest: (iter: Iterable<Uint8Array>) => Uint8Array }
type Codec = { name: string; code: number; encode: (value: any) => Iterable<Uint8Array> }

Codec and Digest are similar to some existing interfaces in the JavaScript IPLD ecosystem, but are defined the way they are here to support synchronous zero-copy streaming encoders.

0.8.27-patch.14

11 days ago

0.8.27-patch.13

11 days ago

0.8.27-patch.16

11 days ago

0.8.27-patch.15

11 days ago

0.8.27-patch.10

12 days ago

0.8.27-patch.6

12 days ago

0.8.27-patch.7

12 days ago

0.8.27-patch.12

11 days ago

0.8.27-patch.8

12 days ago

0.8.27-patch.11

12 days ago

0.8.27-patch.9

12 days ago

0.8.27-patch.20

10 days ago

0.8.27-patch.18

10 days ago

0.8.27-patch.17

10 days ago

0.8.27-patch.19

10 days ago

0.8.27-patch.2

15 days ago

0.8.27-patch.3

15 days ago

0.8.27-patch.4

15 days ago

0.8.27-patch.5

15 days ago

0.8.27-patch.1

15 days ago

0.8.26-alpha.4

4 months ago

0.8.26-alpha.3

4 months ago

0.8.26-alpha.2

4 months ago

0.8.26

4 months ago

0.8.26-alpha.1

4 months ago

0.8.25

4 months ago

0.8.24

4 months ago

0.8.23

4 months ago

0.8.22

4 months ago

0.8.21

4 months ago

0.8.20

4 months ago

0.8.19

4 months ago

0.8.18

4 months ago

0.8.17

5 months ago

0.8.16

5 months ago

0.8.15

5 months ago

0.8.14-alpha.1

5 months ago

0.8.14

5 months ago

0.8.13

5 months ago

0.8.12

5 months ago

0.8.11

5 months ago

0.8.10

5 months ago

0.8.9

5 months ago

0.8.8

5 months ago

0.8.5

5 months ago

0.8.4

5 months ago

0.8.7

5 months ago

0.8.6

5 months ago

0.8.2-alpha.1

5 months ago

0.8.2-patch.1

5 months ago

0.8.3

5 months ago

0.8.2

5 months ago

0.7.2-alpha.1

5 months ago

0.7.2-alpha.2

5 months ago

0.7.2-alpha.3

5 months ago

0.7.2-alpha.4

5 months ago

0.7.2-alpha.5

5 months ago

0.7.2-alpha.6

5 months ago

0.7.2-alpha.7

5 months ago

0.8.1

5 months ago

0.7.2

5 months ago

0.8.0

5 months ago

0.7.1

5 months ago

0.7.3

5 months ago

0.7.0

6 months ago

0.6.1

6 months ago

0.6.0

6 months ago

0.5.1

6 months ago

0.5.0

7 months ago

0.6.0-alpha7

7 months ago

0.6.0-alpha6

7 months ago

0.6.0-alpha5

7 months ago

0.6.0-alpha4

7 months ago

0.6.0-alpha3

7 months ago

0.6.0-alpha2

8 months ago

0.6.0-alpha1

8 months ago