Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…tiplink into dev
  • Loading branch information
Rudra-Sankha-Sinhamahapatra committed Oct 5, 2024
2 parents 7aa0a5e + 8ebdcd8 commit a260ff8
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 34 deletions.
5 changes: 4 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ ENCRYPTION_KEY = ''

#AWS KMS
AWS_CMK_ARN =
AWS_SECRET_ACCESS_KEY = # IAM user access key (check for correct policies though)
AWS_ACCESS_KEY_ID = # IAM user secret key (check for correct policies though)

#GCP KMS
PROJECT_ID = ''
LOCATION_ID = ''
KEY_RING_ID = ''
KEY_ID = ''
KEY_ID = ''
GOOGLE_APPLICATION_CREDENTIALS = # <path_to_your_json_file> Look into Service accounts in GCP console for more info
143 changes: 143 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@
"@solana/web3.js": "^1.95.2",
"@types/bs58": "^4.0.4",
"@types/qrcode.react": "^1.0.5",
"aws-sdk": "^2.1691.0",
"axios": "^1.7.7",
"bs58": "^4.0.1",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
"ed25519": "^0.0.5",
"framer-motion": "^11.3.21",
"lucide-react": "^0.419.0",
"next": "14.2.5",
Expand Down
41 changes: 23 additions & 18 deletions src/actions/pvtKeyEncryptMgmt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,30 @@ import { splitSecret } from '@/services/keyShardingService'
import { getServerSession } from 'next-auth'

export async function pvtKeyEncryptionManager(privateKey: string) {
const session = await getServerSession(authOptions)
const userId = session?.user?.id
try {
const session = await getServerSession(authOptions)
const userId = session?.user?.id

const { aesShareString, awsShareString, gcpShareString } =
await splitSecret(privateKey)
const { aesShareString, awsShareString, gcpShareString } =
await splitSecret(privateKey)

const aesEncryptedShare = aesEncrypt(aesShareString)
const awsEncryptedShare = await awsEncrypt(awsShareString, {
purpose: 'tiplink',
country: 'India',
})
const gcpEncryptedShare = await gcpEncrypt(gcpShareString)
const aesEncryptedShare = aesEncrypt(aesShareString)

await prisma.user.update({
where: { id: userId },
data: {
aesShare: aesEncryptedShare,
awsShare: awsEncryptedShare,
gcpShare: gcpEncryptedShare,
},
})
const awsEncryptedShare = await awsEncrypt(awsShareString, {
purpose: 'tiplink',
country: 'India',
})
const gcpEncryptedShare = await gcpEncrypt(gcpShareString)

await prisma.user.update({
where: { id: userId },
data: {
aesShare: aesEncryptedShare,
awsShare: awsEncryptedShare,
gcpShare: gcpEncryptedShare,
},
})
} catch (error) {
throw new Error(`Failed to encrypt private key: ${error}`)
}
}
27 changes: 14 additions & 13 deletions src/services/aes-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@ export function aesEncrypt(plainText: any) {
if (!ENCRYPTION_KEY) {
throw new Error('Encryption key is not set')
}

const iv = crypto.randomBytes(IV_LENGTH)

const cipher = crypto.createCipheriv(
'aes-256-cbc',
Buffer.from(ENCRYPTION_KEY),
iv,
)

let encrypted = cipher.update(plainText, 'utf8', 'hex')
encrypted += cipher.final('hex')

return iv.toString('hex') + ':' + encrypted
try {
const encryptionKey32 = crypto.createHash('sha256').update(Buffer.from(ENCRYPTION_KEY, 'hex')).digest();
const iv = crypto.randomBytes(IV_LENGTH)
const cipher = crypto.createCipheriv(
'aes-256-ctr',
encryptionKey32,
iv
)
let encrypted = cipher.update(plainText, 'utf8', 'hex')
encrypted += cipher.final('hex')
return iv.toString('hex') + ':' + encrypted
} catch (error) {
throw new Error(`AES fails: ${error}`)
}
}

export function aesDecrypt(encryptedData: any) {
Expand Down
8 changes: 8 additions & 0 deletions src/services/aws-kms-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import {
KmsKeyringNode,
buildClient,
} from '@aws-crypto/client-node'
import AWS from 'aws-sdk'

const credentials = {
accessKeyId: process.env.AWS_ACCESS_KEY_ID!,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!
}

AWS.config.update({ credentials })

const generatorKeyId = process.env.AWS_CMK_ARN
if (!generatorKeyId) {
Expand Down
4 changes: 2 additions & 2 deletions src/services/keyShardingService.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import base58 from 'bs58'
import {
split as shamirSplit,
combine as shamirCombine,
} from 'shamir-secret-sharing'

import * as bs58 from 'bs58'

export async function splitSecret(privateKey: string) {
if (!privateKey) {
throw new Error('Private key is undefined')
}
try {
const secretKeyUint8Array = new Uint8Array(bs58.decode(privateKey))
const secretKeyUint8Array = new Uint8Array(base58.decode(privateKey))

const shares = await shamirSplit(secretKeyUint8Array, 3, 3)

Expand Down

0 comments on commit a260ff8

Please sign in to comment.