-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove fabric-network dependency (#6)
Couldn't overcome dependency conflicts for packages that depended on both this module and different versions of fabric-network. Read and write the old style wallet files directly instead of using fabric-network. Signed-off-by: Mark S. Lewis <mark_lewis@uk.ibm.com>
- Loading branch information
1 parent
f4df973
commit f1e8d4d
Showing
17 changed files
with
1,191 additions
and
884 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules | ||
dist | ||
*.tsbuildinfo | ||
coverage |
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 |
---|---|---|
|
@@ -4,3 +4,4 @@ tsconfig.json | |
src | ||
test | ||
jest.config.js | ||
coverage |
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,4 +1,5 @@ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
collectCoverage: true | ||
}; |
Large diffs are not rendered by default.
Oops, something went wrong.
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,122 @@ | ||
/** | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { User } from "./User"; | ||
import { IdentityConverter } from "./IdentityConverter"; | ||
import { IdentityData } from "./IdentityData"; | ||
|
||
import path = require("path"); | ||
import fs = require("fs"); | ||
import util = require("util"); | ||
import _rimraf = require("rimraf"); | ||
const rimraf = util.promisify(_rimraf); | ||
|
||
const encoding = "utf8"; | ||
const privateKeyExtension = "-priv"; | ||
|
||
export class FileSystemWalletStoreV1 { | ||
private readonly directory: string; | ||
private readonly converter = new IdentityConverter(); | ||
|
||
constructor(directory: string) { | ||
this.directory = directory; | ||
} | ||
|
||
async get(label: string): Promise<Buffer | undefined> { | ||
try { | ||
const user = await this.getUser(label); | ||
const privateKey = await this.getPrivateKey(user); | ||
const storeData = this.converter.userToStoreData(user, privateKey); | ||
const json = JSON.stringify(storeData); | ||
return Buffer.from(json, encoding); | ||
} catch (error) { | ||
return undefined; | ||
} | ||
} | ||
|
||
async list(): Promise<string[]> { | ||
const dirEntries = await fs.promises.readdir(this.directory, { withFileTypes: true }); | ||
const labels = dirEntries | ||
.filter((dirEntry) => dirEntry.isDirectory()) | ||
.map((dirEntry) => dirEntry.name); | ||
|
||
const results: string[] = []; | ||
for (const label of labels) { | ||
try { | ||
await this.getUser(label); | ||
results.push(label); | ||
} catch (error) { | ||
// Not a valid user | ||
} | ||
} | ||
|
||
return results; | ||
} | ||
|
||
async put(label: string, data: Buffer): Promise<void> { | ||
const json = data.toString(encoding); | ||
const storeData = JSON.parse(json) as IdentityData; | ||
const { user, privateKey } = this.converter.storeDataToUser(storeData, label); | ||
|
||
await this.createIdentityDir(label); | ||
await this.writeUser(user, label); | ||
if (privateKey) { | ||
await this.writePrivateKey(user, privateKey); | ||
} | ||
} | ||
|
||
async remove(label: string): Promise<void> { | ||
const identityDir = this.getIdentityDir(label); | ||
await rimraf(identityDir); | ||
} | ||
|
||
private async getUser(label: string): Promise<User> { | ||
const userPath = this.getUserPath(label); | ||
const userData = await fs.promises.readFile(userPath); | ||
return JSON.parse(userData.toString(encoding)); | ||
} | ||
|
||
private getUserPath(label: string) { | ||
const identityDir = this.getIdentityDir(label); | ||
return path.join(identityDir, label); | ||
} | ||
|
||
private getIdentityDir(label: string) { | ||
return path.join(this.directory, label); | ||
} | ||
|
||
private async getPrivateKey(user: User) { | ||
const keyPath = this.getPrivateKeyPath(user); | ||
try { | ||
const keyData = await fs.promises.readFile(keyPath); | ||
return keyData.toString(encoding).trim(); | ||
} catch (error) { | ||
// No private key | ||
return undefined; | ||
} | ||
} | ||
|
||
private getPrivateKeyPath(user: User) { | ||
const identityDir = this.getIdentityDir(user.name); | ||
const file = user.enrollment.signingIdentity + privateKeyExtension; | ||
return path.join(identityDir, file); | ||
} | ||
|
||
private async createIdentityDir(label: string) { | ||
const identityDir = this.getIdentityDir(label); | ||
await fs.promises.mkdir(identityDir); | ||
} | ||
|
||
private async writeUser(user: User, label: string) { | ||
const userJson = JSON.stringify(user); | ||
const userData = Buffer.from(userJson); | ||
const userPath = this.getUserPath(label); | ||
await fs.promises.writeFile(userPath, userData); | ||
} | ||
|
||
private async writePrivateKey(user: User, privateKey: string) { | ||
const privateKeyPath = this.getPrivateKeyPath(user); | ||
await fs.promises.writeFile(privateKeyPath, privateKey); | ||
} | ||
} |
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,13 @@ | ||
/** | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
export interface IdentityData { | ||
type: "X.509" | "HSM-X.509"; | ||
version: 1; | ||
credentials: { | ||
certificate: string; | ||
privateKey?: string; | ||
}; | ||
mspId: string; | ||
} |
This file was deleted.
Oops, something went wrong.
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,16 @@ | ||
/** | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
export interface User { | ||
name: string; | ||
mspid: string; | ||
enrollment: Enrollment; | ||
} | ||
|
||
export interface Enrollment { | ||
identity: { | ||
certificate: string; | ||
}; | ||
signingIdentity: string; | ||
} |
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.