Skip to content

feat: identity class #815

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
2 changes: 2 additions & 0 deletions packages/credentials/src/issuer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import type { UnsignedVc, VerifiableCredential } from './V1/types.js'
import type { CTypeLoader } from './ctype/index.js'
import type { IssuerOptions } from './interfaces.js'

export { IssuerOptions }

/**
* Creates a new credential document as a basis for issuing a credential.
* This document can be shown to users as a preview or be extended with additional properties before moving on to the second step of credential issuance:
Expand Down
53 changes: 51 additions & 2 deletions packages/did/src/Did.signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
* found in the LICENSE file in the root directory of this source tree.
*/

import { isHex } from '@polkadot/util'
import { isHex, u8aEq } from '@polkadot/util'
import type { Keypair } from '@polkadot/util-crypto/types'

import type {
DereferenceDidUrl,
Expand All @@ -14,9 +15,11 @@ import type {
Did,
DidUrl,
SignatureVerificationRelationship,
KeyringPair,
SignerInterface,
} from '@kiltprotocol/types'

import { Crypto, SDKErrors } from '@kiltprotocol/utils'
import { Crypto, SDKErrors, Signers } from '@kiltprotocol/utils'

import { multibaseKeyToDidKey, parse, validateDid } from './Did.utils.js'
import { dereference } from './DidResolver/DidResolver.js'
Expand Down Expand Up @@ -182,3 +185,49 @@ export function signatureFromJson(input: DidSignature | LegacyDidSignature): {
const signature = Crypto.coToUInt8(input.signature)
return { signature, keyUri }
}

/**
* Creates signers for {@link Did} based on a {@link DidDocument} and one or more {@link Keypair} / {@link KeyringPair}.
*
* @param didDocument The Did's DidDocument.
* @param keypairs One or more signing key pairs.
* @returns An array of signers based on the key pair.
*/
export async function signersForDid(
didDocument: DidDocument,
...keypairs: Array<Keypair | KeyringPair>
): Promise<Array<SignerInterface<string, DidUrl>>> {
const didKeys = didDocument.verificationMethod?.map(
({ publicKeyMultibase, id }) => ({
...multibaseKeyToDidKey(publicKeyMultibase),
id,
})
)
if (didKeys && didKeys.length !== 0) {
return (
await Promise.all(
keypairs.map(async (keypair) => {
const thisType = 'type' in keypair ? keypair.type : undefined
const matchingKey = didKeys?.find(({ publicKey, keyType }) => {
if (thisType && thisType !== keyType) {
return false
}
return u8aEq(publicKey, keypair.publicKey)
})
if (matchingKey) {
const id: DidUrl = matchingKey.id.startsWith('#')
? `${didDocument.id}${matchingKey.id}`
: (matchingKey.id as DidUrl)
return Signers.getSignersForKeypair({
keypair,
id,
type: matchingKey.keyType,
})
}
return []
})
)
).flat()
}
return []
}
3 changes: 2 additions & 1 deletion packages/sdk-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"@kiltprotocol/credentials": "workspace:*",
"@kiltprotocol/did": "workspace:*",
"@kiltprotocol/type-definitions": "^0.35.0",
"@kiltprotocol/utils": "workspace:*"
"@kiltprotocol/utils": "workspace:*",
"@polkadot/util": "^12.0.0"
},
"peerDependencies": {
"@kiltprotocol/augment-api": "^1.11210.0"
Expand Down
Loading