Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

Do not require padding on schema #63

Merged
merged 3 commits into from
Mar 22, 2024
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: 4 additions & 0 deletions packages/wallet-connectors/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Fixed serialization of parameters in `requestVerifiablePresentation` and parsing of the result of the request.

### Changed

- `schemaAsBuffer` and thereby `typeSchemaFromBase64` no longer requires a base64 encoded schema to be padded.

## [0.5.0] - 2024-03-13

### Added
Expand Down
18 changes: 18 additions & 0 deletions packages/wallet-connectors/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { Config } from '@jest/types';

const esModules = ['@concordium/web-sdk', '@noble/ed25519'].join('|');

const config: Config.InitialOptions = {
preset: 'ts-jest/presets/js-with-ts-esm',
transformIgnorePatterns: [`node_modules/(?!${esModules})`],
transform: {
'^.+\\.tsx?$': [
'ts-jest',
{
tsconfig: 'tsconfig.test.json',
},
],
},
};

export default config;
8 changes: 7 additions & 1 deletion packages/wallet-connectors/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"url": "https://github.com/Concordium/concordium-dapp-libraries/tree/main/packages/wallet-connectors"
},
"scripts": {
"build": "tsc"
"build": "tsc",
"test": "jest"
},
"dependencies": {
"@concordium/browser-wallet-api-helpers": "^3.0.0",
Expand All @@ -35,9 +36,14 @@
"@concordium/web-sdk": "7.x"
},
"devDependencies": {
"@jest/types": "^29.6.3",
"@tsconfig/recommended": "^1.0.1",
"@types/jest": "^29.5.12",
"@walletconnect/types": "^2.1.4",
"jest": "^29.7.0",
"prettier": "2.8.1",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.2",
"typescript": "^5.2.2"
}
}
15 changes: 11 additions & 4 deletions packages/wallet-connectors/src/WalletConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,18 @@ export function typeSchema(schema: Buffer): TypeSchema {
};
}

function schemaAsBuffer(schemaBase64: string) {
export function schemaAsBuffer(schemaBase64: string) {
let unpaddedLen = schemaBase64.length;
if (schemaBase64.charAt(unpaddedLen - 1) === '=') {
unpaddedLen--;

if (schemaBase64.charAt(unpaddedLen - 1) === '=') {
unpaddedLen--;
}
}
const res = toBuffer(schemaBase64, 'base64');
// Check round-trip. This requires the provided schema to be properly padded.
if (res.toString('base64') !== schemaBase64) {
throw new Error(`provided schema '${schemaBase64}' is not valid base64`);
if (unpaddedLen !== Math.ceil((4 * res.length) / 3)) {
throw new Error(`The provided schema '${schemaBase64}' is not valid base64`);
}
return res;
}
Expand Down
120 changes: 120 additions & 0 deletions packages/wallet-connectors/test/WalletConnection.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import { schemaAsBuffer } from '../src/WalletConnection';

// Tests have been taken from https://github.com/bcgit/bc-java/blob/main/core/src/test/java/org/bouncycastle/util/encoders/test/Base64Test.java.

test('Test Vector 1', () => {
schemaAsBuffer('');
});

test('Test Vector 2', () => {
schemaAsBuffer('Zg==');
});

test('Test Vector 3', () => {
schemaAsBuffer('Zm8=');
});

test('Test Vector 4', () => {
schemaAsBuffer('Zm9v');
});

test('Test Vector 5', () => {
schemaAsBuffer('Zm9vYg==');
});

test('Test Vector 6', () => {
schemaAsBuffer('Zm9vYmE=');
});

test('Test Vector 7', () => {
schemaAsBuffer('Zm9vYmFy');
});

test('Test Vector 8', () => {
const schemaBuffer = schemaAsBuffer('mO4TyLWG7vjFWdKT8IJcVbZ/jwc=');
expect(schemaBuffer.toString('hex')).toEqual('98ee13c8b586eef8c559d293f0825c55b67f8f07');
});

test('Test Vector 9', () => {
const schemaBuffer = schemaAsBuffer('F4I4p8Vf/mS+Kxvri3FPoMcqmJ1f');
expect(schemaBuffer.toString('hex')).toEqual('178238a7c55ffe64be2b1beb8b714fa0c72a989d5f');
});

test('Test Vector 10', () => {
const schemaBuffer = schemaAsBuffer('UJmEdJYodqHJmd7Rtv6/OP29/jUEFw==');
expect(schemaBuffer.toString('hex')).toEqual('50998474962876a1c999ded1b6febf38fdbdfe350417');
});

test('Test error on invalid input 1', () => {
const invalid = '%O4TyLWG7vjFWdKT8IJcVbZ/jwc=';
expect(() => schemaAsBuffer(invalid)).toThrow();
});

test('Test error on invalid input 2', () => {
const invalid = 'F%I4p8Vf/mS+Kxvri3FPoMcqmJ1f';
expect(() => schemaAsBuffer(invalid)).toThrow();
});

test('Test error on invalid input 3', () => {
const invalid = 'UJ%EdJYodqHJmd7Rtv6/OP29/jUEFw==';
expect(() => schemaAsBuffer(invalid)).toThrow();
});

test('Test error on invalid input 4', () => {
const invalid = 'mO4%yLWG7vjFWdKT8IJcVbZ/jwc=';
expect(() => schemaAsBuffer(invalid)).toThrow();
});

test('Test error on invalid input 5', () => {
const invalid = 'UJmEdJYodqHJmd7Rtv6/OP29/jUEF%==';
expect(() => schemaAsBuffer(invalid)).toThrow();
});

test('Test error on invalid input 6', () => {
const invalid = 'mO4TyLWG7vjFWdKT8IJcVbZ/jw%=';
expect(() => schemaAsBuffer(invalid)).toThrow();
});

test('Test error on invalid input 7', () => {
const invalid = 'F4I4p8Vf/mS+Kxvri3FPoMcqmJ1%';
expect(() => schemaAsBuffer(invalid)).toThrow();
});

test('Test error on invalid input 8', () => {
const invalid = 'UJmEdJYodqHJmd7Rtv6/OP29/jUE%c==';
expect(() => schemaAsBuffer(invalid)).toThrow();
});

test('Test error on invalid input 9', () => {
const invalid = 'mO4TyLWG7vjFWdKT8IJcVbZ/j%c=';
expect(() => schemaAsBuffer(invalid)).toThrow();
});

test('Test error on invalid input 10', () => {
const invalid = 'F4I4p8Vf/mS+Kxvri3FPoMcqmJ%1';
expect(() => schemaAsBuffer(invalid)).toThrow();
});

test('Test error on invalid input 11', () => {
const invalid = 'UJmEdJYodqHJmd7Rtv6/OP29/jU%Fc==';
expect(() => schemaAsBuffer(invalid)).toThrow();
});

test('Test error on invalid input 12', () => {
const invalid = 'mO4TyLWG7vjFWdKT8IJcVbZ/%wc=';
expect(() => schemaAsBuffer(invalid)).toThrow();
});

test('Test error on invalid input 13', () => {
const invalid = 'F4I4p8Vf/mS+Kxvri3FPoMcqm%1c';
expect(() => schemaAsBuffer(invalid)).toThrow();
});
test('Test error on invalid input 14', () => {
const invalid = 'UJmEdJYodqHJmd7Rtv6/OP29/jUEFw=1';
expect(() => schemaAsBuffer(invalid)).toThrow();
});

test('Test error on invalid input 15', () => {
const invalid = 'M';
expect(() => schemaAsBuffer(invalid)).toThrow();
});
7 changes: 7 additions & 0 deletions packages/wallet-connectors/tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"target": "ES2020",
"allowJs": true
}
}
Loading