Skip to content

Commit

Permalink
test: enforce linters and tests
Browse files Browse the repository at this point in the history
- adds tests to the core library
- adds ci for pull requests
  • Loading branch information
PhearZero committed Apr 15, 2024
1 parent 9203910 commit 102a55c
Show file tree
Hide file tree
Showing 29 changed files with 4,241 additions and 411 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: CI
on: [pull_request]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [ 18.x, 20.x ]
steps:
- name: Checkout
uses: actions/checkout@master
- name: Use Node.js
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Install Dependencies
run: npm install
- name: Run Build
run: npm run build
- name: Lint Codebase
run: npm run lint
- name: Unit Tests with Coverage
run: npm run test:cov
3 changes: 2 additions & 1 deletion clients/liquid-auth-client-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"dev": "tsc --watch",
"build": "tsc",
"preinstall": "npm run build",
"test": "tsc && c8 node --test ./tests/connect.test.js"
"test": "tsc && node --test ./tests/connect.spec.js",
"test:cov": "tsc && c8 node --test ./tests/connect.spec.js"
},
"author": "",
"license": "MIT",
Expand Down
10 changes: 5 additions & 5 deletions clients/liquid-auth-client-js/src/connect.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DEFAULT_FETCH_OPTIONS } from './constants.js';
import type { Account } from 'algosdk';
import type { SignKeyPair } from 'tweetnacl';
import { sign } from 'tweetnacl';
import nacl from 'tweetnacl';
import { toBase64URL, encodeAddress } from '@liquid/core/encoding';

export class Message {
Expand Down Expand Up @@ -58,9 +58,9 @@ export class Message {
// Seed or Secret Key
if (key instanceof Uint8Array) {
if (key.length === 32) {
keyPair = sign.keyPair.fromSeed(key);
keyPair = nacl.sign.keyPair.fromSeed(key);
} else if (key.length === 64) {
keyPair = sign.keyPair.fromSecretKey(key);
keyPair = nacl.sign.keyPair.fromSecretKey(key);
} else {
throw new TypeError('Invalid seed or secret key');
}
Expand All @@ -71,7 +71,7 @@ export class Message {
typeof (key as Account).addr !== 'undefined' &&
typeof (key as Account).addr === 'string'
) {
keyPair = sign.keyPair.fromSecretKey((key as Account).sk);
keyPair = nacl.sign.keyPair.fromSecretKey((key as Account).sk);
}

// NACL
Expand All @@ -86,7 +86,7 @@ export class Message {
throw new TypeError('Invalid key');
}
this.signature = toBase64URL(
sign.detached(encoder.encode(this.challenge), keyPair.secretKey),
nacl.sign.detached(encoder.encode(this.challenge), keyPair.secretKey),
);
this.wallet = encodeAddress(keyPair.publicKey);
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
[
""
]
]
2 changes: 1 addition & 1 deletion clients/liquid-auth-client-js/tests/connect.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import algosdk from 'algosdk';
import nacl from "tweetnacl";

import { Message } from '../lib/connect.js';
import { fromBase64Url } from "../lib/encoding.js";
import { fromBase64Url } from "@liquid/core";
const encoder = new TextEncoder;
test("create instance", async () => {
const msg = new Message("hello", "1234", 1234);
Expand Down
2 changes: 1 addition & 1 deletion clients/liquid-auth-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"dev": "tsc --watch",
"build": "tsc",
"preinstall": "npm run build",
"test": "tsc && c8 node --test ./tests/connect.test.js"
"test": "tsc && c8 -r html node --test ./tests/encoding.spec.js"
},
"author": "",
"license": "MIT",
Expand Down
19 changes: 6 additions & 13 deletions clients/liquid-auth-core/src/encoding.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import nacl from "tweetnacl";
import { decodeAsBytes, encode } from "./hi-base32.js";
import { decodeAsBytes, encodeBytes } from "./hi-base32.js";
import { createMethod } from "./sha512.js";
const sha512_256 = createMethod(256);
const chars =
Expand All @@ -11,6 +11,8 @@ const ALGORAND_ADDRESS_LENGTH = 58;
const HASH_BYTES_LENGTH = 32;
export const MALFORMED_ADDRESS_ERROR_MSG = "Malformed address";
export const ALGORAND_ADDRESS_BAD_CHECKSUM_ERROR_MSG = "Bad checksum";
export const INVALID_BASE64URL_INPUT = "Invalid base64url input";

/**
* Bytes to Base64URL
* @param {Uint8Array| ArrayBuffer} arr Bytes to convert to URL safe Base64
Expand All @@ -19,7 +21,6 @@ export function toBase64URL(arr: Uint8Array | ArrayBuffer): string {
const bytes = arr instanceof Uint8Array ? arr : new Uint8Array(arr);
const len = bytes.length;
let base64 = "";

for (let i = 0; i < len; i += 3) {
base64 += chars[bytes[i] >> 2];
base64 += chars[((bytes[i] & 3) << 4) | (bytes[i + 1] >> 4)];
Expand All @@ -42,9 +43,10 @@ export function toBase64URL(arr: Uint8Array | ArrayBuffer): string {
*/
export function fromBase64Url(base64url: string): Uint8Array {
if (typeof base64url !== "string") {
throw new TypeError("Must be string!");
throw new Error(INVALID_BASE64URL_INPUT);
}
return new Uint8Array(
// TODO: Cross-platform solution since atob is deprecated in Node
atob(base64url.replace(/-/g, "+").replace(/_/g, "/").replace(/\s/g, ""))
.split("")
.map((c) => c.charCodeAt(0)),
Expand Down Expand Up @@ -72,7 +74,7 @@ export function encodeAddress(address: Uint8Array) {
nacl.sign.publicKeyLength - ALGORAND_CHECKSUM_BYTE_LENGTH,
nacl.sign.publicKeyLength,
);
const addr = encode(concatArrays(address, checksum));
const addr = encodeBytes(concatArrays(address, checksum));

return addr.toString().slice(0, ALGORAND_ADDRESS_LENGTH); // removing the extra '===='
}
Expand Down Expand Up @@ -124,12 +126,3 @@ export function decodeAddress(address: string): Uint8Array {

return pk;
}

export function base64ToUint8Array(encoded) {
return new Uint8Array(
// TODO: Cross-platform solution since atob is deprecated in Node
atob(encoded)
.split("")
.map((c) => c.charCodeAt(0)),
);
}
Loading

0 comments on commit 102a55c

Please sign in to comment.