-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from aviarytech/latest-updates
Version 0.3
- Loading branch information
Showing
22 changed files
with
509 additions
and
2,675 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import * as ed from '@noble/ed25519'; | ||
import { edwardsToMontgomeryPub, edwardsToMontgomeryPriv } from '@noble/curves/ed25519'; | ||
|
||
import { bytesToHex, createDate } from "./utils"; | ||
import { base58btc } from "multiformats/bases/base58" | ||
import { canonicalize } from 'json-canonicalize'; | ||
import { createHash } from 'node:crypto'; | ||
|
||
export const createSigner = (vm: VerificationMethod) => { | ||
return async (doc: any, challenge: string) => { | ||
try { | ||
const proof: any = { | ||
type: 'DataIntegrityProof', | ||
cryptosuite: 'eddsa-jcs-2022', | ||
verificationMethod: `did:key:${vm.publicKeyMultibase}`, | ||
created: createDate(), | ||
proofPurpose: 'authentication', | ||
challenge | ||
} | ||
const dataHash = createHash('sha256').update(canonicalize(doc)).digest(); | ||
const proofHash = createHash('sha256').update(canonicalize(proof)).digest(); | ||
const input = Buffer.concat([dataHash, proofHash]); | ||
const secretKey = base58btc.decode(vm.secretKeyMultibase!); | ||
|
||
const output = await ed.signAsync(bytesToHex(input), bytesToHex(secretKey.slice(2, 34))); | ||
|
||
proof.proofValue = base58btc.encode(output); | ||
return {...doc, proof}; | ||
} catch (e: any) { | ||
console.error(e) | ||
throw new Error(`Document signing failure: ${e.details}`) | ||
} | ||
} | ||
} | ||
|
||
export const generateEd25519VerificationMethod = async (purpose: 'authentication' | 'assertionMethod' | 'capabilityInvocation' | 'capabilityDelegation'): Promise<VerificationMethod> => { | ||
const privKey = ed.utils.randomPrivateKey(); | ||
const pubKey = await ed.getPublicKeyAsync(privKey); | ||
const publicKeyMultibase = base58btc.encode(Buffer.concat([new Uint8Array([0xed, 0x01]), pubKey])); | ||
const secretKeyMultibase = base58btc.encode(Buffer.concat([new Uint8Array([0x80, 0x26]), privKey])); | ||
|
||
return { | ||
type: purpose, | ||
publicKeyMultibase, | ||
secretKeyMultibase | ||
}; | ||
} | ||
|
||
export const generateX25519VerificationMethod = async (purpose: 'keyAgreement'): Promise<VerificationMethod> => { | ||
const privKey = ed.utils.randomPrivateKey(); | ||
const pubKey = await ed.getPublicKeyAsync(privKey); | ||
const x25519PubKey = edwardsToMontgomeryPub(pubKey); | ||
const x25519PrivKey = edwardsToMontgomeryPriv(privKey); | ||
const publicKeyMultibase = base58btc.encode(Buffer.concat([new Uint8Array([0xec, 0x01]), x25519PubKey])); | ||
const secretKeyMultibase = base58btc.encode(Buffer.concat([new Uint8Array([0x82, 0x26]), x25519PrivKey])); | ||
|
||
return { | ||
type: purpose, | ||
publicKeyMultibase, | ||
secretKeyMultibase | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
declare module '@digitalbazaar/ed25519-multikey'; | ||
declare module 'fast-json-patch/index.mjs'; | ||
declare module 'base32'; | ||
declare module '@interop/base58-universal'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export { createSigner } from './signing'; | ||
export { createSigner, generateEd25519VerificationMethod } from './cryptography'; | ||
export { resolveDID, createDID, updateDID } from './method'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.