Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove dependencies of atob #116

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,8 @@
"typescript": "~3.8.3"
},
"dependencies": {
"@types/atob": "^2.1.2",
"atob": "^2.1.2",
"ethereum-cryptography": "^1.0.1",
"node-fetch": "^2.6.0"
"node-fetch": "^2.6.7"
},
"husky": {
"hooks": {
Expand Down
1 change: 0 additions & 1 deletion src/core/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import '../utils/shim';
import { TokenModule } from '../modules/token';
import { UsersModule } from '../modules/users';
import { UtilsModule } from '../modules/utils';
Expand Down
25 changes: 25 additions & 0 deletions src/utils/codec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Check if global object window is defined
*/
export function isBrowser() {
return typeof window !== 'undefined';
}

/*
* Decode base64 value, returns string
* @Params: string
*/
export function decodeValue(value: string): string {
if (!value) {
return '';
}

const valueToString = value.toString();

if (isBrowser()) {
return atob(valueToString);
}

const buff = Buffer.from(valueToString, 'base64');
return buff.toString('ascii');
}
5 changes: 3 additions & 2 deletions src/utils/parse-didt.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Claim, ParsedDIDToken } from '../types';
import { isDIDTClaim } from './type-guards';
import { createMalformedTokenError } from '../core/sdk-exceptions';
import { decodeValue } from './codec';

interface ParseDIDTokenResult {
raw: [string, string];
Expand All @@ -10,9 +11,9 @@ interface ParseDIDTokenResult {
/**
* Parses a DID Token so that the encoded `claim` is in object form.
*/
export function parseDIDToken(DIDToken: string): ParseDIDTokenResult {
export function parseDIDToken(didToken: string): ParseDIDTokenResult {
try {
const [proof, claim] = JSON.parse(globalThis.atob(DIDToken)) as [string, string];
const [proof, claim] = JSON.parse(decodeValue(didToken)) as [string, string];
const parsedClaim = JSON.parse(claim) as Claim;
if (isDIDTClaim(parsedClaim)) return { raw: [proof, claim], withParsedClaim: [proof, parsedClaim] };
throw new Error();
Expand Down
4 changes: 0 additions & 4 deletions src/utils/shim.ts

This file was deleted.

1 change: 1 addition & 0 deletions test/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export const API_KEY = 'sk_test_123';
export const VALID_DIDT =
'WyIweGUwMjQzNTVlNDI5ZGNhZDM1MTdhZDk5ZWEzNDEwYWJmZDQ1YjBiNjM5OGIwNjY1NGRiYTQxNzljODdlMTYyNzgxNTc1YjA5ODFjNjU4ZjcwMjYwZTQ5MjMwZGE5NDg4YTA0ZDk5NzBlYjM4ZTZmZGRlY2Q2NTA5YTAyN2IwOGI5MWIiLCJ7XCJpYXRcIjoxNTg1MDExMjA0LFwiZXh0XCI6MTkwMDQxMTIwNCxcImlzc1wiOlwiZGlkOmV0aHI6MHhCMmVjOWI2MTY5OTc2MjQ5MWI2NTQyMjc4RTlkRkVDOTA1MGY4MDg5XCIsXCJzdWJcIjpcIjZ0RlhUZlJ4eWt3TUtPT2pTTWJkUHJFTXJwVWwzbTNqOERReWNGcU8ydHc9XCIsXCJhdWRcIjpcImRpZDptYWdpYzpmNTQxNjhlOS05Y2U5LTQ3ZjItODFjOC03Y2IyYTk2YjI2YmFcIixcIm5iZlwiOjE1ODUwMTEyMDQsXCJ0aWRcIjpcIjJkZGY1OTgzLTk4M2ItNDg3ZC1iNDY0LWJjNWUyODNhMDNjNVwiLFwiYWRkXCI6XCIweDkxZmJlNzRiZTZjNmJmZDhkZGRkZDkzMDExYjA1OWI5MjUzZjEwNzg1NjQ5NzM4YmEyMTdlNTFlMGUzZGYxMzgxZDIwZjUyMWEzNjQxZjIzZWI5OWNjYjM0ZTNiYzVkOTYzMzJmZGViYzhlZmE1MGNkYjQxNWU0NTUwMDk1MmNkMWNcIn0iXQ==';

export const DECODED_DIDT = `["0xe024355e429dcad3517ad99ea3410abfd45b0b6398b06654dba4179c87e162781575b0981c658f70260e49230da9488a04d9970eb38e6fddecd6509a027b08b91b","{\\"iat\\":1585011204,\\"ext\\":1900411204,\\"iss\\":\\"did:ethr:0xB2ec9b61699762491b6542278E9dFEC9050f8089\\",\\"sub\\":\\"6tFXTfRxykwMKOOjSMbdPrEMrpUl3m3j8DQycFqO2tw=\\",\\"aud\\":\\"did:magic:f54168e9-9ce9-47f2-81c8-7cb2a96b26ba\\",\\"nbf\\":1585011204,\\"tid\\":\\"2ddf5983-983b-487d-b464-bc5e283a03c5\\",\\"add\\":\\"0x91fbe74be6c6bfd8ddddd93011b059b9253f10785649738ba217e51e0e3df1381d20f521a3641f23eb99ccb34e3bc5d96332fdebc8efa50cdb415e45500952cd1c\\"}"]`;
export const VALID_DIDT_WITH_INVALID_RECOVERY_BIT =
'WyIweGUwMjQzNTVlNDI5ZGNhZDM1MTdhZDk5ZWEzNDEwYWJmZDQ1YjBiNjM5OGIwNjY1NGRiYTQxNzljODdlMTYyNzgxNTc1YjA5ODFjNjU4ZjcwMjYwZTQ5MjMwZGE5NDg4YTA0ZDk5NzBlYjM4ZTZmZGRlY2Q2NTA5YTAyN2IwOGI5MjMiLCJ7XCJpYXRcIjoxNTg1MDExMjA0LFwiZXh0XCI6MTkwMDQxMTIwNCxcImlzc1wiOlwiZGlkOmV0aHI6MHhCMmVjOWI2MTY5OTc2MjQ5MWI2NTQyMjc4RTlkRkVDOTA1MGY4MDg5XCIsXCJzdWJcIjpcIjZ0RlhUZlJ4eWt3TUtPT2pTTWJkUHJFTXJwVWwzbTNqOERReWNGcU8ydHc9XCIsXCJhdWRcIjpcImRpZDptYWdpYzpmNTQxNjhlOS05Y2U5LTQ3ZjItODFjOC03Y2IyYTk2YjI2YmFcIixcIm5iZlwiOjE1ODUwMTEyMDQsXCJ0aWRcIjpcIjJkZGY1OTgzLTk4M2ItNDg3ZC1iNDY0LWJjNWUyODNhMDNjNVwiLFwiYWRkXCI6XCIweDkxZmJlNzRiZTZjNmJmZDhkZGRkZDkzMDExYjA1OWI5MjUzZjEwNzg1NjQ5NzM4YmEyMTdlNTFlMGUzZGYxMzgxZDIwZjUyMWEzNjQxZjIzZWI5OWNjYjM0ZTNiYzVkOTYzMzJmZGViYzhlZmE1MGNkYjQxNWU0NTUwMDk1MmNkMWNcIn0iXQ==';

Expand Down
26 changes: 26 additions & 0 deletions test/spec/utils/codec.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { decodeValue } from '../../../src/utils/codec';
import { DECODED_DIDT, VALID_DIDT } from '../../lib/constants';

test('Decoding', async () => {
const decodedString = decodeValue(VALID_DIDT);

expect(decodedString).toBe(DECODED_DIDT);
});

test('Decoding malformed string', async () => {
const decodedString = decodeValue(`tRollGoat${VALID_DIDT}`);

expect(decodedString).not.toBe(DECODED_DIDT);
});

test('Decoding null value', async () => {
const decodedString = decodeValue(null);

expect(decodedString).toBe('');
});

test('Decoding undefined', async () => {
const decodedString = decodeValue(undefined);

expect(decodedString).toBe('');
});
1 change: 0 additions & 1 deletion test/spec/utils/parse-didt.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import '../../../src/utils/shim';
import { VALID_DIDT, VALID_DIDT_DECODED, INVALID_DIDT_MALFORMED_CLAIM } from '../../lib/constants';
import { createMalformedTokenError } from '../../../src/core/sdk-exceptions';
import { parseDIDToken } from '../../../src/utils/parse-didt';
Expand Down
24 changes: 0 additions & 24 deletions test/spec/utils/shim.spec.ts

This file was deleted.

27 changes: 26 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3913,11 +3913,18 @@ nice-try@^1.0.4:
resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.5.tgz#a3378a7696ce7d223e88fc9b764bd7ef1089e366"
integrity sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==

[email protected], node-fetch@^2.6.0, node-fetch@^2.6.1:
[email protected], node-fetch@^2.6.1:
version "2.6.1"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052"
integrity sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==

node-fetch@^2.6.7:
version "2.7.0"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.7.0.tgz#d0f0fa6e3e2dc1d27efcd8ad99d550bda94d187d"
integrity sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==
dependencies:
whatwg-url "^5.0.0"

node-int64@^0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/node-int64/-/node-int64-0.4.0.tgz#87a9065cdb355d3182d8f94ce11188b825c68a3b"
Expand Down Expand Up @@ -5191,6 +5198,11 @@ tr46@^2.1.0:
dependencies:
punycode "^2.1.1"

tr46@~0.0.3:
version "0.0.3"
resolved "https://registry.yarnpkg.com/tr46/-/tr46-0.0.3.tgz#8184fd347dac9cdc185992f3a6622e14b9d9ab6a"
integrity sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==

trim-newlines@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/trim-newlines/-/trim-newlines-1.0.0.tgz#5887966bb582a4503a41eb524f7d35011815a613"
Expand Down Expand Up @@ -5402,6 +5414,11 @@ walker@^1.0.7:
dependencies:
makeerror "1.0.12"

webidl-conversions@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-3.0.1.tgz#24534275e2a7bc6be7bc86611cc16ae0a5654871"
integrity sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==

webidl-conversions@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/webidl-conversions/-/webidl-conversions-5.0.0.tgz#ae59c8a00b121543a2acc65c0434f57b0fc11aff"
Expand All @@ -5424,6 +5441,14 @@ whatwg-mimetype@^2.3.0:
resolved "https://registry.yarnpkg.com/whatwg-mimetype/-/whatwg-mimetype-2.3.0.tgz#3d4b1e0312d2079879f826aff18dbeeca5960fbf"
integrity sha512-M4yMwr6mAnQz76TbJm914+gPpB/nCwvZbJU28cUD6dR004SAxDLOOSUaB1JDRqLtaOV/vi0IC5lEAGFgrjGv/g==

whatwg-url@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-5.0.0.tgz#966454e8765462e37644d3626f6742ce8b70965d"
integrity sha512-saE57nupxk6v3HY35+jzBwYa0rKSy0XR8JSxZPwgLr7ys0IBzhGviA1/TUGJLmSVqs8pb9AnvICXEuOHLprYTw==
dependencies:
tr46 "~0.0.3"
webidl-conversions "^3.0.0"

whatwg-url@^8.0.0, whatwg-url@^8.5.0:
version "8.7.0"
resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-8.7.0.tgz#656a78e510ff8f3937bc0bcbe9f5c0ac35941b77"
Expand Down