Skip to content

Commit f974648

Browse files
authored
chore: remove buffer dependency (#321)
1 parent 0ea4966 commit f974648

File tree

12 files changed

+52
-146
lines changed

12 files changed

+52
-146
lines changed

package-lock.json

Lines changed: 3 additions & 41 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
"dependencies": {
6161
"@noble/curves": "^1.8.1",
6262
"@noble/hashes": "^1.3.2",
63-
"buffer": "^6.0.3",
63+
"base64-js": "^1.5.1",
6464
"libsodium-wrappers-sumo": "^0.7.9",
6565
"mathjs": "^12.4.0",
6666
"structured-headers": "^0.5.0"

src/keri/core/base64.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1-
import { Buffer } from 'buffer';
2-
// base64url is supported by node Buffer, but not in buffer package for browser compatibility
3-
// https://github.com/feross/buffer/issues/309
1+
import { fromByteArray, toByteArray } from 'base64-js';
42

5-
// Instead of using a node.js-only module and forcing us to polyfill the Buffer global,
6-
// we insert code from https://gitlab.com/seangenabe/safe-base64 here
3+
export function encodeBase64Url(buffer: Uint8Array): string {
4+
const input = Buffer.from(buffer);
75

8-
export function encodeBase64Url(buffer: Buffer | Uint8Array): string {
9-
const input = Buffer.isBuffer(buffer) ? buffer : Buffer.from(buffer);
10-
11-
return input
12-
.toString('base64')
6+
return fromByteArray(input)
137
.replace(/\+/g, '-')
148
.replace(/\//g, '_')
159
.replace(/=+/, '');
@@ -23,5 +17,5 @@ export function decodeBase64Url(input: string): Uint8Array {
2317
const n = input.length % 4;
2418
const padded = input + '='.repeat(n > 0 ? 4 - n : n);
2519
const base64String = padded.replace(/-/g, '+').replace(/_/g, '/');
26-
return Uint8Array.from(Buffer.from(base64String, 'base64'));
20+
return toByteArray(base64String);
2721
}

src/keri/core/bexter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { BexDex, Matter, MatterArgs, MtrDex } from './matter.ts';
22
import { EmptyMaterialError } from './kering.ts';
3-
import { Buffer } from 'buffer';
43
import { decodeBase64Url, encodeBase64Url } from './base64.ts';
4+
import { concat } from './core.ts';
55

66
const B64REX = '^[A-Za-z0-9\\-_]*$';
77
export const Reb64 = new RegExp(B64REX);
@@ -123,7 +123,7 @@ export class Bexter extends Matter {
123123
get bext(): string {
124124
const sizage = Matter.Sizes.get(this.code);
125125
const wad = Uint8Array.from(new Array(sizage?.ls).fill(0));
126-
const bext = encodeBase64Url(Buffer.from([...wad, ...this.raw]));
126+
const bext = encodeBase64Url(concat(wad, this.raw));
127127

128128
let ws = 0;
129129
if (sizage?.ls === 0 && bext !== undefined) {

src/keri/core/diger.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { blake3 } from '@noble/hashes/blake3';
22
import { Matter, MatterArgs, MtrDex } from './matter.ts';
3-
import { Buffer } from 'buffer';
43

54
/**
65
* @description : Diger is subset of Matter and is used to verify the digest of serialization
@@ -10,10 +9,7 @@ import { Buffer } from 'buffer';
109
*/
1110

1211
export class Diger extends Matter {
13-
private readonly _verify: (
14-
a: Uint8Array,
15-
b: Uint8Array | Buffer
16-
) => boolean;
12+
private readonly _verify: (a: Uint8Array, b: Uint8Array) => boolean;
1713

1814
// This constructor will assign digest verification function to ._verify
1915
constructor(
@@ -79,13 +75,11 @@ export class Diger extends Matter {
7975
return diger.verify(ser) && this.verify(ser);
8076
}
8177

82-
blake3_256(ser: Uint8Array, dig: Uint8Array | Buffer) {
78+
blake3_256(ser: Uint8Array, dig: Uint8Array) {
8379
const digest = blake3.create({ dkLen: 32 }).update(ser).digest();
84-
const other = Uint8Array.from(dig);
8580

8681
return (
87-
digest.length == other.length &&
88-
digest.toString() === other.toString()
82+
digest.length == dig.length && digest.toString() === dig.toString()
8983
);
9084
}
9185
}

src/keri/core/httping.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { b } from './core.ts';
1010
import { Cigar } from './cigar.ts';
1111
import { nowUTC } from './utils.ts';
1212
import { Siger } from './siger.ts';
13-
import { Buffer } from 'buffer';
1413
import { encodeBase64Url } from './base64.ts';
1514

1615
export const HEADER_SIG_INPUT = normalize('Signature-Input');
@@ -130,7 +129,7 @@ export class Unqualified {
130129
}
131130

132131
get qb64(): string {
133-
return encodeBase64Url(Buffer.from(this._raw));
132+
return encodeBase64Url(this._raw);
134133
}
135134

136135
get qb64b(): Uint8Array {

src/keri/core/indexer.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { EmptyMaterialError } from './kering.ts';
22
import { b, b64ToInt, d, intToB64, readInt } from './core.ts';
3-
import { Buffer } from 'buffer';
43
import { decodeBase64Url, encodeBase64Url } from './base64.ts';
54

65
export class IndexerCodex {
@@ -398,8 +397,7 @@ export class Indexer {
398397
bytes[odx] = raw[i];
399398
}
400399

401-
const full =
402-
both + encodeBase64Url(Buffer.from(bytes)).slice(ps - xizage.ls);
400+
const full = both + encodeBase64Url(bytes).slice(ps - xizage.ls);
403401
if (full.length != xizage.fs) {
404402
throw new Error(`Invalid code=${both} for raw size=${raw.length}.`);
405403
}

src/keri/core/matter.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { EmptyMaterialError } from './kering.ts';
22

33
import { intToB64, readInt } from './core.ts';
44
import { b, d } from './core.ts';
5-
import { Buffer } from 'buffer';
65
import { decodeBase64Url, encodeBase64Url } from './base64.ts';
76

87
export class Codex {
@@ -421,7 +420,7 @@ export class Matter {
421420
bytes[odx] = raw[i];
422421
}
423422

424-
return both + encodeBase64Url(Buffer.from(bytes));
423+
return both + encodeBase64Url(bytes);
425424
} else {
426425
const both = code;
427426
const cs = both.length;
@@ -443,7 +442,7 @@ export class Matter {
443442
bytes[odx] = raw[i];
444443
}
445444

446-
return both + encodeBase64Url(Buffer.from(bytes)).slice(cs % 4);
445+
return both + encodeBase64Url(bytes).slice(cs % 4);
447446
}
448447
}
449448

src/keri/core/saider.ts

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { DigiDex, Matter, MatterArgs, MtrDex } from './matter.ts';
22
import { deversify, Dict, Serials } from './core.ts';
33
import { EmptyMaterialError } from './kering.ts';
44
import { dumps, sizeify } from './serder.ts';
5-
import { Buffer } from 'buffer';
65
import { blake3 } from '@noble/hashes/blake3';
76

87
const Dummy = '#';
@@ -11,25 +10,7 @@ export enum Ids {
1110
d = 'd',
1211
}
1312

14-
class Digestage {
15-
public klas: any = undefined;
16-
public size: number | undefined = 0;
17-
public length: number | undefined = 0;
18-
constructor(klas: any, size?: number, length?: number) {
19-
this.klas = klas;
20-
this.size = size;
21-
this.length = length;
22-
}
23-
}
24-
2513
export class Saider extends Matter {
26-
static Digests = new Map<string, Digestage>([
27-
[
28-
MtrDex.Blake3_256,
29-
new Digestage(Saider._derive_blake3_256, undefined, undefined),
30-
],
31-
]);
32-
3314
constructor(
3415
{ raw, code, qb64b, qb64, qb2 }: MatterArgs,
3516
sad?: Dict<any>,
@@ -69,21 +50,13 @@ export class Saider extends Matter {
6950
}
7051
}
7152

72-
static _derive_blake3_256(
73-
ser: Uint8Array,
74-
_digest_size: number,
75-
_length: number
76-
) {
77-
return Buffer.from(blake3.create({ dkLen: 32 }).update(ser).digest());
78-
}
79-
8053
private static _derive(
8154
sad: Dict<any>,
8255
code: string,
8356
kind: Serials | undefined,
8457
label: string
8558
): [Uint8Array, Dict<any>] {
86-
if (!DigiDex.has(code) || !Saider.Digests.has(code)) {
59+
if (!DigiDex.has(code)) {
8760
throw new Error(`Unsupported digest code = ${code}.`);
8861
}
8962

@@ -95,19 +68,14 @@ export class Saider extends Matter {
9568

9669
const ser = { ...sad };
9770

98-
const digestage = Saider.Digests.get(code);
99-
10071
const cpa = Saider._serialze(ser, kind);
101-
const args: any[] = [];
102-
if (digestage!.size != undefined) {
103-
args.push(digestage!.size);
104-
}
10572

106-
if (digestage!.length != undefined) {
107-
args.push(digestage!.length);
73+
switch (code) {
74+
case MtrDex.Blake3_256:
75+
return [blake3.create({ dkLen: 32 }).update(cpa).digest(), sad];
76+
default:
77+
throw new Error(`Unsupported digest code = ${code}.`);
10878
}
109-
110-
return [digestage!.klas(cpa, ...args), sad];
11179
}
11280

11381
public derive(

test/core/base64.test.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ import {
33
decodeBase64Url,
44
encodeBase64Url,
55
} from '../../src/keri/core/base64.ts';
6-
import { Buffer } from 'buffer';
76

87
test('encode', () => {
9-
assert.equal(encodeBase64Url(Uint8Array.from(Buffer.from('f'))), 'Zg');
10-
assert.equal(encodeBase64Url(Uint8Array.from(Buffer.from('fi'))), 'Zmk');
11-
assert.equal(encodeBase64Url(Uint8Array.from(Buffer.from('fis'))), 'Zmlz');
8+
assert.equal(encodeBase64Url(Uint8Array.from([102])), 'Zg');
9+
assert.equal(encodeBase64Url(Uint8Array.from([102, 105])), 'Zmk');
10+
assert.equal(encodeBase64Url(Uint8Array.from([102, 105, 115])), 'Zmlz');
1211
assert.equal(
13-
encodeBase64Url(Uint8Array.from(Buffer.from('fish'))),
12+
encodeBase64Url(Uint8Array.from([102, 105, 115, 104])),
1413
'ZmlzaA'
1514
);
1615
assert.equal(encodeBase64Url(Uint8Array.from([248])), '-A');
@@ -25,18 +24,17 @@ test('decode', () => {
2524
decodeBase64Url('ZmlzaA'),
2625
Uint8Array.from([102, 105, 115, 104])
2726
);
28-
assert.deepEqual(Buffer.from([248]), decodeBase64Url('-A'));
29-
assert.deepEqual(Buffer.from([252]), decodeBase64Url('_A'));
27+
assert.deepEqual(Uint8Array.from([248]), decodeBase64Url('-A'));
28+
assert.deepEqual(Uint8Array.from([252]), decodeBase64Url('_A'));
3029
});
3130

3231
test('Test encode / decode compare with built in node Buffer', () => {
3332
const text = '🏳️🏳️';
3433
const b64url = '8J-Ps--4j_Cfj7PvuI8';
34+
const data = Uint8Array.from([
35+
240, 159, 143, 179, 239, 184, 143, 240, 159, 143, 179, 239, 184, 143,
36+
]);
3537

36-
assert.deepEqual(
37-
Buffer.from(text).toString('base64url'),
38-
encodeBase64Url(Buffer.from(text))
39-
);
40-
41-
assert.deepEqual(Buffer.from(b64url, 'base64url'), decodeBase64Url(b64url));
38+
assert.deepEqual(b64url, encodeBase64Url(new TextEncoder().encode(text)));
39+
assert.deepEqual(data, decodeBase64Url(b64url));
4240
});

0 commit comments

Comments
 (0)