-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #180 from rahulbarmann/rahulbarmann/feat#169
Feat: add server action for decryption of shares and reconstruct it back to the private key
- Loading branch information
Showing
6 changed files
with
89 additions
and
30 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 +1,46 @@ | ||
//Here you create decryption + retrieval logic in a top level server action like the other file | ||
'use server' | ||
|
||
import prisma from '@/db' | ||
import { authOptions } from '@/lib/auth' | ||
import { aesDecrypt } from '@/services/aes-module' | ||
import { awsDecrypt } from '@/services/aws-kms-module' | ||
import { gcpDecrypt } from '@/services/gcp-kms-module' | ||
import { combineSecret } from '@/services/keyShardingService' | ||
import { getServerSession } from 'next-auth' | ||
import bs58 from 'bs58' | ||
|
||
export async function pvtKeyDecryptionManager() { | ||
try { | ||
const session = await getServerSession(authOptions) | ||
const userId = session.user.id | ||
|
||
const { aesShare, awsShare, gcpShare }: any = await prisma.user.findFirst({ | ||
where: { id: userId }, | ||
select: { aesShare: true, awsShare: true, gcpShare: true }, | ||
}) | ||
|
||
const decryptedAesShare = aesDecrypt(aesShare) | ||
const decryptedAwsShare = await awsDecrypt(awsShare, { | ||
purpose: 'tiplink', | ||
country: 'India', | ||
}) | ||
const decryptedGcpShare = await gcpDecrypt(gcpShare) | ||
|
||
const aesShareArray = new Uint8Array(Buffer.from(decryptedAesShare, 'hex')) | ||
const awsShareArray = new Uint8Array(Buffer.from(decryptedAwsShare, 'hex')) | ||
const gcpShareArray = new Uint8Array(Buffer.from(decryptedGcpShare, 'hex')) | ||
|
||
const res = await combineSecret([ | ||
aesShareArray, | ||
awsShareArray, | ||
gcpShareArray, | ||
]) | ||
|
||
const privateKey = bs58.encode(res) | ||
|
||
return privateKey | ||
} catch (error) { | ||
console.error('Error in pvtKeyDecryptionManager:', error) | ||
throw 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
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,21 +1,41 @@ | ||
import { split as shamirSplit, combine as shamirCombine } from 'shamir-secret-sharing'; | ||
const CryptoJS = require('crypto-js'); | ||
import { | ||
split as shamirSplit, | ||
combine as shamirCombine, | ||
} from 'shamir-secret-sharing' | ||
|
||
export async function splitSecret(secretKey: Uint8Array) { | ||
if (!secretKey) { | ||
throw new Error('Secret is undefined'); | ||
} | ||
try { | ||
const shares = await shamirSplit(secretKey, 3, 3); | ||
import * as bs58 from 'bs58' | ||
|
||
const [aesShare, awsShare, gcpShare] = shares; | ||
const aesShareString = Buffer.from(aesShare).toString('hex'); | ||
const awsShareString = Buffer.from(awsShare).toString('hex'); | ||
const gcpShareString = Buffer.from(gcpShare).toString('hex'); | ||
export async function splitSecret(privateKey: string) { | ||
if (!privateKey) { | ||
throw new Error('Private key is undefined') | ||
} | ||
try { | ||
const secretKeyUint8Array = new Uint8Array(bs58.decode(privateKey)) | ||
|
||
const shares = await shamirSplit(secretKeyUint8Array, 3, 3) | ||
|
||
return { aesShareString, awsShareString, gcpShareString }; | ||
} catch (error) { | ||
console.error('Error splitting and encrypting secret:', error); | ||
} | ||
const [aesShare, awsShare, gcpShare] = shares | ||
const aesShareString = Buffer.from(aesShare).toString('hex') | ||
const awsShareString = Buffer.from(awsShare).toString('hex') | ||
const gcpShareString = Buffer.from(gcpShare).toString('hex') | ||
|
||
return { aesShareString, awsShareString, gcpShareString } | ||
} catch (error) { | ||
console.error('Error splitting secret:', error) | ||
throw error | ||
} | ||
} | ||
|
||
export async function combineSecret(shares: Uint8Array[]) { | ||
if (!shares || shares.length === 0) { | ||
throw new Error('Shares are undefined or empty') | ||
} | ||
|
||
try { | ||
const secretKey = await shamirCombine(shares) | ||
return new Uint8Array(secretKey) | ||
} catch (e) { | ||
console.error('Error while combining shares: ', e) | ||
throw e | ||
} | ||
} |
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