Skip to content

Commit

Permalink
Merge pull request #11 from zama-ai/prepare-release
Browse files Browse the repository at this point in the history
pkg() prepare release
  • Loading branch information
immortal-tofu authored Jun 21, 2023
2 parents bf72d3a + baeddaa commit aaf624b
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 17 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
on:
release:
types: [created]

jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18.x
- run: npm ci
- run: npm test
- run: npm run build
- uses: JS-DevTools/npm-publish@0f451a94170d1699fd50710966d48fb26194d939
with:
token: ${{ secrets.NPM_TOKEN }}
22 changes: 22 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Node.js CI

on:
pull_request:

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [18.x]
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm test
- run: npm run build
- uses: ArtiomTr/jest-coverage-report-action@v2
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# fhEVM SDK
# fhevmjs

This is a library to interact with a blockchain using fhEVM.
7 changes: 7 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,11 @@ module.exports = {
coverageReporters: ['lcov', 'text-summary', 'json'],
transformIgnorePatterns: ['/node_modules/'],
coveragePathIgnorePatterns: [],
coverageThreshold: {
global: {
branches: 80,
functions: 80,
lines: 80,
},
},
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fhevmjs",
"version": "0.0.2",
"version": "0.0.1",
"description": "fhEVM SDK for blockchain using TFHE",
"main": "lib/node.js",
"browser": "lib/web.js",
Expand Down
1 change: 0 additions & 1 deletion src/sdk/encrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export const encrypt16 = (value: number, publicKey: TfheCompactPublicKey): Uint8
};

export const encrypt32 = (value: number, publicKey: TfheCompactPublicKey): Uint8Array => {
// const encrypted = FheUint32.encrypt_with_compact_public_key(value, publicKey);
const uint32Array = new Uint32Array([value]);
const encrypted = CompactFheUint32List.encrypt_with_compact_public_key(uint32Array, publicKey);
return encrypted.serialize();
Expand Down
63 changes: 52 additions & 11 deletions src/sdk/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('token', () => {

const contractAddress = '0x1c786b8ca49D932AFaDCEc00827352B503edf16c';

const instance = await createInstance({
const instance = createInstance({
chainId: 1234,
publicKey: TFHEkeypair.publicKey,
keypairs: {
Expand All @@ -56,14 +56,28 @@ describe('token', () => {
publicKey: TFHEkeypair.publicKey,
});

expect(() => instance.encrypt8(undefined as any)).toThrow();
expect(() => instance.encrypt16(undefined as any)).toThrow();
expect(() => instance.encrypt32(undefined as any)).toThrow();
expect(() => instance.encrypt8(undefined as any)).toThrow('Missing value');
expect(() => instance.encrypt16(undefined as any)).toThrow('Missing value');
expect(() => instance.encrypt32(undefined as any)).toThrow('Missing value');

expect(() => instance.encrypt8('wrong value' as any)).toThrow('Value must be a number');
expect(() => instance.encrypt16('wrong value' as any)).toThrow('Value must be a number');
expect(() => instance.encrypt32('wrong value' as any)).toThrow('Value must be a number');
});

it('controls generateToken', async () => {
const TFHEkeypair = createTFHEKey();
const instance = createInstance({
chainId: 1234,
publicKey: TFHEkeypair.publicKey,
});
await expect(instance.generateToken(undefined as any)).rejects.toThrow('Missing contract address');
await expect(instance.generateToken({ verifyingContract: '' })).rejects.toThrow('Missing contract address');
});

it('save generated tokens', async () => {
it('save generated token', async () => {
const TFHEkeypair = createTFHEKey();
const instance = await createInstance({
const instance = createInstance({
chainId: 1234,
publicKey: TFHEkeypair.publicKey,
});
Expand All @@ -72,20 +86,47 @@ describe('token', () => {

const { token, publicKey } = await instance.generateToken({ verifyingContract: contractAddress });

instance.setTokenSignature(contractAddress, 'signnnn');

expect(instance.hasKeypair(contractAddress)).toBeTruthy();

const kp = instance.getTokenSignature(contractAddress);
expect(kp!.publicKey).toBe(publicKey);
});

it("don't export keys without signature", async () => {
const TFHEkeypair = createTFHEKey();
const instance = createInstance({
chainId: 1234,
publicKey: TFHEkeypair.publicKey,
});

const contractAddress = '0x1c786b8ca49D932AFaDCEc00827352B503edf16c';

const { token, publicKey } = await instance.generateToken({ verifyingContract: contractAddress });
const keypairs = instance.serializeKeypairs();
expect(keypairs[contractAddress]).toBeUndefined();
const keypair = instance.getTokenSignature(contractAddress);
expect(keypair).toBeNull();
expect(instance.hasKeypair(contractAddress)).toBeFalsy();
});

it('decrypts data', async () => {
const TFHEkeypair = createTFHEKey();
const instance = createInstance({
chainId: 1234,
publicKey: TFHEkeypair.publicKey,
});

const contractAddress = '0x1c786b8ca49D932AFaDCEc00827352B503edf16c';

const { token, publicKey } = await instance.generateToken({ verifyingContract: contractAddress });

instance.setTokenSignature(contractAddress, 'signnnn');

const kp = instance.getTokenSignature(contractAddress);
expect(kp!.publicKey).toBe(publicKey);

const keypairs2 = instance.serializeKeypairs();
expect(keypairs2[contractAddress].publicKey).toBe(toHexString(publicKey));
const keypair2 = instance.getTokenSignature(contractAddress);
expect(keypair2?.publicKey).toBe(publicKey);

const value = 8238290348;
const ciphertext = sodium.crypto_box_seal(numberToBytes(value), publicKey, 'hex');
const cleartext = await instance.decrypt(contractAddress, ciphertext);
Expand Down
6 changes: 4 additions & 2 deletions src/sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,24 @@ export const createInstance = (params: FhevmInstanceParams): FhevmInstance => {
// Parameters
encrypt8(value) {
if (!value) throw new Error('Missing value');
if (typeof value !== 'number') throw new Error('Value must be a number');
return encrypt8(value, publicKey);
},
encrypt16(value) {
if (!value) throw new Error('Missing value');
if (typeof value !== 'number') throw new Error('Value must be a number');
return encrypt16(value, publicKey);
},

encrypt32(value) {
if (!value) throw new Error('Missing value');

if (typeof value !== 'number') throw new Error('Value must be a number');
return encrypt32(value, publicKey);
},

// Reencryption
async generateToken(options) {
if (!options.verifyingContract) throw new Error('Missing contract address');
if (!options || !options.verifyingContract) throw new Error('Missing contract address');
if (!isAddress(options.verifyingContract)) throw new Error('Invalid contract address');
let kp;
if (!options.force && contractKeypairs[options.verifyingContract]) {
Expand Down
1 change: 0 additions & 1 deletion src/web.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './sdk';
export * from './tfhe';
export * from './init';

0 comments on commit aaf624b

Please sign in to comment.