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
  • Loading branch information
ditoglez committed Dec 14, 2024
1 parent fc2c735 commit b48da45
Show file tree
Hide file tree
Showing 16 changed files with 164 additions and 93 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 @@ -371,7 +369,7 @@ export class Data {
receiverEncryptionPublicKey,
);
const publicNotesSignature = nacl.sign.detached(
Utf8Codec.encode(publicNotes),
utf8Encode(publicNotes),
issuerAuthenticationKeyPair.secretKey,
);

Expand All @@ -380,16 +378,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(encryptorPublicKey),
};
}
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 @@ -246,7 +247,7 @@ export class IframeEnclave implements EnclaveProvider {

return {
humanId,
encryptionPublicKey: Base64Codec.encode(encryptionPublicKey),
encryptionPublicKey: base64Encode(encryptionPublicKey),
};
}
}
Expand Down
17 changes: 8 additions & 9 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 @@ -47,23 +46,23 @@ export class Enclave {
if (!this.encryptionPublicKey) await this.ready();

const { content, encryptorPublicKey } = await this.provider.encrypt(
Utf8Codec.encode(message),
receiverPublicKey === undefined ? undefined : Base64Codec.decode(receiverPublicKey),
utf8Encode(message),
receiverPublicKey === undefined ? undefined : base64Decode(receiverPublicKey),
);

return {
content: Base64Codec.encode(content),
encryptorPublicKey: Base64Codec.encode(encryptorPublicKey),
content: base64Encode(content),
encryptorPublicKey: base64Encode(encryptorPublicKey),
};
}

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 @@ -45,7 +43,7 @@ const buildInsertableIDOSCredential = (
},
): InsertableIDOSCredential => {
const ephemeralKeyPair = nacl.box.keyPair();
const content = Base64Codec.decode(
const content = base64Decode(
encryptContent(plaintextContent, receiverEncryptionPublicKey, ephemeralKeyPair.secretKey),
);

Expand All @@ -55,20 +53,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(ephemeralKeyPair.publicKey),
issuer_auth_public_key: hexEncode(issuerConfig.signingKeyPair.publicKey, true),
encryption_public_key: base64Encode(ephemeralKeyPair.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 b48da45

Please sign in to comment.