Skip to content

Commit

Permalink
feat: optional privacy and asset blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
tammo committed Oct 2, 2020
1 parent af3c763 commit 69dc92e
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 140 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
"lib"
],
"dependencies": {
"jsbi": "3.1.1"
"jsbi": "3.1.4"
},
"devDependencies": {
"prettier": "1.18.2",
"typescript": "^3.9.5"
"prettier": "2.1.2",
"typescript": "4.0.3"
}
}
74 changes: 3 additions & 71 deletions src/Block.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { StorageId } from './Storage';
import { NTRUPublicKey } from './Keys';
import { AssetSymbol } from './Blockchain';

export type EncryptedNumber = string;
export type EncryptedString = string;
Expand All @@ -9,14 +9,6 @@ export class Signature extends String {}
export type SignatureData = {
type: string;
prev: Signature | undefined;
payload: string;
amountCommitment: string;
balanceCommitment: string;
amountRangeProof: string;
balanceRangeProof: string;
receiverAmount: string;
receiverBlindingFactorAmount: string;
senderBlindingFactorBalance: string;
senderBalance: string;
senderAmount: string;
refBlock: Signature | undefined;
Expand All @@ -30,6 +22,7 @@ export enum BlockType {
SEND = 'SEND',
DEPOSIT = 'DEPOSIT',
WITHDRAWAL = 'WITHDRAWAL',
ASSET = 'ASSET',
}

export class Block {
Expand All @@ -39,54 +32,21 @@ export class Block {
signature: Signature;
type: BlockType;
prev: Signature | undefined;
payload: string;
refBlock: Signature | undefined;
refAsset: string | undefined;
claimSignature: string | undefined;
createdAt: number;
amountCommitment: string;
balanceCommitment: string;
amountRangeProof: string;
balanceRangeProof: string;
receiverAmount: EncryptedNumber;
receiverBlindingFactorAmount: EncryptedString;
senderBlindingFactorBalance: EncryptedString;
senderBalance: EncryptedNumber;
senderAmount: EncryptedNumber;
publicNtruKey: NTRUPublicKey | undefined;

state?: string;

getDataForSignature(): SignatureData {
const {
type,
prev,
payload,
amountCommitment,
balanceCommitment,
amountRangeProof,
balanceRangeProof,
receiverAmount,
receiverBlindingFactorAmount,
senderBlindingFactorBalance,
senderBalance,
senderAmount,
refBlock,
refAsset,
claimSignature,
} = this;
const { type, prev, senderBalance, senderAmount, refBlock, refAsset, claimSignature } = this;

return {
type,
prev,
payload,
amountCommitment,
balanceCommitment,
amountRangeProof,
balanceRangeProof,
receiverAmount,
receiverBlindingFactorAmount,
senderBlindingFactorBalance,
senderBalance,
senderAmount,
refBlock,
Expand Down Expand Up @@ -122,10 +82,6 @@ export class Block {
if (prev) {
this.prev = prev.signature;
}

if (this.type === BlockType.SEND) {
this.receiverAmount = amount.toString();
}
}
}

Expand All @@ -135,46 +91,22 @@ export function fromBlockObject(obj: any) {
block.signature = obj.signature;
block.type = obj.type;
block.prev = obj.prev;
block.payload = obj.payload;
block.refBlock = obj.refBlock;
block.refAsset = obj.refAsset;
block.claimSignature = obj.claimSignature;
block.createdAt = obj.createdAt;
block.amountCommitment = obj.amountCommitment;
block.balanceCommitment = obj.balanceCommitment;
block.amountRangeProof = obj.amountRangeProof;
block.balanceRangeProof = obj.balanceRangeProof;
block.receiverAmount = obj.receiverAmount;
block.receiverBlindingFactorAmount = obj.receiverBlindingFactorAmount;
block.senderBlindingFactorBalance = obj.senderBlindingFactorBalance;
block.senderBalance = obj.senderBalance;
block.senderAmount = obj.senderAmount;
block.publicNtruKey = obj.publicNtruKey;
block.state = obj.state;

return block;
}

export type CommitmentData = {
type: string;
amount: string | number | bigint;
balance: string | number | bigint;
};

export type BlockValues = {
amount: string | number | bigint;
balance: string | number | bigint;
};

export type BlockCommitments = {
balanceCommitment: any;
amountCommitment: any;
amountRangeProof: any;
balanceRangeProof: any;
balanceCommitmentBlindFactor: any;
amountCommitmentBlindFactor: any;
};

export function isBlock(val: any): boolean {
if (!val) return false;

Expand Down
59 changes: 50 additions & 9 deletions src/Blockchain.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Block, fromBlockObject, BlockType } from './Block';
import { SigPublicKey, NTRUPublicKey } from './Keys';
import { SigPublicKey } from './Keys';
import { StorageId } from './Storage';

export enum AssetSymbol {
Expand All @@ -11,23 +11,56 @@ export class Blockchain {
id: StorageId;
leafId: StorageId | undefined;
publicSig: SigPublicKey;
publicNtru: NTRUPublicKey | undefined;
assetSymbol: AssetSymbol;
blocks: Block[] = [];

constructor(publicSig: SigPublicKey, publicNtru?: NTRUPublicKey, symbol?: AssetSymbol) {
constructor(publicSig: SigPublicKey) {
this.publicSig = publicSig;
this.publicNtru = publicNtru;
this.assetSymbol = symbol || AssetSymbol.TXL;
}

addBlock(block: Block): void {
this.blocks.push(block);
}

leaf(): Block | undefined {
const prevs = this.blocks.map(block => block.prev).filter(prev => !!prev);
return this.blocks.find(block => prevs.indexOf(block.signature) === -1);
const prevIndex: Record<string, Block> = {};

// index points to next blocks
// you can look up which blocks points to a signature
this.blocks.forEach(block => {
// only interested in asset blocks
if (block.type !== BlockType.ASSET) return;

prevIndex[block.prev as string] = block;
});

const anyAssetBlock = this.blocks.find(block => block.type === BlockType.ASSET);

if (!anyAssetBlock) {
// chains without assets block may have just one or zero blocks
return this.blocks[0];
}

return recursiveLeaf(prevIndex, anyAssetBlock);
}

leafAsset(asset: AssetSymbol): Block | undefined {
const prevIndex: Record<string, Block> = {};

// index points to next blocks
// you can look up which blocks points to a signature
this.blocks.forEach(block => {
// not interested in asset blocks
if (block.type === BlockType.ASSET) return;

prevIndex[block.prev as string] = block;
});

const assetBlock = this.blocks.find(block => block.type === BlockType.ASSET && block.refAsset === asset);

// chain has no asset created yet
if (!assetBlock) return;

return recursiveLeaf(prevIndex, assetBlock);
}

openingBlock(): Block | undefined {
Expand All @@ -39,8 +72,16 @@ export class Blockchain {
}
}

function recursiveLeaf(index: Record<string, Block>, current: Block): Block {
const nextBlock = index[current.signature as string];

if (!nextBlock) return current;

return recursiveLeaf(index, nextBlock);
}

export function fromBlockchainObject(obj: any) {
const chain = new Blockchain(obj.publicSig, obj.publicNtru, obj.assetSymbol);
const chain = new Blockchain(obj.publicSig);
chain.blocks = obj.blocks.map(deserializeBlock);

return chain;
Expand Down
22 changes: 0 additions & 22 deletions src/Crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,6 @@
*/
export interface Crypto {
randomBytes: (length: number) => any;
secp256k1: {
commit: (bf: any, amount: string) => any;
blindSum: (bfs: any[], mode: number) => any;
rangeProofSign: (
minValue: number,
commitment: any,
commitBlind: any,
nonce: any,
base10Exp: number,
minBits: number,
actualValue: string,
) => any;
};
aes: {
encrypt: (payload: string | String, key: any) => Promise<string>;
decrypt: (payload: string | String, key: any) => Promise<string>;
};
ntru: {
keyPair: (seed: any) => Promise<any>;
encrypt: (payload: string | String, key: any) => Promise<string>;
decrypt: (payload: string | String, key: any) => Promise<string>;
};
base64: {
toBytes: (payload: string | String) => any;
toString: (payload: any) => string;
Expand Down
8 changes: 0 additions & 8 deletions src/Keys.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
export class NTRUPrivateKey extends String {}
export class NTRUPublicKey extends String {}
export class SigPublicKey extends String {}
export class SigPrivateKey extends String {}
export class AESPrivateKey extends String {}

export type KeySet = {
aes: AESPrivateKey;
sig: {
privateKey: SigPrivateKey;
publicKey: SigPublicKey;
};
ntru: {
private: NTRUPrivateKey;
public: NTRUPublicKey;
};
};
20 changes: 10 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Block } from './Block';
import { Blockchain } from './Blockchain';
import { Transaction } from './Transaction';
import { Block } from './Block';
import { Blockchain } from './Blockchain';
import { Transaction } from './Transaction';

export * from './Block';
export * from './Blockchain';
export * from './Crypto';
export * from './Transaction';
export * from './Keys';
export * from './Ledger';
export * from './Storage';
export * from './Block';
export * from './Blockchain';
export * from './Crypto';
export * from './Transaction';
export * from './Keys';
export * from './Ledger';
export * from './Storage';

export type BlockTx = {
tx: Transaction;
Expand Down
29 changes: 12 additions & 17 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,17 @@
# yarn lockfile v1


[email protected].1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/jsbi/-/jsbi-3.1.1.tgz#8ea18b3e08d102c6cc09acaa9a099921d775f4fa"
integrity sha512-+HQESPaV0mRiH614z4JPVPAftcRC2p53x92lySPzUzFwJbJTMpzHz8OYUkcXPN3fOcHUe0NdVcHnCtX/1+eCrA==
[email protected].4:
version "3.1.4"
resolved "https://registry.yarnpkg.com/jsbi/-/jsbi-3.1.4.tgz#9654dd02207a66a4911b4e4bb74265bc2cbc9dd0"
integrity sha512-52QRRFSsi9impURE8ZUbzAMCLjPm4THO7H2fcuIvaaeFTbSysvkodbQQXIVsNgq/ypDbq6dJiuGKL0vZ/i9hUg==

prettier@1.18.2:
version "1.18.2"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-1.18.2.tgz#6823e7c5900017b4bd3acf46fe9ac4b4d7bda9ea"
integrity sha512-OeHeMc0JhFE9idD4ZdtNibzY0+TPHSpSSb9h8FqtP+YnoZZ1sl8Vc9b1sasjfymH3SonAF4QcA2+mzHPhMvIiw==
prettier@2.1.2:
version "2.1.2"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.1.2.tgz#3050700dae2e4c8b67c4c3f666cdb8af405e1ce5"
integrity sha512-16c7K+x4qVlJg9rEbXl7HEGmQyZlG4R9AgP+oHKRMsMsuk8s+ATStlf1NpDqyBI1HpVyfjLOeMhH2LvuNvV5Vg==

tsc@^1.20150623.0:
version "1.20150623.0"
resolved "https://registry.yarnpkg.com/tsc/-/tsc-1.20150623.0.tgz#4ebc3c774e169148cbc768a7342533f082c7a6e5"
integrity sha1-Trw8d04WkUjLx2inNCUz8ILHpuU=

typescript@^3.9.5:
version "3.9.5"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.9.5.tgz#586f0dba300cde8be52dd1ac4f7e1009c1b13f36"
integrity sha512-hSAifV3k+i6lEoCJ2k6R2Z/rp/H3+8sdmcn5NrS3/3kE7+RyZXm9aqvxWqjEXHAd8b0pShatpcdMTvEdvAJltQ==
[email protected]:
version "4.0.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.0.3.tgz#153bbd468ef07725c1df9c77e8b453f8d36abba5"
integrity sha512-tEu6DGxGgRJPb/mVPIZ48e69xCn2yRmCgYmDugAVwmJ6o+0u1RI18eO7E7WBTLYLaEVVOhwQmcdhQHweux/WPg==

0 comments on commit 69dc92e

Please sign in to comment.