123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- import { CID } from "multiformats/cid";
- import { sha256 } from "multiformats/hashes/sha2";
- import { fromString, toString } from "uint8arrays";
- import {
- Name,
- NameAccumulator,
- PrivateDirectory,
- PrivateForest,
- AccessKey,
- PublicDirectory,
- } from "../dist/bundler/wnfs_wasm";
- /** A mock CID. */
- const sampleCID = CID.parse(
- "bagaaierasords4njcts6vs7qvdjfcvgnume4hqohf65zsfguprqphs3icwea"
- ).bytes;
- /**
- * An in-memory block store to simulate IPFS.
- *
- * IPFS is basically a glorified HashMap.
- */
- class MemoryBlockStore {
- private store: Map<string, Uint8Array>;
- /** Creates a new in-memory block store. */
- constructor() {
- this.store = new Map();
- }
- /** Stores an array of bytes in the block store. */
- async getBlock(cid: Uint8Array): Promise<Uint8Array | undefined> {
- const decodedCid = CID.decode(cid);
- return this.store.get(decodedCid.toString());
- }
- /** Retrieves an array of bytes from the block store with given CID. */
- async putBlockKeyed(cid: Uint8Array, bytes: Uint8Array): Promise<void> {
- const decodedCid = CID.decode(cid);
- this.store.set(decodedCid.toString(), bytes);
- }
- /** Finds out whether a block is retrievable from this blockstore */
- async hasBlock(cid: Uint8Array): Promise<boolean> {
- const decodedCid = CID.decode(cid);
- return this.store.has(decodedCid.toString());
- }
- }
- /** A pseudo-random number generator */
- class Rng {
- /** Returns random bytes of specified length */
- randomBytes(count: number): Uint8Array {
- const array = new Uint8Array(count);
- self.crypto.getRandomValues(array);
- return array;
- }
- }
- /** A mock exchange key. */
- class ExchangeKey {
- key: CryptoKey;
- constructor(key: CryptoKey) {
- this.key = key;
- }
- static async fromModulus(modulus: Uint8Array): Promise<ExchangeKey> {
- var keyData = {
- kty: "RSA",
- n: toString(modulus, "base64url"),
- e: toString(new Uint8Array([0x01, 0x00, 0x01]), "base64url"),
- alg: "RSA-OAEP-256",
- ext: true,
- };
- const key = await crypto.subtle.importKey(
- "jwk",
- keyData,
- {
- name: "RSA-OAEP",
- hash: { name: "SHA-256" },
- },
- false,
- ["encrypt"]
- );
- return new ExchangeKey(key);
- }
- async encrypt(data: Uint8Array): Promise<Uint8Array> {
- const encryptedData = await window.crypto.subtle.encrypt(
- {
- name: "RSA-OAEP",
- },
- this.key,
- data
- );
- return new Uint8Array(encryptedData);
- }
- async getPublicKeyModulus(): Promise<Uint8Array> {
- const key = await crypto.subtle.exportKey("jwk", this.key);
- return fromString(key.n as string, "base64url");
- }
- }
- /** A mock private key. */
- class PrivateKey {
- key: CryptoKeyPair;
- constructor(key: CryptoKeyPair) {
- this.key = key;
- }
- static async generate(): Promise<PrivateKey> {
- const keyPair = await crypto.subtle.generateKey(
- {
- name: "RSA-OAEP",
- modulusLength: 2048,
- publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
- hash: { name: "SHA-256" },
- },
- true,
- ["decrypt"]
- );
- return new PrivateKey(keyPair);
- }
- async decrypt(data: Uint8Array): Promise<Uint8Array> {
- const decryptedData = await window.crypto.subtle.decrypt(
- {
- name: "RSA-OAEP",
- },
- this.key.privateKey,
- data
- );
- return new Uint8Array(decryptedData);
- }
- getPublicKey(): ExchangeKey {
- return new ExchangeKey(this.key.publicKey);
- }
- }
- const createSharerDir = async (
- initialForest: PrivateForest,
- store: MemoryBlockStore,
- rng: Rng
- ): Promise<{ rootDir: PrivateDirectory; forest: PrivateForest }> => {
- var { rootDir, forest } = await PrivateDirectory.newAndStore(
- initialForest.emptyName(),
- new Date(),
- initialForest,
- store,
- rng
- );
- return await rootDir.write(
- ["text.txt"],
- true,
- new Uint8Array([1, 2, 3, 4, 5]),
- new Date(),
- forest,
- store,
- rng
- );
- };
- const createRecipientExchangeRoot = async (
- store: MemoryBlockStore
- ): Promise<[PrivateKey, PublicDirectory]> => {
- const key = await PrivateKey.generate();
- const exchangeKey = await key.getPublicKey().getPublicKeyModulus();
- const { rootDir } = await new PublicDirectory(new Date()).write(
- ["device1", "v1.exchange_key"],
- exchangeKey,
- new Date(),
- store
- );
- return [key, rootDir];
- };
- class Sha256BlockStore {
- private store: Map<string, Uint8Array>;
- constructor() {
- this.store = new Map();
- }
- async getBlock(cid: Uint8Array): Promise<Uint8Array | undefined> {
- const decodedCid = CID.decode(cid);
- return this.store.get(decodedCid.toString());
- }
- async putBlockKeyed(cid: Uint8Array, bytes: Uint8Array): Promise<void> {
- const decodedCid = CID.decode(cid);
- this.store.set(decodedCid.toString(), bytes);
- }
- async hasBlock(cid: Uint8Array): Promise<boolean> {
- const decodedCid = CID.decode(cid);
- return this.store.has(decodedCid.toString());
- }
- // We overwrite the putBlock method
- async putBlock(bytes: Uint8Array, codec: number): Promise<Uint8Array> {
- const hash = await sha256.digest(bytes);
- const cid = CID.create(1, codec, hash);
- this.store.set(cid.toString(), bytes);
- return cid.bytes;
- }
- }
- export {
- sampleCID,
- CID,
- MemoryBlockStore,
- Sha256BlockStore,
- Rng,
- createSharerDir,
- createRecipientExchangeRoot,
- PrivateKey,
- ExchangeKey,
- AccessKey,
- };
|