-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
1,953 additions
and
301 deletions.
There are no files selected for viewing
Empty file.
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,44 @@ | ||
{ | ||
"name": "@sphereon/ssi-sdk-ext.musap-rn-kms", | ||
"description": "Sphereon SSI-SDK react-native plugin for management of keys with musap.", | ||
"version": "0.22.0", | ||
"source": "src/index.ts", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"scripts": { | ||
"build": "tsc --build", | ||
"build:clean": "tsc --build --clean && tsc --build" | ||
}, | ||
"dependencies": { | ||
"@sphereon/musap-react-native": "0.0.1-next.139", | ||
"@types/uuid": "^8.3.4", | ||
"@veramo/core": "4.2.0", | ||
"@veramo/key-manager": "4.2.0", | ||
"@veramo/kms-local": "4.2.0", | ||
"react-native-get-random-values": "1.11.0", | ||
"text-encoding": "^0.7.0", | ||
"uuid": "^10.0.0" | ||
}, | ||
"devDependencies": { | ||
"@types/text-encoding": "0.0.39" | ||
}, | ||
"files": [ | ||
"dist/**/*", | ||
"src/**/*", | ||
"README.md", | ||
"LICENSE" | ||
], | ||
"private": false, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"repository": "[email protected]:Sphereon-OpenSource/SSI-SDK-crypto-extensions.git", | ||
"author": "Sphereon <[email protected]>", | ||
"license": "Apache-2.0", | ||
"keywords": [ | ||
"musap", | ||
"key-management", | ||
"react-native", | ||
"Veramo" | ||
] | ||
} |
127 changes: 127 additions & 0 deletions
127
packages/musap-rn-kms/src/agent/MusapKeyManagerSystem.ts
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,127 @@ | ||
import { IKey, ManagedKeyInfo, MinimalImportableKey, TKeyType } from '@veramo/core'; | ||
import { | ||
KeyAlgorithmType, | ||
KeyGenReq, | ||
MusapKey, | ||
MusapModuleType, signatureAlgorithmFromKeyAlgorithm, | ||
SignatureAlgorithmType, | ||
SignatureFormat, | ||
SignatureReq, | ||
} from '@sphereon/musap-react-native' | ||
import { SscdType } from '@sphereon/musap-react-native/src/types/musap-types'; | ||
import { KeyManagementSystem } from '@veramo/kms-local'; | ||
import { AbstractPrivateKeyStore } from '@veramo/key-manager'; | ||
import 'react-native-get-random-values' | ||
import { v4 as uuid } from 'uuid'; | ||
import { TextDecoder } from 'text-encoding'; | ||
import Debug from 'debug' | ||
|
||
const debug = Debug('sphereon:musap-rn-kms') | ||
|
||
export class MusapKeyManagementSystem extends KeyManagementSystem { | ||
private musapKeyStore: MusapModuleType; | ||
private sscdType: SscdType; | ||
|
||
constructor(keyStore: MusapModuleType, sscdType?: SscdType) { | ||
super(keyStore as unknown as AbstractPrivateKeyStore); | ||
this.musapKeyStore = keyStore; | ||
this.sscdType = sscdType ? sscdType : 'TEE'; | ||
this.musapKeyStore.enableSscd(this.sscdType) | ||
} | ||
|
||
async listKeys(): Promise<ManagedKeyInfo[]> { | ||
try { | ||
const keysJson: MusapKey[] = (await this.musapKeyStore.listKeys()) as MusapKey[]; | ||
return keysJson.map((key) => this.asMusapKeyInfo(key)); | ||
} catch (error) { | ||
throw error; | ||
} | ||
} | ||
|
||
async createKey(args: { type: TKeyType; sscdType?: SscdType }): Promise<ManagedKeyInfo> { | ||
const keyAlgorithm = this.mapKeyTypeToAlgorithmType(args.type); | ||
|
||
const keyGenReq: KeyGenReq = { | ||
keyAlgorithm: keyAlgorithm, | ||
did: '', | ||
keyUsage: 'sign', | ||
keyAlias: uuid(), | ||
attributes: [ | ||
{ name: 'purpose', value: 'encrypt' }, | ||
{ name: 'purpose', value: 'decrypt' } | ||
], | ||
role: 'administrator' | ||
}; | ||
|
||
try { | ||
const generatedKeyUri = await this.musapKeyStore.generateKey(this.sscdType, keyGenReq); | ||
if (generatedKeyUri) { | ||
debug('Generated key:', generatedKeyUri); | ||
const key = await this.musapKeyStore.getKeyByUri(generatedKeyUri); | ||
return this.asMusapKeyInfo(key); | ||
} else { | ||
throw new Error('Failed to generate key'); | ||
} | ||
} catch (error) { | ||
console.error('An error occurred:', error); | ||
throw error; | ||
} | ||
} | ||
|
||
mapKeyTypeToAlgorithmType = (type: TKeyType): KeyAlgorithmType => { | ||
switch (type) { | ||
case 'Secp256k1': | ||
return 'ECCP256K1'; | ||
case 'Secp256r1': | ||
return 'ECCP256R1'; | ||
case 'RSA': | ||
return 'RSA2K'; | ||
default: | ||
throw new Error(`Key type ${type} is not supported by MUSAP`); | ||
} | ||
} | ||
|
||
async deleteKey({ kid }: { kid: string }): Promise<boolean> { | ||
try { | ||
await this.musapKeyStore.removeKey(kid); | ||
return true; | ||
} catch (error) { | ||
console.warn('Failed to delete key:', error); | ||
return false; | ||
} | ||
} | ||
|
||
async sign(args: { keyRef: Pick<IKey, 'kid'>; algorithm?: string; data: Uint8Array; [x: string]: any }): Promise<string> { | ||
if (!args.keyRef) { | ||
throw new Error('key_not_found: No key ref provided'); | ||
} | ||
|
||
const data = new TextDecoder().decode(args.data as Uint8Array) | ||
|
||
const key: MusapKey = (this.musapKeyStore.getKeyById(args.keyRef.kid)) as MusapKey; | ||
const signatureReq: SignatureReq = { | ||
keyUri: key.keyUri, | ||
data, | ||
algorithm: args.algorithm as SignatureAlgorithmType ?? signatureAlgorithmFromKeyAlgorithm(key.algorithm), | ||
displayText: args.displayText, | ||
transId: args.transId, | ||
format: args.format as SignatureFormat ?? 'RAW', | ||
attributes: args.attributes | ||
} | ||
return this.musapKeyStore.sign(signatureReq) | ||
} | ||
|
||
async importKey(args: Omit<MinimalImportableKey, 'kms'> & { privateKeyPEM?: string }): Promise<ManagedKeyInfo> { | ||
throw new Error('Not implemented.'); | ||
} | ||
|
||
private asMusapKeyInfo(args: MusapKey): ManagedKeyInfo & { keyUri?: string } { | ||
return { | ||
kid: args.keyId, | ||
kms: args.sscdId, | ||
type: args.keyType as unknown as TKeyType, | ||
publicKeyHex: args.publicKey.toString(), | ||
keyUri: args.keyUri | ||
}; | ||
} | ||
} |
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 @@ | ||
export * from './agent/MusapKeyManagerSystem' |
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,8 @@ | ||
{ | ||
"extends": "../tsconfig-base.json", | ||
"compilerOptions": { | ||
"rootDir": "src", | ||
"outDir": "dist", | ||
"declarationDir": "dist" | ||
} | ||
} |
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 |
---|---|---|
|
@@ -36,6 +36,9 @@ | |
}, | ||
{ | ||
"path": "mnemonic-seed-manager" | ||
}, | ||
{ | ||
"path": "musap-rn-kms" | ||
} | ||
] | ||
} |
Oops, something went wrong.