Skip to content

Commit

Permalink
chore(codecs): add package and reuse across sdk's
Browse files Browse the repository at this point in the history
chore(codecs): update description

chore(codecs): add readme and license

chore(codecs): remove erroneus package linking

refactor(issuer-sdk): use `codecs` library

refactor(client-sdk): use `codecs` library

chore(codecs): bootstrap library
  • Loading branch information
ditoglez committed Dec 14, 2024
1 parent bbff067 commit ac4a2b0
Show file tree
Hide file tree
Showing 16 changed files with 265 additions and 225 deletions.
21 changes: 21 additions & 0 deletions packages/codecs/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 idOS Network

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions packages/codecs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# idOS codecs

A collection of codecs used by the idOS SDK.
10 changes: 10 additions & 0 deletions packages/codecs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export { decode as base64Decode } from "@stablelib/base64";
export { encode as base64Encode } from "@stablelib/base64";
export { writeUint16BE as binaryWriteUint16BE } from "@stablelib/binary";
export { concat as bytesConcat } from "@stablelib/bytes";
export { decode as hexDecode } from "@stablelib/hex";
export { encode as hexEncode } from "@stablelib/hex";
export { hash as sha256Hash } from "@stablelib/sha256";
export { decode as utf8Decode } from "@stablelib/utf8";
export { encode as utf8Encode } from "@stablelib/utf8";
export { serialize as borshSerialize } from "borsh";
19 changes: 19 additions & 0 deletions packages/codecs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "@idos-network/codecs",
"version": "0.0.1",
"description": "A collection of codecs used by the idOS SDK.",
"homepage": "https://idos.network",
"repository": "https://github.com/idos-network/idos-sdk-js",
"license": "MIT",
"type": "module",
"main": "index.ts",
"dependencies": {
"@stablelib/base64": "^1.0.1",
"@stablelib/binary": "^1.0.1",
"@stablelib/bytes": "^1.0.1",
"@stablelib/hex": "^2.0.0",
"@stablelib/sha256": "^1.0.1",
"@stablelib/utf8": "^1.0.1",
"borsh": "^1.0.0"
}
}
20 changes: 20 additions & 0 deletions packages/codecs/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"target": "ESNext",
"composite": true,
"lib": ["ESNext"],
"module": "ESNext",
"skipLibCheck": true,
/* Bundler mode */
"moduleResolution": "bundler",
"resolveJsonModule": true,
"isolatedModules": true,
/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"baseUrl": "."
},
"include": ["./src/**/*.ts"]
}
8 changes: 1 addition & 7 deletions packages/idos-sdk-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,9 @@
"dependencies": {
"@digitalbazaar/ed25519-signature-2020": "^5.2.0",
"@digitalbazaar/ed25519-verification-key-2020": "^4.1.0",
"@idos-network/codecs": "workspace:*",
"@digitalbazaar/vc": "^6.0.2",
"@kwilteam/kwil-js": "0.7.1",
"@stablelib/base64": "^1.0.1",
"@stablelib/binary": "^1.0.1",
"@stablelib/bytes": "^1.0.1",
"@stablelib/hex": "^2.0.0",
"@stablelib/sha256": "^1.0.1",
"@stablelib/utf8": "^1.0.1",
"borsh": "^1.0.0",
"es-toolkit": "^1.23.0",
"bs58": "^6.0.0",
"jsonld": "^8.3.1",
Expand Down
24 changes: 13 additions & 11 deletions packages/idos-sdk-js/src/lib/auth.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import {
base64Decode,
binaryWriteUint16BE,
borshSerialize,
bytesConcat,
utf8Decode,
} from "@idos-network/codecs";
import type { EthSigner } from "@kwilteam/kwil-js/dist/core/builders";
import type { SignMessageParams, SignedMessage, Wallet } from "@near-wallet-selector/core";
import * as Base64Codec from "@stablelib/base64";
import * as BinaryCodec from "@stablelib/binary";
import * as BytesCodec from "@stablelib/bytes";
import * as Utf8Codec from "@stablelib/utf8";
import * as BorshCodec from "borsh";
import type { Signer } from "ethers";

import type { EthSigner } from "@kwilteam/kwil-js/dist/core/builders";
import type { Store } from "../../../idos-store";
import type { KwilWrapper } from "./kwil-wrapper";
import { Nonce } from "./nonce";
Expand Down Expand Up @@ -149,7 +151,7 @@ export class Auth {

const signer = async (message: string | Uint8Array): Promise<Uint8Array> => {
// biome-ignore lint/style/noParameterAssign: we're narrowing the type on purpose.
if (typeof message !== "string") message = Utf8Codec.decode(message);
if (typeof message !== "string") message = utf8Decode(message);
if (!wallet.signMessage) throw new Error("Only wallets with signMessage are supported.");

const nonceSuggestion = Buffer.from(new Nonce(32).bytes);
Expand Down Expand Up @@ -188,12 +190,12 @@ export class Auth {
callbackUrl,
};

const nep413BorshPayload = BorshCodec.serialize(nep413BorschSchema, nep413BorshParams);
const nep413BorshPayload = borshSerialize(nep413BorschSchema, nep413BorshParams);

return BytesCodec.concat(
BinaryCodec.writeUint16BE(nep413BorshPayload.length),
return bytesConcat(
binaryWriteUint16BE(nep413BorshPayload.length),
nep413BorshPayload,
Base64Codec.decode(signature),
base64Decode(signature),
);
};

Expand Down
20 changes: 9 additions & 11 deletions packages/idos-sdk-js/src/lib/data.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { base64Decode, base64Encode, hexEncode, utf8Encode } from "@idos-network/codecs";
import type { idOSCredential } from "@idos-network/idos-sdk-types";
import * as Base64Codec from "@stablelib/base64";
import * as HexCodec from "@stablelib/hex";
import * as Utf8Codec from "@stablelib/utf8";
import nacl from "tweetnacl";
import type { Enclave } from "./enclave";
import type { KwilWrapper } from "./kwil-wrapper";
Expand Down Expand Up @@ -81,7 +79,7 @@ export class Data {
let receiverPublicKey: string | undefined;

if (tableName === "credentials") {
receiverPublicKey = receiverPublicKey ?? Base64Codec.encode(await this.enclave.ready());
receiverPublicKey = receiverPublicKey ?? base64Encode(await this.enclave.ready());
for (const record of records) {
Object.assign(
record,
Expand Down Expand Up @@ -131,7 +129,7 @@ export class Data {
}

if (tableName === "credentials") {
receiverPublicKey ??= Base64Codec.encode(await this.enclave.ready());
receiverPublicKey ??= base64Encode(await this.enclave.ready());
Object.assign(
record,
await this.#buildInsertableIDOSCredential(
Expand Down Expand Up @@ -266,7 +264,7 @@ export class Data {
const record: any = recordLike;

if (tableName === "credentials") {
receiverPublicKey ??= Base64Codec.encode(await this.enclave.ready());
receiverPublicKey ??= base64Encode(await this.enclave.ready());
Object.assign(
record,
await this.#buildInsertableIDOSCredential(
Expand Down Expand Up @@ -368,7 +366,7 @@ export class Data {

const content = await this.enclave.encrypt(plaintextContent, receiverEncryptionPublicKey);
const publicNotesSignature = nacl.sign.detached(
Utf8Codec.encode(publicNotes),
utf8Encode(publicNotes),
issuerAuthenticationKeyPair.secretKey,
);

Expand All @@ -377,16 +375,16 @@ export class Data {
content,

public_notes: publicNotes,
public_notes_signature: Base64Codec.encode(publicNotesSignature),
public_notes_signature: base64Encode(publicNotesSignature),

broader_signature: Base64Codec.encode(
broader_signature: base64Encode(
nacl.sign.detached(
Uint8Array.from([...publicNotesSignature, ...Base64Codec.decode(content)]),
Uint8Array.from([...publicNotesSignature, ...base64Decode(content)]),
issuerAuthenticationKeyPair.secretKey,
),
),

issuer_auth_public_key: HexCodec.encode(issuerAuthenticationKeyPair.publicKey, true),
issuer_auth_public_key: hexEncode(issuerAuthenticationKeyPair.publicKey, true),
encryption_public_key: isPresent(this.enclave.auth.currentUser.currentUserPublicKey),
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { base64Encode } from "@idos-network/codecs";
import type { idOSCredential } from "@idos-network/idos-sdk-types";
import * as Base64Codec from "@stablelib/base64";

import type { BackupPasswordInfo } from "../types";
import type {
DiscoverEncryptionKeyResponse,
Expand Down Expand Up @@ -243,7 +244,7 @@ export class IframeEnclave implements EnclaveProvider {

return {
humanId,
encryptionPublicKey: Base64Codec.encode(encryptionPublicKey),
encryptionPublicKey: base64Encode(encryptionPublicKey),
};
}
}
Expand Down
15 changes: 7 additions & 8 deletions packages/idos-sdk-js/src/lib/enclave.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { base64Decode, base64Encode, utf8Decode, utf8Encode } from "@idos-network/codecs";
import type { idOSCredential } from "@idos-network/idos-sdk-types";
import * as Base64Codec from "@stablelib/base64";
import * as Utf8Codec from "@stablelib/utf8";
import type { Auth } from "./auth";
import type { EnclaveProvider } from "./enclave-providers/types";
import type { BackupPasswordInfo } from "./types";
Expand Down Expand Up @@ -43,21 +42,21 @@ export class Enclave {
async encrypt(message: string, receiverPublicKey?: string): Promise<string> {
if (!this.encryptionPublicKey) await this.ready();

return Base64Codec.encode(
return base64Encode(
await this.provider.encrypt(
Utf8Codec.encode(message),
receiverPublicKey === undefined ? undefined : Base64Codec.decode(receiverPublicKey),
utf8Encode(message),
receiverPublicKey === undefined ? undefined : base64Decode(receiverPublicKey),
),
);
}

async decrypt(message: string, senderPublicKey?: string): Promise<string> {
if (!this.encryptionPublicKey) await this.ready();

return Utf8Codec.decode(
return utf8Decode(
await this.provider.decrypt(
Base64Codec.decode(message),
senderPublicKey === undefined ? undefined : Base64Codec.decode(senderPublicKey),
base64Decode(message),
senderPublicKey === undefined ? undefined : base64Decode(senderPublicKey),
),
);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/idos-sdk-js/src/lib/grants/near.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { base64Decode } from "@idos-network/codecs";
import type { SignMessageParams, SignedMessage, Wallet } from "@near-wallet-selector/core";
import * as Base64Codec from "@stablelib/base64";
import type { Contract, connect, keyStores, providers } from "near-api-js";
import { Nonce } from "../nonce";
import Grant from "./grant";
Expand Down Expand Up @@ -135,7 +135,7 @@ export class NearGrants implements GrantChild {
if (!nonce) throw new Error("signMessage is expected to return a nonce, but it didn't");

return {
signature: Base64Codec.decode(b64Signature),
signature: base64Decode(b64Signature),
nonce,
};
}
Expand Down
4 changes: 2 additions & 2 deletions packages/idos-sdk-js/src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { hexEncode } from "@idos-network/codecs";
import type { AccessKeyList } from "@near-js/types";
import { encode as encodeHex } from "@stablelib/hex";
import bs58 from "bs58";
import type { connect as connectT } from "near-api-js";

Expand Down Expand Up @@ -36,7 +36,7 @@ export async function getNearFullAccessPublicKeys(

export function implicitAddressFromPublicKey(publicKey: string) {
const key_without_prefix = publicKey.replace(/^ed25519:/, "");
const implicitAddress = encodeHex(bs58.decode(key_without_prefix));
const implicitAddress = hexEncode(bs58.decode(key_without_prefix));
return implicitAddress;
}

Expand Down
4 changes: 1 addition & 3 deletions packages/issuer-sdk-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,8 @@
"vitest": "^0.31.4"
},
"dependencies": {
"@idos-network/codecs": "workspace:*",
"@kwilteam/kwil-js": "0.7.1",
"@stablelib/base64": "^1.0.1",
"@stablelib/hex": "^2.0.0",
"@stablelib/utf8": "^1.0.1",
"es-toolkit": "^1.23.0",
"tiny-invariant": "^1.3.3",
"tweetnacl": "^1.0.3"
Expand Down
20 changes: 9 additions & 11 deletions packages/issuer-sdk-js/src/credentials.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { base64Decode, base64Encode, hexEncode, utf8Encode } from "@idos-network/codecs";
import type { idOSCredential } from "@idos-network/idos-sdk-types";
import * as Base64Codec from "@stablelib/base64";
import * as HexCodec from "@stablelib/hex";
import * as Utf8Codec from "@stablelib/utf8";
import { omit } from "es-toolkit";
import nacl from "tweetnacl";
import type { IssuerConfig } from "./create-issuer-config";
Expand All @@ -15,13 +13,13 @@ const buildUpdateablePublicNotes = (
{ publicNotes }: UpdateablePublicNotes,
) => {
const publicNotesSignature = nacl.sign.detached(
Utf8Codec.encode(publicNotes),
utf8Encode(publicNotes),
issuerConfig.signingKeyPair.secretKey,
);

return {
public_notes: publicNotes,
public_notes_signature: Base64Codec.encode(publicNotesSignature),
public_notes_signature: base64Encode(publicNotesSignature),
};
};

Expand All @@ -44,7 +42,7 @@ const buildInsertableIDOSCredential = (
receiverEncryptionPublicKey: Uint8Array;
},
): InsertableIDOSCredential => {
const content = Base64Codec.decode(
const content = base64Decode(
encryptContent(
plaintextContent,
receiverEncryptionPublicKey,
Expand All @@ -58,20 +56,20 @@ const buildInsertableIDOSCredential = (

return {
human_id: humanId,
content: Base64Codec.encode(content),
content: base64Encode(content),

public_notes,
public_notes_signature,

broader_signature: Base64Codec.encode(
broader_signature: base64Encode(
nacl.sign.detached(
Uint8Array.from([...Base64Codec.decode(public_notes_signature), ...content]),
Uint8Array.from([...base64Decode(public_notes_signature), ...content]),
issuerConfig.signingKeyPair.secretKey,
),
),

issuer_auth_public_key: HexCodec.encode(issuerConfig.signingKeyPair.publicKey, true),
encryption_public_key: Base64Codec.encode(issuerConfig.encryptionKeyPair.publicKey),
issuer_auth_public_key: hexEncode(issuerConfig.signingKeyPair.publicKey, true),
encryption_public_key: base64Encode(issuerConfig.encryptionKeyPair.publicKey),
};
};

Expand Down
10 changes: 5 additions & 5 deletions packages/issuer-sdk-js/src/internal.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { base64Encode } from "@idos-network/codecs";
import { Utils } from "@kwilteam/kwil-js";
import * as Base64Codec from "@stablelib/base64";
import nacl from "tweetnacl";

export function ensureEntityId<T extends { id?: string }>(entity: T): { id: string } & T {
Expand Down Expand Up @@ -29,9 +29,9 @@ export function encryptContent(
throw Error(
`Couldn't encrypt. ${JSON.stringify(
{
message: Base64Codec.encode(message),
nonce: Base64Codec.encode(nonce),
receiverPublicKey: Base64Codec.encode(receiverPublicKey),
message: base64Encode(message),
nonce: base64Encode(nonce),
receiverPublicKey: base64Encode(receiverPublicKey),
},
null,
2,
Expand All @@ -42,5 +42,5 @@ export function encryptContent(
fullMessage.set(nonce, 0);
fullMessage.set(encrypted, nonce.length);

return Base64Codec.encode(fullMessage);
return base64Encode(fullMessage);
}
Loading

0 comments on commit ac4a2b0

Please sign in to comment.