Skip to content

Commit

Permalink
Merge pull request #2 from DIMO-Network/web3-support
Browse files Browse the repository at this point in the history
Adding Attestation API Support + Set up for web3
  • Loading branch information
jamesliupenn authored Jul 30, 2024
2 parents 6d61129 + f493c53 commit 0f85139
Show file tree
Hide file tree
Showing 11 changed files with 823 additions and 27 deletions.
45 changes: 26 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ await dimo.user.get(auth);

// Pass the auth object to a protected endpoint with body parameters
await dimo.tokenexchange.exchange({
...auth,
privileges: [4],
tokenId: <vehicle_token_id>
...auth,
privileges: [4],
tokenId: <vehicle_token_id>
});

```
Expand All @@ -162,7 +162,7 @@ const authHeader = await dimo.authenticate();
### Querying the DIMO REST API
The SDK supports async await and your typical JS Promises. HTTP operations can be utilized in either ways:

```js
```ts
// Async Await
async function getAllDeviceMakes() {
try {
Expand All @@ -186,17 +186,17 @@ dimo.devicedefinitions.listDeviceMakes().then((result) => {
#### Query Parameters

For query parameters, simply feed in an input that matches with the expected query parameters:
```js
```ts
dimo.devicedefinitions.getByMMY({
make: '<vehicle_make>',
model: '<vehicle_model',
year: 2021
make: '<vehicle_make>',
model: '<vehicle_model',
year: 2021
});
```
#### Path Parameters

For path parameters, simply feed in an input that matches with the expected path parameters:
```js
```ts
dimo.devicedefinitions.getById({ id: '26G4j1YDKZhFeCsn12MAlyU3Y2H' })
```

Expand All @@ -210,16 +210,23 @@ For the end users of your application, they will need to share their vehicle per

Typically, any endpoints that uses a NFT `tokenId` in path parameters will require privilege tokens. You can get the privilege token and pipe it through to corresponding endpoints like this:

```js
```ts
const privToken = await dimo.tokenexchange.exchange({
...auth,
privileges: [1],
tokenId: <vehicle_token_id>
...auth,
privileges: [1, 5],
tokenId: <vehicle_token_id>
});

// Vehicle Status uses privId 1
await dimo.devicedata.getVehicleStatus({
...privToken,
tokenId: <vehicle_token_id>
...privToken,
tokenId: <vehicle_token_id>
});

// VIN Vehicle Credentials uses privId 5
await dimo.attestation.createVinVC({
...privToken,
tokenId: <vehicle_token_id>
});
```

Expand All @@ -230,11 +237,11 @@ The SDK accepts any type of valid custom GraphQL queries, but we've also include
#### Authentication for GraphQL API
The GraphQL entry points are designed almost identical to the REST API entry points. For any GraphQL API that requires auth headers (Telemetry API for example), you can use the same pattern as you would in the REST protected endpoints.

```js
```ts
const privToken = await dimo.tokenexchange.exchange({
...auth,
privileges: [1, 3, 4],
tokenId: <vehicle_token_id>
...auth,
privileges: [1, 3, 4],
tokenId: <vehicle_token_id>
});

const tele = await dimo.telemetry.query({
Expand Down
16 changes: 16 additions & 0 deletions src/api/resources/Attestation/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Resource } from '../../Resource';
import { DimoEnvironment } from '../../../environments';

export class Attestation extends Resource {

constructor(api: any, env: keyof typeof DimoEnvironment) {
super(api, 'Attestation', env);
this.setResource({
createVinVC: {
method: 'GET',
path: '/v1/vc/vin/:tokenId',
auth: 'privilege_token'
}
})
}
}
3 changes: 2 additions & 1 deletion src/api/resources/DimoRestResources.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Attestation } from './Attestation';
import { Auth } from './Auth';
import { DeviceData } from './DeviceData';
import { DeviceDefinitions } from './DeviceDefinitions';
Expand All @@ -9,4 +10,4 @@ import { User } from './User';
import { Valuations } from './Valuations';
import { VehicleSignalDecoding } from './VehicleSignalDecoding';

export { Auth, DeviceData, DeviceDefinitions, Devices, Events, TokenExchange, Trips, User, Valuations, VehicleSignalDecoding };
export { Attestation, Auth, DeviceData, DeviceDefinitions, Devices, Events, TokenExchange, Trips, User, Valuations, VehicleSignalDecoding };
9 changes: 7 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
export const DimoConstants = {
Production: {
'NFT_address': '0xbA5738a18d83D41847dfFbDC6101d37C69c9B0cF',
'RPC_provider': 'https://eth.llamarpc.com'
'RPC_provider': 'https://eth.llamarpc.com',
'DLX_address': '0x9A9D2E717bB005B240094ba761Ff074d392C7C85',
'DCX_address': '0x7186F9aC35d24c9a4cf1E58a797c04DF1b334322',
'Vehicle_address': '0xba5738a18d83d41847dffbdc6101d37c69c9b0cf'

},
Dev: {
'NFT_address': '0x45fbCD3ef7361d156e8b16F5538AE36DEdf61Da8',
'RPC_provider': 'https://eth.llamarpc.com'
'RPC_provider': 'https://eth.llamarpc.com',
'DLX_address': ''
}
} as const;

Expand Down
6 changes: 5 additions & 1 deletion src/dimo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const dimo = new DIMO(PROD);
const devDimo = new DIMO(DEV);

describe('Production Environment', () => {
test('Production resources are initialized with the correct environment', () => {
test('Production resources are initialized with the correct environment', () => {
expect(dimo.attestation.env).toBe(PROD);
expect(dimo.auth.env).toBe(PROD);
expect(dimo.devicedata.env).toBe(PROD);
expect(dimo.devicedefinitions.env).toBe(PROD);
Expand All @@ -23,6 +24,7 @@ describe('Production Environment', () => {
});

test('Production API endpoints are defined', () => {
expect(dimo.attestation.api).toBeDefined;
expect(dimo.auth.api).toBeDefined;
expect(dimo.devicedata.api).toBeDefined;
expect(dimo.devicedefinitions.api).toBeDefined;
Expand All @@ -40,6 +42,7 @@ describe('Production Environment', () => {

describe('Dev Environment', () => {
test('Dev resources are initialized with the correct environment', () => {
expect(devDimo.attestation.env).toBe(DEV);
expect(devDimo.auth.env).toBe(DEV);
expect(devDimo.devicedata.env).toBe(DEV);
expect(devDimo.devicedefinitions.env).toBe(DEV);
Expand All @@ -55,6 +58,7 @@ describe('Dev Environment', () => {
});

test('Dev API endpoints are defined', () => {
expect(devDimo.attestation.api).toBeDefined;
expect(devDimo.auth.api).toBeDefined;
expect(devDimo.devicedata.api).toBeDefined;
expect(devDimo.devicedefinitions.api).toBeDefined;
Expand Down
5 changes: 4 additions & 1 deletion src/dimo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
Telemetry
} from './graphql/resources/DimoGraphqlResources';

import {
import {
Attestation,
Auth,
DeviceData,
DeviceDefinitions,
Expand All @@ -22,6 +23,7 @@ import {
import { Stream } from './streamr';

export class DIMO {
public attestation: Attestation;
public auth: Auth;
public devicedata: DeviceData;
public devicedefinitions: DeviceDefinitions;
Expand All @@ -42,6 +44,7 @@ export class DIMO {
/**
* Set up all REST Endpoints
*/
this.attestation = new Attestation(DimoEnvironment[env].Attestation, env);
this.auth = new Auth(DimoEnvironment[env].Auth, env);
this.devicedata = new DeviceData(DimoEnvironment[env].DeviceData, env);
this.devicedefinitions = new DeviceDefinitions(DimoEnvironment[env].DeviceDefinitions, env);
Expand Down
2 changes: 2 additions & 0 deletions src/environments/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const DimoEnvironment = {
Production: {
'Attestation': 'https://attestation-api.dimo.zone',
'Auth': 'https://auth.dimo.zone',
'Identity': 'https://identity-api.dimo.zone/query',
'Devices': 'https://devices-api.dimo.zone',
Expand All @@ -14,6 +15,7 @@ export const DimoEnvironment = {
'VehicleSignalDecoding': 'https://vehicle-signal-decoding.dimo.zone'
},
Dev: {
'Attestation': 'https://attestation-api.dev.dimo.zone',
'Auth': 'https://auth.dev.dimo.zone',
'Identity': 'https://identity-api.dev.dimo.zone/query',
'Devices': 'https://devices-api.dev.dimo.zone',
Expand Down
5 changes: 2 additions & 3 deletions src/streamr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export const Stream = async ({ streamId, clientId, privateKey, log }: StreamOpti
const setupStream = async () => {
try {
const stream = await client.getStream(streamId);

await client.subscribe({
streamId,
erc1271Contract: clientId,
Expand All @@ -41,8 +40,8 @@ export const Stream = async ({ streamId, clientId, privateKey, log }: StreamOpti
};
setupStream();

return () => {
client.unsubscribe(streamId);
return async () => {
await client.unsubscribe(streamId);
}
}).pipe(
catchError(error => {
Expand Down
Empty file.
Loading

0 comments on commit 0f85139

Please sign in to comment.