Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import {AwsConfigs} from '@logchain/configs/aws-configs';
import {inject} from '@loopback/core';
import {Body, Metadata, SSECustomerKey} from 'aws-sdk/clients/s3';
import {Wallet, Wallets, WalletStore} from 'fabric-network';
import * as fs from 'fs';
import * as path from 'path';
import {AwsServiceBindings} from '../keys';
import {AwsS3Service} from './aws-s3.service';
const suffix = '.id';
function isIdentityFile(file: string) {
return file.endsWith(suffix);
}
function toLabel(file: string) {
const endIndex = file.length - suffix.length;
return file.substring(0, endIndex);
}
export class S3WalletStore implements WalletStore {
@inject(AwsServiceBindings.S3_SERVICE)
public s3_service: AwsS3Service;
/**
* Create new instance of S3WalletStore and make path available
* @param {string} directory
* @returns Promise
*/
static async newInstance(directory: string): Promise<S3WalletStore> {
const mkdirOptions = {
recursive: true,
};
await fs.promises.mkdir(directory, mkdirOptions);
return new S3WalletStore(directory);
}
private readonly storePath: string;
private constructor(directory: string) {
this.storePath = directory;
this.s3_service = new AwsS3Service();
}
/**
* Remove a certificate from wallet and S3 bucket
* @param {string} label
* @returns Promise
*/
async remove(label: string): Promise<void> {
const file = this.toPath(label);
await fs.promises.unlink(file);
try {
await this.s3_service.deleteObject(this.toUri(label), AwsConfigs.s3Config.bucketNameForWallet);
} catch(error) {
console.log(error);
}
}
/**
* Get certificate from S3 bucket
* @param {string} label
* @returns Promise
*/
async get(label: string): Promise<Buffer | undefined> {
const file = this.toPath(label);
try {
// Get certificate from S3 bucket
if (!fs.existsSync(file)) {
const data = await this.s3_service.getObject(this.toUri(label), AwsConfigs.s3Config.bucketNameForWallet);
await fs.promises.writeFile(file, data?.Body as Buffer);
}
return await fs.promises.readFile(file);
} catch (error) {
return undefined;
}
}
/**
* Read available certificates on the wallet folder
* @returns Promise
*/
async list(): Promise<string[]> {
return (await fs.promises.readdir(this.storePath)).filter(isIdentityFile).map(toLabel);
}
/**
* Put certificate to S3 bucket
* @param {string} label
* @param {Buffer} data
* @returns Promise
*/
async put(label: string, data: Buffer): Promise<void> {
const file = this.toPath(label);
// Cache on local
await fs.promises.writeFile(file, data);
// Push to S3 bucket
const param = {
Key: this.toUri(label),
Body: data,
ContentEncoding: 'utf-8',
ContentType: 'text/plain; charset=utf-8',
ServerSideEncryption: 'AES256',
Bucket: AwsConfigs.s3Config.bucketNameForWallet,
};
await this.s3_service.putObject(param);
}
/**
* Convert file name to Real Path
* @param {string} label
*/
toPath(label: string) {
return path.join(this.storePath, label + suffix);
}
/**
* Convert file name to filekey
* @param {string} label
*/
toUri(label: string) {
return `wallets/${label}.id`;
}
}
export class S3Wallets extends Wallets {
/**
* Create new instance wallet with S3WalletStore
* @param {string} directory
*/
static async newFileSystemWallet(directory: string) {
const store = await S3WalletStore.newInstance(directory);
return new Wallet(store);
}
}
export class WalletBucketPutObjectRequest {
ACL?: string | undefined;
Body?: Body | undefined;
Bucket?: string = AwsConfigs.s3Config.bucketName;
CacheControl?: string | undefined;
ContentDisposition?: string | undefined;
ContentEncoding?: string | undefined;
ContentLanguage?: string | undefined;
ContentLength?: number | undefined;
ContentMD5?: string | undefined;
ContentType?: string | undefined;
Expires?: Date | undefined;
GrantFullControl?: string | undefined;
GrantRead?: string | undefined;
GrantReadACP?: string | undefined;
GrantWriteACP?: string | undefined;
Key: string;
Metadata?: Metadata | undefined;
ServerSideEncryption?: string | undefined;
StorageClass?: string | undefined;
WebsiteRedirectLocation?: string | undefined;
SSECustomerAlgorithm?: string | undefined;
SSECustomerKey?: SSECustomerKey | undefined;
SSECustomerKeyMD5?: string | undefined;
SSEKMSKeyId?: string | undefined;
SSEKMSEncryptionContext?: string | undefined;
BucketKeyEnabled?: boolean | undefined;
RequestPayer?: string | undefined;
Tagging?: string | undefined;
ObjectLockMode?: string | undefined;
ObjectLockRetainUntilDate?: Date | undefined;
ObjectLockLegalHoldStatus?: string | undefined;
ExpectedBucketOwner?: string | undefined;
}
|