Skip to content

Commit

Permalink
fix: bump @KILTprotocol jcs dependencies (#872)
Browse files Browse the repository at this point in the history
* Bump @KILTprotocol jcs dependencies
* Replace logic for decoding multikeys with our suite
* Add TODO
  • Loading branch information
ntn-x2 authored Jul 17, 2024
1 parent 0ff63cd commit b813e7c
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 61 deletions.
8 changes: 4 additions & 4 deletions packages/credentials/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 3 additions & 1 deletion packages/did/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
58 changes: 32 additions & 26 deletions packages/did/src/Did.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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'
Expand Down Expand Up @@ -161,31 +161,36 @@ const multicodecReversePrefixes: Record<DidVerificationMethodType, number> = {
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.
*
Expand All @@ -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
}

/**
Expand Down Expand Up @@ -246,10 +252,10 @@ export function didKeyToVerificationMethod<IdType extends DidUrl | UriFragment>(
`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,
Expand Down
2 changes: 1 addition & 1 deletion packages/types/src/Did.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export type DidSignature = {
signature: string
}

type Base58BtcMultibaseString = `z${string}`
export type Base58BtcMultibaseString = `z${string}`

/**
* The verification method of a DID.
Expand Down
6 changes: 3 additions & 3 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
62 changes: 36 additions & 26 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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:
Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit b813e7c

Please sign in to comment.