Comparing version 7.3.0 to 7.4.0
@@ -19,4 +19,5 @@ import SimpleSigner from './signers/SimpleSigner.js'; | ||
export { type JWTOptions, type JWTVerifyOptions } from './JWT.js'; | ||
export { base64ToBytes, base58ToBytes, hexToBytes, genX25519EphemeralKeyPair } from './util.js'; | ||
export { base64ToBytes, bytesToBase64url, base58ToBytes, bytesToBase58, hexToBytes, bytesToHex, genX25519EphemeralKeyPair, multibaseToBytes, bytesToMultibase, supportedCodecs, } from './util.js'; | ||
export { extractPublicKeyBytes } from './VerifierAlgorithm.js'; | ||
export * from './Errors.js'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -30,3 +30,34 @@ import { bases } from 'multiformats/basics'; | ||
export declare function bytesToBase58(b: Uint8Array): string; | ||
export declare function bytesToMultibase(b: Uint8Array, base: keyof typeof bases): string; | ||
export declare const supportedCodecs: { | ||
'ed25519-pub': number; | ||
'x25519-pub': number; | ||
'secp256k1-pub': number; | ||
'bls12_381-g1-pub': number; | ||
'bls12_381-g2-pub': number; | ||
'p256-pub': number; | ||
}; | ||
/** | ||
* Encodes the given byte array to a multibase string (defaulting to base58btc). | ||
* If a codec is provided, the corresponding multicodec prefix will be added. | ||
* | ||
* @param b - the Uint8Array to be encoded | ||
* @param base - the base to use for encoding (defaults to base58btc) | ||
* @param codec - the codec to use for encoding (defaults to no codec) | ||
* | ||
* @returns the multibase encoded string | ||
* | ||
* @public | ||
*/ | ||
export declare function bytesToMultibase(b: Uint8Array, base?: keyof typeof bases, codec?: keyof typeof supportedCodecs | number): string; | ||
/** | ||
* Converts a multibase string to the Uint8Array it represents. | ||
* This method will assume the byte array that is multibase encoded is a multicodec and will attempt to decode it. | ||
* | ||
* @param s - the string to be converted | ||
* | ||
* @throws if the string is not formatted correctly. | ||
* | ||
* @public | ||
*/ | ||
export declare function multibaseToBytes(s: string): Uint8Array; | ||
export declare function hexToBytes(s: string, minLength?: number): Uint8Array; | ||
@@ -33,0 +64,0 @@ export declare function encodeBase64url(s: string): string; |
{ | ||
"name": "did-jwt", | ||
"version": "7.3.0", | ||
"version": "7.4.0", | ||
"description": "Library for Signing and Verifying JWTs that use DIDs as issuers and JWEs that use DIDs as recipients", | ||
@@ -5,0 +5,0 @@ "type": "module", |
@@ -66,2 +66,53 @@ import VerifierAlgorithm from '../VerifierAlgorithm.js' | ||
}) | ||
it('handles EdDSA algorithm with did:peer', async () => { | ||
expect.assertions(1) | ||
const resolver = { | ||
resolve: async () => ({ | ||
didDocumentMetadata: {}, | ||
didResolutionMetadata: { | ||
contentType: 'application/did+ld+json', | ||
}, | ||
didDocument: { | ||
'@context': ['https://www.w3.org/ns/did/v1', 'https://w3id.org/security/suites/ed25519-2020/v1'], | ||
id: 'did:peer:0z6MknNW5mvrUpssSJwZRQSinLWXzcECPtjzeKUsTR1Mvumfw', | ||
verificationMethod: [ | ||
{ | ||
id: 'did:peer:0z6MknNW5mvrUpssSJwZRQSinLWXzcECPtjzeKUsTR1Mvumfw#6MknNW5mvrUpssSJwZRQSinLWXzcECPtjzeKUsTR1Mvumfw', | ||
type: 'Ed25519VerificationKey2020', | ||
controller: 'did:peer:0z6MknNW5mvrUpssSJwZRQSinLWXzcECPtjzeKUsTR1Mvumfw', | ||
publicKeyMultibase: 'z6MknNW5mvrUpssSJwZRQSinLWXzcECPtjzeKUsTR1Mvumfw', | ||
}, | ||
], | ||
authentication: [ | ||
'did:peer:0z6MknNW5mvrUpssSJwZRQSinLWXzcECPtjzeKUsTR1Mvumfw#6MknNW5mvrUpssSJwZRQSinLWXzcECPtjzeKUsTR1Mvumfw', | ||
], | ||
assertionMethod: [ | ||
'did:peer:0z6MknNW5mvrUpssSJwZRQSinLWXzcECPtjzeKUsTR1Mvumfw#6MknNW5mvrUpssSJwZRQSinLWXzcECPtjzeKUsTR1Mvumfw', | ||
], | ||
capabilityInvocation: [ | ||
'did:peer:0z6MknNW5mvrUpssSJwZRQSinLWXzcECPtjzeKUsTR1Mvumfw#6MknNW5mvrUpssSJwZRQSinLWXzcECPtjzeKUsTR1Mvumfw', | ||
], | ||
capabilityDelegation: [ | ||
'did:peer:0z6MknNW5mvrUpssSJwZRQSinLWXzcECPtjzeKUsTR1Mvumfw#6MknNW5mvrUpssSJwZRQSinLWXzcECPtjzeKUsTR1Mvumfw', | ||
], | ||
}, | ||
}), | ||
} | ||
const jwt = | ||
'eyJhbGciOiJFZERTQSIsInR5cCI6IkpXVCJ9.eyJ2YyI6eyJAY29udGV4dCI6WyJodHRwczovL3d3dy53My5vcmcvMjAxOC9jcmVkZW50aWFscy92MSJdLCJ0eXBlIjpbIlZlcmlmaWFibGVDcmVkZW50aWFsIl0sImNyZWRlbnRpYWxTdWJqZWN0Ijp7Im5vdGhpbmciOiJlbHNlIG1hdHRlcnMifX0sIm5iZiI6MTY5NTA1MjE4MSwiaXNzIjoiZGlkOnBlZXI6MHo2TWtuTlc1bXZyVXBzc1NKd1pSUVNpbkxXWHpjRUNQdGp6ZUtVc1RSMU12dW1mdyJ9.mvgdqscXYjIXRuut83e8AfcBVdQJJOppQ9flohALoke_qRL9rR0FBOuBjWbf6uHftKv8lqUcqZuPnmsAJ0sbAA' | ||
const { payload } = await verifyJWT(jwt, { resolver }) | ||
return expect(payload).toMatchObject({ | ||
iss: 'did:peer:0z6MknNW5mvrUpssSJwZRQSinLWXzcECPtjzeKUsTR1Mvumfw', | ||
nbf: 1695052181, | ||
vc: { | ||
'@context': ['https://www.w3.org/2018/credentials/v1'], | ||
credentialSubject: { | ||
nothing: 'else matters', | ||
}, | ||
type: ['VerifiableCredential'], | ||
}, | ||
}) | ||
}) | ||
}) |
@@ -218,2 +218,3 @@ // @ts-ignore | ||
const publicKeyMultibase = bytesToMultibase(publicKeyBytes, 'base58btc') | ||
const publicKeyMultibaseMulticodec = bytesToMultibase(publicKeyBytes, 'base58btc', 'secp256k1-pub') | ||
const eip155 = toEthereumAddress(publicKeyHex) | ||
@@ -388,2 +389,13 @@ const bip122 = toBip122Address(publicKeyHex, 'undefined') | ||
it('validates with publicKeyMultibase multicodec', async () => { | ||
expect.assertions(1) | ||
const jwt = await createJWT({ bla: 'bla' }, { issuer: did, signer }) | ||
const parts = jwt.match(/^([a-zA-Z0-9_-]+\.[a-zA-Z0-9_-]+)\.([a-zA-Z0-9_-]+)$/) | ||
const pubkey = Object.assign({ publicKeyMultibase: publicKeyMultibaseMulticodec }, ecKey2) | ||
// @ts-ignore | ||
delete pubkey.publicKeyHex | ||
// @ts-ignore | ||
return expect(verifier(parts[1], parts[2], [pubkey])).toEqual(pubkey) | ||
}) | ||
it('validates signature with compressed public key and picks correct public key', async () => { | ||
@@ -390,0 +402,0 @@ expect.assertions(1) |
@@ -64,4 +64,17 @@ import SimpleSigner from './signers/SimpleSigner.js' | ||
export { base64ToBytes, base58ToBytes, hexToBytes, genX25519EphemeralKeyPair } from './util.js' | ||
export { | ||
base64ToBytes, | ||
bytesToBase64url, | ||
base58ToBytes, | ||
bytesToBase58, | ||
hexToBytes, | ||
bytesToHex, | ||
genX25519EphemeralKeyPair, | ||
multibaseToBytes, | ||
bytesToMultibase, | ||
supportedCodecs, | ||
} from './util.js' | ||
export { extractPublicKeyBytes } from './VerifierAlgorithm.js' | ||
export * from './Errors.js' |
@@ -5,2 +5,3 @@ import { concat, fromString, toString } from 'uint8arrays' | ||
import type { EphemeralKeyPair } from './encryption/types.js' | ||
import { varint } from 'multiformats' | ||
@@ -56,6 +57,79 @@ const u8a = { toString, fromString, concat } | ||
export function bytesToMultibase(b: Uint8Array, base: keyof typeof bases): string { | ||
return bases[base].encode(b) | ||
// this is from the multicodec table https://github.com/multiformats/multicodec/blob/master/table.csv | ||
export const supportedCodecs = { | ||
'ed25519-pub': 0xed, | ||
'x25519-pub': 0xec, | ||
'secp256k1-pub': 0xe7, | ||
'bls12_381-g1-pub': 0xea, | ||
'bls12_381-g2-pub': 0xeb, | ||
'p256-pub': 0x1200, | ||
} | ||
/** | ||
* Encodes the given byte array to a multibase string (defaulting to base58btc). | ||
* If a codec is provided, the corresponding multicodec prefix will be added. | ||
* | ||
* @param b - the Uint8Array to be encoded | ||
* @param base - the base to use for encoding (defaults to base58btc) | ||
* @param codec - the codec to use for encoding (defaults to no codec) | ||
* | ||
* @returns the multibase encoded string | ||
* | ||
* @public | ||
*/ | ||
export function bytesToMultibase( | ||
b: Uint8Array, | ||
base: keyof typeof bases = 'base58btc', | ||
codec?: keyof typeof supportedCodecs | number | ||
): string { | ||
if (!codec) { | ||
return bases[base].encode(b) | ||
} else { | ||
const codecCode = typeof codec === 'string' ? supportedCodecs[codec] : codec | ||
const prefixLength = varint.encodingLength(codecCode) | ||
const multicodecEncoding = new Uint8Array(prefixLength + b.length) | ||
varint.encodeTo(codecCode, multicodecEncoding) // set prefix | ||
multicodecEncoding.set(b, prefixLength) // add the original bytes | ||
return bases[base].encode(multicodecEncoding) | ||
} | ||
} | ||
/** | ||
* Converts a multibase string to the Uint8Array it represents. | ||
* This method will assume the byte array that is multibase encoded is a multicodec and will attempt to decode it. | ||
* | ||
* @param s - the string to be converted | ||
* | ||
* @throws if the string is not formatted correctly. | ||
* | ||
* @public | ||
*/ | ||
export function multibaseToBytes(s: string): Uint8Array { | ||
const { base10, base16, base16upper, base58btc, base64, base64url } = bases | ||
const baseDecoder = base58btc.decoder | ||
.or(base10.decoder) | ||
.or(base16.decoder) | ||
.or(base16upper.decoder) | ||
.or(base64.decoder) | ||
.or(base64url.decoder) | ||
const bytes = baseDecoder.decode(s) | ||
// look for known key lengths first | ||
// Ed25519/X25519, secp256k1/P256 compressed or not, BLS12-381 G1/G2 compressed | ||
if ([32, 33, 48, 64, 65, 96].includes(bytes.length)) { | ||
return bytes | ||
} | ||
// then assume multicodec, otherwise return the bytes | ||
try { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const [codec, length] = varint.decode(bytes) | ||
return bytes.slice(length) | ||
} catch (e) { | ||
// not a multicodec, return the bytes | ||
return bytes | ||
} | ||
} | ||
export function hexToBytes(s: string, minLength?: number): Uint8Array { | ||
@@ -62,0 +136,0 @@ let input = s.startsWith('0x') ? s.substring(2) : s |
import { sha256, toEthereumAddress } from './Digest.js' | ||
import type { VerificationMethod } from 'did-resolver' | ||
import { bases } from 'multiformats/basics' | ||
import { | ||
hexToBytes, | ||
base58ToBytes, | ||
base64ToBytes, | ||
bytesToBigInt, | ||
bytesToHex, | ||
EcdsaSignature, | ||
ECDSASignature, | ||
hexToBytes, | ||
multibaseToBytes, | ||
stringToBytes, | ||
bytesToBigInt, | ||
ECDSASignature, | ||
} from './util.js' | ||
@@ -70,5 +70,3 @@ import { verifyBlockchainAccountId } from './blockchains/index.js' | ||
} else if (pk.publicKeyMultibase) { | ||
const { base16, base58btc, base64, base64url } = bases | ||
const baseDecoder = base16.decoder.or(base58btc.decoder.or(base64.decoder.or(base64url.decoder))) | ||
return baseDecoder.decode(pk.publicKeyMultibase) | ||
return multibaseToBytes(pk.publicKeyMultibase) | ||
} | ||
@@ -185,5 +183,7 @@ return new Uint8Array() | ||
type Verifier = (data: string, signature: string, authenticators: VerificationMethod[]) => VerificationMethod | ||
interface Algorithms { | ||
[name: string]: Verifier | ||
} | ||
const algorithms: Algorithms = { | ||
@@ -190,0 +190,0 @@ ES256: verifyES256, |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1230795
16099