diff --git a/README.md b/README.md index dcf25c7..f83306f 100644 --- a/README.md +++ b/README.md @@ -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'; @@ -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 @@ -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. @@ -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() { @@ -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() { @@ -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 diff --git a/src/index.ts b/src/index.ts index 9b445a3..0ecf0d4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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, @@ -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(); @@ -58,6 +60,7 @@ export class Gateway { walletPrivateKey: this.config.walletPrivateKey, walletType: this.config.walletType, }); + this.cryptoService = new CryptoService(); this.initializeModules(validationService); } diff --git a/src/services/crypto-service.ts b/src/services/crypto-service.ts index f35c549..06c951b 100644 --- a/src/services/crypto-service.ts +++ b/src/services/crypto-service.ts @@ -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; @@ -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. diff --git a/test/utils.test.ts b/test/utils.test.ts index 1c2d313..14b93ef 100644 --- a/test/utils.test.ts +++ b/test/utils.test.ts @@ -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(); + }); });