Skip to content

Commit

Permalink
Merge pull request ONDC-Official#51 from AbhijeetONDC/main
Browse files Browse the repository at this point in the history
feat: added vlookup signature method
  • Loading branch information
92shreyansh authored Feb 19, 2024
2 parents 7419f79 + 890768e commit 621c33d
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 16 deletions.
Binary file modified utilities/.DS_Store
Binary file not shown.
Binary file added utilities/ondc-crypto-sdk-nodejs/.DS_Store
Binary file not shown.
27 changes: 23 additions & 4 deletions utilities/ondc-crypto-sdk-nodejs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { createAuthorizationHeader } from "ondc-crypto-sdk-nodejs"
const header = await createAuthorizationHeader({
body: { context: {...}, message: {...} },
privateKey: privateKey,
subscriberId: "...", // Subscriber ID that you get after registering to ONDC Network
subscriberId: "abcd.com/ondc", // Subscriber ID that you get after registering to ONDC Network
subscriberUniqueKeyId: "584", // Unique Key Id or uKid that you get after registering to ONDC Network
});
```
Expand All @@ -19,16 +19,35 @@ The method returns a set a unique signature that is ONDC-compliant and can be ve

## Verifying Authorisation Header

For verifying the verification header, you can use the `isSignatureValid` method.
For verifying the verification header, you can use the `isHeaderValid` method.

```javascript
import { isSignatureValid } from "ondc-crypto-sdk-nodejs"
import { isHeaderValid } from "ondc-crypto-sdk-nodejs"

const isValid = await isSignatureValid({
const isValid = await isHeaderValid({
header: header, // The Authorisation header sent by other network participants
body: { context: {...}, message: {...} },
publicKey: publicKey,
});
```

The method returns a boolean value whether the signature is valid or not.

## Create vLookup signature

For creating a signature for the vLookup request, you can use the `createVLookupSignature` method.

```javascript
import { createVLookupSignature } from 'ondc-crypto-sdk-nodejs';

const isValid = await createVLookupSignature({
country: 'IND',
domain: 'ONDC:RET10',
type: 'sellerApp',
city: 'std:080',
subscriber_id: 'subscriberId',
privateKey: 'privateKey',
});
```

The method returns a signature that can be used in the /vlookup call on the registry.
4 changes: 2 additions & 2 deletions utilities/ondc-crypto-sdk-nodejs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion utilities/ondc-crypto-sdk-nodejs/package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "ondc-crypto-sdk-nodejs",
"version": "1.0.1",
"version": "1.0.4",
"description": "A crypto utility for exposing signing and verification functions by ONDC",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "tsc",
"run": "tsc && node lib/index.js",
"format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
"lint": "tslint -p tsconfig.json",
"prepare": "npm run build",
Expand Down
13 changes: 10 additions & 3 deletions utilities/ondc-crypto-sdk-nodejs/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { createAuthorizationHeader, isSignatureValid } from './utility';
import { ICreateAuthorizationHeader, IsSignatureValid } from './types';
import { createAuthorizationHeader, isHeaderValid, createVLookupSignature } from './utility';
import { ICreateAuthorizationHeader, IsHeaderValid, CreateVLookupSignature } from './types';

export { createAuthorizationHeader, isSignatureValid, ICreateAuthorizationHeader, IsSignatureValid };
export {
createAuthorizationHeader,
isHeaderValid,
createVLookupSignature,
ICreateAuthorizationHeader,
IsHeaderValid,
CreateVLookupSignature,
};
17 changes: 13 additions & 4 deletions utilities/ondc-crypto-sdk-nodejs/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,25 @@ export interface IVerifyHeader {
publicKey: string;
}

export interface IsSignatureValid {
export interface IsHeaderValid {
header: string;
body: any;
publicKey: string;
}
export interface ICreateAuthorizationHeader {
message: GenericObject;
body: GenericObject;
privateKey: string;
bapId: string;
bapUniqueKeyId: string;
subscriberId: string;
subscriberUniqueKeyId: string;
expires?: string;
created?: string;
}

export interface CreateVLookupSignature {
country: string;
domain: string;
type: string;
city: string;
subscriber_id: string;
privateKey: string;
}
21 changes: 19 additions & 2 deletions utilities/ondc-crypto-sdk-nodejs/src/utility/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {
ISignMessage,
IVerifyHeader,
IVerifyMessage,
IsSignatureValid,
IsHeaderValid,
CreateVLookupSignature,
} from '../types';

export const createSigningString = async ({ message, created, expires }: ICreateSigningString) => {
Expand Down Expand Up @@ -109,7 +110,7 @@ const verifyHeader = async ({ headerParts, body, publicKey }: IVerifyHeader) =>
return verified;
};

export const isSignatureValid = async ({ header, body, publicKey }: IsSignatureValid) => {
export const isHeaderValid = async ({ header, body, publicKey }: IsHeaderValid) => {
try {
const headerParts = splitAuthHeader(header);
const keyIdSplit = headerParts?.keyId?.split('|');
Expand All @@ -122,3 +123,19 @@ export const isSignatureValid = async ({ header, body, publicKey }: IsSignatureV
return false;
}
};

export const createVLookupSignature = async ({
country,
domain,
type,
city,
subscriber_id,
privateKey,
}: CreateVLookupSignature) => {
const stringToSign = `${country}|${domain}|${type}|${city}|${subscriber_id}`;
const signature = await signMessage({
signingString: stringToSign,
privateKey,
});
return signature;
};

0 comments on commit 621c33d

Please sign in to comment.