Skip to content

Commit

Permalink
feat: made crypto service as public
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddharth9890 committed Jul 10, 2024
1 parent a5d452b commit fccb672
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 48 deletions.
68 changes: 20 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ yarn add @gateway-dao/sdk

## Gateway Client

To setup the gateway client we will authenticate with a bearer-token and a Api key as follows
To setup the gateway client we will authenticate with a bearer-token,api key and wallet private key as follows

```typescript
import { Gateway } from '@gateway-dao/sdk';
Expand All @@ -52,9 +52,12 @@ const gateway = new Gateway({
apiKey: 'your-api-key',
token: 'your-token',
url: 'https://sandbox.protocol.mygateway.xyz/graphql',
walletPrivateKey: 'your-private-key', // store this in env file!
});
```

**The wallet private key is not send anywhere and is just used to sign messages on behalf of developer/organization using it. This way we minimize signature errors on protocol and provide smoother developer experience**

**Make sure you add token without Bearer as we add Bearer automatically when you make request. Else it will give you Unauthorized error even if your token is correct**
For example

Expand All @@ -66,16 +69,12 @@ const gateway = new Gateway({
token: 'Bearer your-token',
// wrong will not work just use token: 'your-token'
url: 'https://sandbox.protocol.mygateway.xyz/graphql',
walletPrivateKey: 'your-private-key', // store this in env file!
});
```

This library supports Bearer Token along with Api Key. Do not share your authentication token with people you don’t trust. This gives the user control over your account and they will be able to manage PDAs (and more) with it. Use environment variables to keep it safe.

## Error Handling

All the methods throw a validation error if the validation does not match for example:- invalid wallet, invalid uuid for all ids,
Incase of any protocol errors we will throw a custom message which is a string which has all neccessary info regarding error. Make sure to use try catch blocks to handle those.

## Examples

Make sure to add try catch blocks around methods to catch all the validation and protocol based errors.
Expand All @@ -89,6 +88,7 @@ const gateway = new Gateway({
apiKey: 'your-api-key',
token: 'your-token',
url: 'https://sandbox.protocol.mygateway.xyz/graphql',
walletPrivateKey: 'your-private-key', // store this in env file!
});

async function main() {
Expand All @@ -114,15 +114,16 @@ async function main() {
main();
```

### Getting a Organization
### Creating a Organization

````typescript
```typescript
import { Gateway } from '@gateway-dao/sdk';

const gateway = new Gateway({
apiKey: 'your-api-key',
token: 'your-token',
url: 'https://sandbox.protocol.mygateway.xyz/graphql',
walletPrivateKey: 'your-private-key', // store this in env file!
});

async function main() {
Expand All @@ -133,51 +134,22 @@ async function main() {
description: 'test organization',
};
const { createOrganization } =
## Error Handling

All the methods throw a validation error if the validation does not match for example:- invalid wallet, invalid uuid for all ids,
Incase of any protocol errors we will throw a custom message which is a string which has all neccessary info regarding error. Make sure to use try catch blocks to handle those.``

### Create a Data request template

```typescript
import { Gateway } from '@gateway-dao/sdk';
const gateway = new Gateway({
apiKey: 'your-api-key',
token: 'your-token',
url: 'https://sandbox.protocol.mygateway.xyz/graphql',
});
async function main() {
try {
const { createDataRequestTemplate } =
await gateway.dataRequestTemplate.createDataRequestTemplate({
title: 'Create Data Request Template Example',
description: 'Lorem ipsum dolor sit amet.',
dataModels: [
{
id: 'uuid-here',
required: true,
claimValidations: {
type: 'object',
properties: {
gatewayUse: {
type: 'string',
},
},
required: ['gatewayUse'],
},
},
],
});
await gateway.organization.createOrganization(obj);
} catch (error) {
console.log(error); // Can log it for degugging
}
}
main();
````
```

## More examples

We have created a separate repository which have more [examples you can access it here](https://github.com/Gateway-DAO/sdk-scripts-example/)

## Error Handling

All the methods throw a validation error if the validation does not match for example:- invalid wallet, invalid uuid for all ids,
Incase of any protocol errors we will throw a custom message which is a string which has all neccessary info regarding error. Make sure to use try catch blocks to handle those.

## License

Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Config } from './common/types';
import { ValidationService } from './services/validator-service';
import { Activity } from './modules/activity/activity';
import { WalletService } from './services/wallet-service';
import { CryptoService } from './services/crypto-service';
export * from '../gatewaySdk/sources/Gateway/types';
export {
AuthType,
Expand Down Expand Up @@ -49,6 +50,7 @@ export class Gateway {
public proof!: Proof;
public request!: Request;
public user!: User;
public cryptoService: CryptoService;

constructor(config: Config) {
const validationService = new ValidationService();
Expand All @@ -58,6 +60,7 @@ export class Gateway {
walletPrivateKey: this.config.walletPrivateKey,
walletType: this.config.walletType,
});
this.cryptoService = new CryptoService();
this.initializeModules(validationService);
}

Expand Down
18 changes: 18 additions & 0 deletions src/services/crypto-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
ENCRYPTIONSCHEME,
} from '../common/constants';
import { EncryptedAESCipher } from '../common/enums';
import { ethers } from 'ethers';
import { Keypair } from '@solana/web3.js';

export const hashMethod = forge.md.sha256;

Expand Down Expand Up @@ -48,6 +50,22 @@ export class CryptoService {
};
}

/**
* The function generates a new Ethereum wallet using ethers.js.
* @returns An Ethereum wallet created randomly using the ethers.js library.
*/
public generateNewEtherumWallet(): ethers.Wallet {
return ethers.Wallet.createRandom();
}

/**
* The function generates a new Solana key pair using TypeScript.
* @returns A new Solana key pair is being returned.
*/
public generateNewSolanaKeyPair(): Keypair {
return Keypair.generate();
}

/**
* The function `sharedEncryptWithPKI` encrypts data using AES encryption and multiple public keys
* provided by different accessors.
Expand Down
15 changes: 15 additions & 0 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,19 @@ describe('UTILS CRYPTO V3 TESTING', () => {
expect(decryptedData).toBeDefined();
expect(decryptedData).toBe('hello');
});

it('generate new ethers wallet', () => {
const wallet = cryptoService.generateNewEtherumWallet();

expect(wallet).toBeDefined();
expect(wallet.privateKey).toBeDefined();
});

it('generate new solana key pair', () => {
const keyPair = cryptoService.generateNewSolanaKeyPair();

expect(keyPair).toBeDefined();
expect(keyPair.publicKey).toBeDefined();
expect(keyPair.secretKey).toBeDefined();
});
});

0 comments on commit fccb672

Please sign in to comment.