diff --git a/packages/credentials/package.json b/packages/credentials/package.json index 4d07f4066..0d44f5c66 100644 --- a/packages/credentials/package.json +++ b/packages/credentials/package.json @@ -41,10 +41,10 @@ "@kiltprotocol/chain-helpers": "workspace:*", "@kiltprotocol/config": "workspace:*", "@kiltprotocol/did": "workspace:*", - "@kiltprotocol/eddsa-jcs-2022": "0.1.0-rc.2", - "@kiltprotocol/es256k-jcs-2023": "0.1.0-rc.2", - "@kiltprotocol/jcs-data-integrity-proofs-common": "0.1.0-rc.2", - "@kiltprotocol/sr25519-jcs-2023": "0.1.0-rc.2", + "@kiltprotocol/eddsa-jcs-2022": "0.1.0-rc.3", + "@kiltprotocol/es256k-jcs-2023": "0.1.0-rc.3", + "@kiltprotocol/jcs-data-integrity-proofs-common": "0.1.0-rc.3", + "@kiltprotocol/sr25519-jcs-2023": "0.1.0-rc.3", "@kiltprotocol/types": "workspace:*", "@kiltprotocol/utils": "workspace:*", "@polkadot/api": "^12.0.0", diff --git a/packages/did/package.json b/packages/did/package.json index 7ed162a1b..cf974958c 100644 --- a/packages/did/package.json +++ b/packages/did/package.json @@ -40,13 +40,15 @@ "@digitalbazaar/multikey-context": "^2.0.1", "@digitalbazaar/security-context": "^1.0.1", "@kiltprotocol/config": "workspace:*", + "@kiltprotocol/jcs-data-integrity-proofs-common": "0.1.0-rc.3", "@kiltprotocol/types": "workspace:*", "@kiltprotocol/utils": "workspace:*", "@polkadot/api": "^12.0.0", "@polkadot/keyring": "^13.0.0", "@polkadot/types": "^12.0.0", "@polkadot/util": "^13.0.0", - "@polkadot/util-crypto": "^13.0.0" + "@polkadot/util-crypto": "^13.0.0", + "varint": "^6.0.0" }, "peerDependenciesMeta": { "@kiltprotocol/augment-api": { diff --git a/packages/did/src/Did.utils.ts b/packages/did/src/Did.utils.ts index 97591482a..0e46e0726 100644 --- a/packages/did/src/Did.utils.ts +++ b/packages/did/src/Did.utils.ts @@ -5,13 +5,10 @@ * found in the LICENSE file in the root directory of this source tree. */ -import { - blake2AsU8a, - encodeAddress, - base58Decode, - base58Encode, -} from '@polkadot/util-crypto' +import { blake2AsU8a, encodeAddress, base58Encode } from '@polkadot/util-crypto' +import { u8aConcat } from '@polkadot/util' import type { + Base58BtcMultibaseString, Did, DidUrl, KeyringPair, @@ -20,6 +17,9 @@ import type { VerificationMethod, } from '@kiltprotocol/types' import { DataUtils, SDKErrors, ss58Format } from '@kiltprotocol/utils' +// @ts-expect-error Not a typescript module +import * as varint from 'varint' +import { decodeBase58BtcMultikey } from '@kiltprotocol/jcs-data-integrity-proofs-common' import type { DidVerificationMethodType } from './DidDetails/DidDetails.js' import { parseDocumentFromLightDid } from './DidDetails/LightDidDetails.js' @@ -161,31 +161,36 @@ const multicodecReversePrefixes: Record = { export function multibaseKeyToDidKey( publicKeyMultibase: VerificationMethod['publicKeyMultibase'] ): DecodedVerificationMethod { - if (!publicKeyMultibase.startsWith('z')) { - throw new SDKErrors.DidError(`invalid format for '${publicKeyMultibase}'`) - } - const decodedMulticodecPublicKey = base58Decode(publicKeyMultibase.slice(1)) - const [keyTypeFlag, publicKey] = [ - decodedMulticodecPublicKey.subarray(0, 1)[0], - decodedMulticodecPublicKey.subarray(1), - ] - const [keyType, expectedPublicKeyLength] = multicodecPrefixes[keyTypeFlag] + const { keyBytes, prefix } = decodeBase58BtcMultikey(publicKeyMultibase) + + const [keyType, expectedPublicKeyLength] = multicodecPrefixes[prefix] if (keyType === undefined) { throw new SDKErrors.DidError( `Cannot decode key type for multibase key "${publicKeyMultibase}".` ) } - if (publicKey.length !== expectedPublicKeyLength) { + if (keyBytes.length !== expectedPublicKeyLength) { throw new SDKErrors.DidError( - `Key of type "${keyType}" is expected to be ${expectedPublicKeyLength} bytes long. Provided key is ${publicKey.length} bytes long instead.` + `Key of type "${keyType}" is expected to be ${expectedPublicKeyLength} bytes long. Provided key is ${keyBytes.length} bytes long instead.` ) } return { keyType, - publicKey, + publicKey: keyBytes, } } +// TODO: This could also be exposed in a new release candidate of the `@kiltprotocol/jcs-data-integrity-proofs-common` package. +function multibase58BtcKeyBytesEncoding( + key: Uint8Array, + keyPrefix: number +): Base58BtcMultibaseString { + const varintEncodedPrefix = varint.encode(keyPrefix) + const prefixedKey = u8aConcat(varintEncodedPrefix, key) + const base58BtcEncodedKey = base58Encode(prefixedKey) + return `z${base58BtcEncodedKey}` +} + /** * Calculate the Multikey representation of a keypair given its type and public key. * @@ -212,11 +217,12 @@ export function keypairToMultibaseKey({ `Key of type "${type}" is expected to be ${expectedPublicKeySize} bytes long. Provided key is ${publicKey.length} bytes long instead.` ) } - const multiCodecPublicKey = [multiCodecPublicKeyPrefix, ...publicKey] - - const encodedPublicKey = base58Encode(Uint8Array.from(multiCodecPublicKey)) + const prefixedEncodedPublicKey = multibase58BtcKeyBytesEncoding( + publicKey, + multiCodecPublicKeyPrefix + ) - return `z${encodedPublicKey}` + return prefixedEncodedPublicKey } /** @@ -246,10 +252,10 @@ export function didKeyToVerificationMethod( `Key of type "${keyType}" is expected to be ${expectedPublicKeySize} bytes long. Provided key is ${publicKey.length} bytes long instead.` ) } - const multiCodecPublicKey = [multiCodecPublicKeyPrefix, ...publicKey] - - const encodedPublicKey = base58Encode(Uint8Array.from(multiCodecPublicKey)) - const prefixedEncodedPublicKey = `z${encodedPublicKey}` as const + const prefixedEncodedPublicKey = multibase58BtcKeyBytesEncoding( + publicKey, + multiCodecPublicKeyPrefix + ) return { controller, diff --git a/packages/types/src/Did.ts b/packages/types/src/Did.ts index 2502e40a4..f65891a5d 100644 --- a/packages/types/src/Did.ts +++ b/packages/types/src/Did.ts @@ -47,7 +47,7 @@ export type DidSignature = { signature: string } -type Base58BtcMultibaseString = `z${string}` +export type Base58BtcMultibaseString = `z${string}` /** * The verification method of a DID. diff --git a/packages/utils/package.json b/packages/utils/package.json index 5e13deee7..aaf882a60 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -35,9 +35,9 @@ "typescript": "^4.8.3" }, "dependencies": { - "@kiltprotocol/eddsa-jcs-2022": "0.1.0-rc.2", - "@kiltprotocol/es256k-jcs-2023": "0.1.0-rc.2", - "@kiltprotocol/sr25519-jcs-2023": "0.1.0-rc.2", + "@kiltprotocol/eddsa-jcs-2022": "0.1.0-rc.3", + "@kiltprotocol/es256k-jcs-2023": "0.1.0-rc.3", + "@kiltprotocol/sr25519-jcs-2023": "0.1.0-rc.3", "@kiltprotocol/types": "workspace:*", "@polkadot/api": "^12.0.0", "@polkadot/keyring": "^13.0.0", diff --git a/yarn.lock b/yarn.lock index 81b38615b..50b27b8c1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2072,10 +2072,10 @@ __metadata: "@kiltprotocol/chain-helpers": "workspace:*" "@kiltprotocol/config": "workspace:*" "@kiltprotocol/did": "workspace:*" - "@kiltprotocol/eddsa-jcs-2022": "npm:0.1.0-rc.2" - "@kiltprotocol/es256k-jcs-2023": "npm:0.1.0-rc.2" - "@kiltprotocol/jcs-data-integrity-proofs-common": "npm:0.1.0-rc.2" - "@kiltprotocol/sr25519-jcs-2023": "npm:0.1.0-rc.2" + "@kiltprotocol/eddsa-jcs-2022": "npm:0.1.0-rc.3" + "@kiltprotocol/es256k-jcs-2023": "npm:0.1.0-rc.3" + "@kiltprotocol/jcs-data-integrity-proofs-common": "npm:0.1.0-rc.3" + "@kiltprotocol/sr25519-jcs-2023": "npm:0.1.0-rc.3" "@kiltprotocol/types": "workspace:*" "@kiltprotocol/utils": "workspace:*" "@polkadot/api": "npm:^12.0.0" @@ -2102,6 +2102,7 @@ __metadata: "@digitalbazaar/multikey-context": "npm:^2.0.1" "@digitalbazaar/security-context": "npm:^1.0.1" "@kiltprotocol/config": "workspace:*" + "@kiltprotocol/jcs-data-integrity-proofs-common": "npm:0.1.0-rc.3" "@kiltprotocol/types": "workspace:*" "@kiltprotocol/utils": "workspace:*" "@polkadot/api": "npm:^12.0.0" @@ -2111,6 +2112,7 @@ __metadata: "@polkadot/util-crypto": "npm:^13.0.0" rimraf: "npm:^3.0.2" typescript: "npm:^4.8.3" + varint: "npm:^6.0.0" peerDependencies: "@kiltprotocol/augment-api": "*" peerDependenciesMeta: @@ -2119,37 +2121,38 @@ __metadata: languageName: unknown linkType: soft -"@kiltprotocol/eddsa-jcs-2022@npm:0.1.0-rc.2": - version: 0.1.0-rc.2 - resolution: "@kiltprotocol/eddsa-jcs-2022@npm:0.1.0-rc.2" +"@kiltprotocol/eddsa-jcs-2022@npm:0.1.0-rc.3": + version: 0.1.0-rc.3 + resolution: "@kiltprotocol/eddsa-jcs-2022@npm:0.1.0-rc.3" dependencies: - "@kiltprotocol/jcs-data-integrity-proofs-common": "npm:^0.1.0-rc.2" + "@kiltprotocol/jcs-data-integrity-proofs-common": "npm:^0.1.0-rc.3" "@noble/curves": "npm:^1.0.0" "@scure/base": "npm:^1.1.1" - checksum: 10c0/b97ba7c517dec33a670d5f7f906f8f49e7734aeec411c7d15c1560f5d89d70fc77553ec5ee188e9734b827016889d1bac0e2982de403c30356cc8b3bd286f9b5 + checksum: 10c0/b29228114faf0322bf21a372c2c4c99379d9d9006bb9a94a7029211bce47321f185ea2fe8182499ac4bcdaa5dd4aad0eafccea1e22cfbcb16058be17a0c3c164 languageName: node linkType: hard -"@kiltprotocol/es256k-jcs-2023@npm:0.1.0-rc.2": - version: 0.1.0-rc.2 - resolution: "@kiltprotocol/es256k-jcs-2023@npm:0.1.0-rc.2" +"@kiltprotocol/es256k-jcs-2023@npm:0.1.0-rc.3": + version: 0.1.0-rc.3 + resolution: "@kiltprotocol/es256k-jcs-2023@npm:0.1.0-rc.3" dependencies: - "@kiltprotocol/jcs-data-integrity-proofs-common": "npm:^0.1.0-rc.2" + "@kiltprotocol/jcs-data-integrity-proofs-common": "npm:^0.1.0-rc.3" "@noble/curves": "npm:^1.0.0" "@scure/base": "npm:^1.1.1" - checksum: 10c0/e8575863eac40d87636184a9889665cd651e2754535097243a4524fb483ea5cd75e471a7b376c112d16ecac33413a99f8b32dd147a677e37336f910d253a6b20 + checksum: 10c0/02347bc7f199833b35995f89f4c13b02211ada54707f086896f841dd64bee1edf332c4e5139343d3d89b990c21fafd27de633e2c5a9c7106b6d5b91901e3d53e languageName: node linkType: hard -"@kiltprotocol/jcs-data-integrity-proofs-common@npm:0.1.0-rc.2, @kiltprotocol/jcs-data-integrity-proofs-common@npm:^0.1.0-rc.2": - version: 0.1.0-rc.2 - resolution: "@kiltprotocol/jcs-data-integrity-proofs-common@npm:0.1.0-rc.2" +"@kiltprotocol/jcs-data-integrity-proofs-common@npm:0.1.0-rc.3, @kiltprotocol/jcs-data-integrity-proofs-common@npm:^0.1.0-rc.3": + version: 0.1.0-rc.3 + resolution: "@kiltprotocol/jcs-data-integrity-proofs-common@npm:0.1.0-rc.3" dependencies: "@noble/hashes": "npm:^1.3.0" canonicalize: "npm:^2.0.0" + varint: "npm:^6.0.0" peerDependencies: "@scure/base": ^1.1.0 - checksum: 10c0/3e65a51695eb80be81c392c3851fccacb5beb880bc093744bcdaf73c3e9b70c363701a0d10d1050bd5b411856979cddb2e65ac7fed8db4cb47cf2d55a7307a21 + checksum: 10c0/0006f1d429bf34b4a1619f344cd194556906d9988cebf6f6d28a730689ca4e7c24e0dcff25f5f03150210f6fcb97b87d014af7cd80a55200baecbc3abcd1491e languageName: node linkType: hard @@ -2213,14 +2216,14 @@ __metadata: languageName: unknown linkType: soft -"@kiltprotocol/sr25519-jcs-2023@npm:0.1.0-rc.2": - version: 0.1.0-rc.2 - resolution: "@kiltprotocol/sr25519-jcs-2023@npm:0.1.0-rc.2" +"@kiltprotocol/sr25519-jcs-2023@npm:0.1.0-rc.3": + version: 0.1.0-rc.3 + resolution: "@kiltprotocol/sr25519-jcs-2023@npm:0.1.0-rc.3" dependencies: - "@kiltprotocol/jcs-data-integrity-proofs-common": "npm:^0.1.0-rc.2" + "@kiltprotocol/jcs-data-integrity-proofs-common": "npm:^0.1.0-rc.3" "@polkadot/util-crypto": "npm:^12.0.1" "@scure/base": "npm:^1.1.1" - checksum: 10c0/fd56ad4815df46b9fae904f4a638df7db8e2d81a6ea18e16b41a5471cd7334beed9727e019e26b2d2c7abea213da4e585181788cc2918a8975c844646e8a6297 + checksum: 10c0/789d06939bed32e41f4543427f3eb32773fd39385ff1f09ff9df345f620c1fe67a1e249e1afef29f4b054e2a79388ff1fea59dd26136565d95f7bf7bda248c0f languageName: node linkType: hard @@ -2249,9 +2252,9 @@ __metadata: version: 0.0.0-use.local resolution: "@kiltprotocol/utils@workspace:packages/utils" dependencies: - "@kiltprotocol/eddsa-jcs-2022": "npm:0.1.0-rc.2" - "@kiltprotocol/es256k-jcs-2023": "npm:0.1.0-rc.2" - "@kiltprotocol/sr25519-jcs-2023": "npm:0.1.0-rc.2" + "@kiltprotocol/eddsa-jcs-2022": "npm:0.1.0-rc.3" + "@kiltprotocol/es256k-jcs-2023": "npm:0.1.0-rc.3" + "@kiltprotocol/sr25519-jcs-2023": "npm:0.1.0-rc.3" "@kiltprotocol/types": "workspace:*" "@polkadot/api": "npm:^12.0.0" "@polkadot/keyring": "npm:^13.0.0" @@ -9998,6 +10001,13 @@ __metadata: languageName: node linkType: hard +"varint@npm:^6.0.0": + version: 6.0.0 + resolution: "varint@npm:6.0.0" + checksum: 10c0/737fc37088a62ed3bd21466e318d21ca7ac4991d0f25546f518f017703be4ed0f9df1c5559f1dd533dddba4435a1b758fd9230e4772c1a930ef72b42f5c750fd + languageName: node + linkType: hard + "vscode-oniguruma@npm:^1.7.0": version: 1.7.0 resolution: "vscode-oniguruma@npm:1.7.0"