-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: vikastc <[email protected]>
- Loading branch information
Showing
6 changed files
with
269 additions
and
99 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -1,34 +1,73 @@ | ||
import * as Cord from '@cord.network/sdk'; | ||
import express from 'express'; | ||
import 'reflect-metadata'; | ||
import { processServiceData } from '../utils/DidValidationUtils' | ||
import { mnemonicGenerate } from '@polkadot/util-crypto'; | ||
import { | ||
addDelegateAsRegistryDelegate, | ||
authorIdentity, | ||
createDid | ||
} from '../init'; | ||
|
||
import { createDid } from '../init'; | ||
import { studio_encrypt } from '../identity/org'; | ||
|
||
export async function generateDid( | ||
export async function generateDid(req: express.Request, res: express.Response) { | ||
const { didName } = req.body; | ||
|
||
try { | ||
const { mnemonic, document } = await createDid(didName); | ||
|
||
return res.status(200).json({ | ||
result: { | ||
message: 'Successfully created did', | ||
mnemonic, | ||
document, | ||
}, | ||
}); | ||
} catch (error) { | ||
console.log('err: ', error); | ||
return res.status(400).json({ error: 'Did not created' }); | ||
} | ||
} | ||
|
||
export async function didNameNewCheck( | ||
req: express.Request, | ||
res: express.Response | ||
) { | ||
const id = req.params.id; | ||
const api = Cord.ConfigService.get('api'); | ||
|
||
try { | ||
if (!authorIdentity) { | ||
await addDelegateAsRegistryDelegate(); | ||
} | ||
const serviceData = req.body.services[0]; | ||
const processedService = processServiceData(serviceData); | ||
const { mnemonic, delegateKeys, document } = await createDid(authorIdentity, processedService); | ||
|
||
return res.status(200).json({ mnemonic, delegateKeys, document }); | ||
const encodedDidNameOwner = await api.call.didApi.queryByName(id); | ||
|
||
// Check if the DID has a linked URI | ||
const hasUri = encodedDidNameOwner?.isSome | ||
? Boolean( | ||
Cord.Did.linkedInfoFromChain(encodedDidNameOwner)?.document?.uri | ||
) | ||
: false; | ||
|
||
return res.status(200).json({ result: hasUri }); | ||
} catch (error) { | ||
console.log('err: ', error); | ||
return res.status(500).json({ error: 'Did not created' }); | ||
console.error('Error querying DID name:', error); | ||
return res | ||
.status(400) | ||
.json({ success: false, message: 'Internal server error' }); | ||
} | ||
} | ||
|
||
export async function encryptMnemonic( | ||
req: express.Request, | ||
res: express.Response | ||
) { | ||
try { | ||
const { issuerMnemonic } = req.body; | ||
|
||
const encryptedMnemonic = JSON.stringify( | ||
await studio_encrypt(issuerMnemonic) | ||
); | ||
|
||
return res.status(200).json({ | ||
result: { message: 'Encryption Successfully', encryptedMnemonic }, | ||
}); | ||
} catch (error) { | ||
console.error('Error in encryption', error); | ||
return res | ||
.status(400) | ||
.json({ success: false, message: 'Internal server error' }); | ||
} | ||
} |
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
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,99 @@ | ||
import { cryptoWaitReady } from '@polkadot/util-crypto'; | ||
import { Keyring } from '@polkadot/keyring'; | ||
import { KeyringInstance, KeyringPair } from '@polkadot/keyring/types'; | ||
import { | ||
mnemonicToMiniSecret, | ||
mnemonicGenerate, | ||
naclDecrypt, | ||
naclEncrypt, | ||
} from '@polkadot/util-crypto'; | ||
import { stringToU8a, u8aToString, u8aToHex, hexToU8a } from '@polkadot/util'; | ||
import nacl, { BoxKeyPair } from 'tweetnacl'; | ||
|
||
export type Identity = { | ||
key: KeyringPair; | ||
boxPair: BoxKeyPair; | ||
}; | ||
|
||
export type EncryptedString = { | ||
encrypt: string; | ||
nonce: string; | ||
}; | ||
|
||
let studio_identity: Identity; | ||
|
||
let keyring: KeyringInstance; | ||
|
||
async function keyringInit() { | ||
await cryptoWaitReady(); | ||
|
||
keyring = new Keyring({ type: 'sr25519', ss58Format: 29 }); | ||
} | ||
|
||
async function generateNewKey(phrase: string) { | ||
if (!keyring) { | ||
await keyringInit(); | ||
} | ||
const seed = mnemonicToMiniSecret(phrase); | ||
return { | ||
key: keyring.addFromSeed(seed), | ||
boxPair: nacl.box.keyPair.fromSecretKey(seed), | ||
}; | ||
} | ||
|
||
export async function studio_identity_init(mnemonic: string) { | ||
studio_identity = await generateNewKey(mnemonic); | ||
} | ||
|
||
export async function org_identity_create() { | ||
const mnemonic = mnemonicGenerate(); | ||
const org: [string, Identity] = [mnemonic, await generateNewKey(mnemonic)]; | ||
return org; | ||
} | ||
|
||
export async function encrypt(key: Identity, u8data: Buffer) { | ||
//const u8data = stringToU8a(data); | ||
const { encrypted, nonce } = naclEncrypt(u8data, key.boxPair.secretKey); | ||
return { encrypt: u8aToHex(encrypted), nonce: u8aToHex(nonce) }; | ||
} | ||
|
||
export async function decrypt(key: Identity, encrypted: EncryptedString) { | ||
const decrypt = naclDecrypt( | ||
hexToU8a(encrypted.encrypt), | ||
hexToU8a(encrypted.nonce), | ||
key.boxPair.secretKey | ||
); | ||
return decrypt; | ||
} | ||
|
||
export async function studio_encrypt(mnemonic: string) { | ||
const u8data = stringToU8a(mnemonic); | ||
const { encrypted, nonce } = naclEncrypt( | ||
u8data, | ||
studio_identity.boxPair.secretKey | ||
); | ||
return { encrypt: u8aToHex(encrypted), nonce: u8aToHex(nonce) }; | ||
} | ||
|
||
export async function studio_decrypt(encrypted: EncryptedString) { | ||
const decrypt = naclDecrypt( | ||
hexToU8a(encrypted.encrypt), | ||
hexToU8a(encrypted.nonce), | ||
studio_identity.boxPair.secretKey | ||
); | ||
return u8aToString(decrypt); | ||
} | ||
|
||
export async function org_identity_from_mnemonic(mnemonic: string) { | ||
return await generateNewKey(mnemonic); | ||
} | ||
|
||
export async function org_identity_from_encrypted_mnemonic( | ||
encrypted: EncryptedString | ||
) { | ||
const mnemonic = await studio_decrypt(encrypted); | ||
if (mnemonic) { | ||
return await generateNewKey(mnemonic); | ||
} | ||
return null; | ||
} |
Oops, something went wrong.