From 0bff0fc6e529087cbe91e9822cec0adcd4100e26 Mon Sep 17 00:00:00 2001 From: Moein zargarzadeh Date: Fri, 8 Nov 2024 14:11:21 +0330 Subject: [PATCH 1/3] refactor readPrivateKeyFromFile to read privKey with base64pad encoding --- .gitignore | 1 + packages/utils/lib/readPrivateKeyFromFile.ts | 25 ++++++++++++++------ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index d1b4e01..8433b2e 100644 --- a/.gitignore +++ b/.gitignore @@ -137,3 +137,4 @@ tests/*/*.tfstate* tests/*/terraform.tfvars .vscode/settings.json +.idea diff --git a/packages/utils/lib/readPrivateKeyFromFile.ts b/packages/utils/lib/readPrivateKeyFromFile.ts index 90a4c77..dc205a3 100644 --- a/packages/utils/lib/readPrivateKeyFromFile.ts +++ b/packages/utils/lib/readPrivateKeyFromFile.ts @@ -1,6 +1,10 @@ import { generateKeyPair, marshalPrivateKey } from '@libp2p/crypto/keys'; import { readFile, writeFile } from 'fs/promises'; import { resolve } from 'path'; +import { + fromString as uint8ArrayFromString, + toString as uint8ArrayToString, +} from 'uint8arrays'; /** * read private key from a file and return its string representation, creating @@ -10,16 +14,23 @@ import { resolve } from 'path'; const readPrivateKeyFromFile = async (path: string) => { const fullPath = resolve(path); try { - return await readFile(fullPath, 'utf-8'); + const fileData = await readFile(fullPath, 'utf-8'); + const nodeDataJson = JSON.parse(fileData); + const privKeyUint8Array = uint8ArrayFromString( + nodeDataJson.privKey, + 'base64pad', + ); + return Buffer.from(privKeyUint8Array).toString('hex'); } catch (error) { const privateKey = await generateKeyPair('Ed25519'); - const privateKeyString = Buffer.from( - marshalPrivateKey(privateKey), - ).toString('hex'); + const marshalPrivKey = marshalPrivateKey(privateKey); + const privateKeyString = uint8ArrayToString(marshalPrivKey, 'base64pad'); + const nodeDataJson = { + privKey: privateKeyString, + }; + await writeFile(fullPath, JSON.stringify(nodeDataJson), 'utf-8'); - await writeFile(fullPath, privateKeyString, 'utf-8'); - - return privateKeyString; + return Buffer.from(marshalPrivKey).toString('hex'); } }; From b8555ab960f53446f167d3d946cb95f03e5304bf Mon Sep 17 00:00:00 2001 From: Moein zargarzadeh Date: Fri, 8 Nov 2024 14:15:29 +0330 Subject: [PATCH 2/3] add changelog --- .changeset/stale-baboons-yawn.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/stale-baboons-yawn.md diff --git a/.changeset/stale-baboons-yawn.md b/.changeset/stale-baboons-yawn.md new file mode 100644 index 0000000..5b5a8ca --- /dev/null +++ b/.changeset/stale-baboons-yawn.md @@ -0,0 +1,5 @@ +--- +'@rosen-bridge/rosenet-utils': minor +--- + +refactor readPrivateKeyFromFile to read privKey with base64pad encoding From f05bfb219eaa11d704458d76bfe41cd9bb163fc0 Mon Sep 17 00:00:00 2001 From: Moein Zargarzadeh Date: Fri, 8 Nov 2024 12:50:45 +0000 Subject: [PATCH 3/3] Update .changeset/stale-baboons-yawn.md --- .changeset/stale-baboons-yawn.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/stale-baboons-yawn.md b/.changeset/stale-baboons-yawn.md index 5b5a8ca..6432438 100644 --- a/.changeset/stale-baboons-yawn.md +++ b/.changeset/stale-baboons-yawn.md @@ -2,4 +2,4 @@ '@rosen-bridge/rosenet-utils': minor --- -refactor readPrivateKeyFromFile to read privKey with base64pad encoding +Refactor readPrivateKeyFromFile to read privKey with base64pad encoding