Skip to content

Commit 8ff8d6f

Browse files
authored
Merge pull request #40 from futureforging/jrayback_240329_general-cleanup
General cleanup
2 parents ec37bdb + 9186979 commit 8ff8d6f

31 files changed

+146
-882
lines changed

index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Note: Main entry point for the library
22
// This represents the public API of the library
33
export {
4-
encodeRaw,
5-
encodeText,
6-
encodeBinary,
4+
raw,
5+
text,
6+
binary,
77
toRaw,
88
toText,
99
toBinary

jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default {
1111
transform: {
1212
// allows jest to resolve module paths to a .ts source file using a .js extension
1313
// see dox for more information on this setting: https://kulshekhar.github.io/ts-jest/docs/guides/esm-support/
14-
'^.+\\.[t|j]sx?$': [
14+
'^.+\\.[tj]sx?$': [
1515
'@swc/jest' // use swc swap in for faster tests
1616
// {
1717
// useESM: true // don't seem to need this section when using @swc/jest

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@jrayback/simple-cesr",
33
"type": "module",
4-
"version": "0.1.2",
4+
"version": "0.1.4",
55
"description": "A simple, limited, true-to-spec implementation of the CESR protocol.",
66
"main": "dist/index.js",
77
"files": [
@@ -13,8 +13,9 @@
1313
"test": "jest ./src/api ./src/implementation",
1414
"standards-test": "jest ./testing/standards",
1515
"all-tests": "jest",
16-
"formatting": "npx ts-standard --fix",
17-
"full-build": "npm run all-tests && npm run formatting && npm run build"
16+
"formatter": "npx ts-standard --fix",
17+
"emitter" : "tsc --emitDeclarationOnly --declaration --outDir dist",
18+
"full-build": "npm run all-tests && npm run formatter && npm run build && npm run emitter"
1819
},
1920
"repository": {
2021
"type": "git",

src/api/api.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ describe('API', () => {
44
it('works...', () => {
55
const code = 'M'
66
const primitive = new Uint8Array([0, 1])
7-
expect(() => cesr.encodeRaw(code, primitive)).not.toThrow()
8-
expect(() => cesr.encodeText(code, primitive)).not.toThrow()
9-
expect(() => cesr.encodeBinary(code, primitive)).not.toThrow()
10-
const raw = cesr.encodeRaw(code, primitive)
11-
const text = cesr.encodeText(code, primitive)
12-
const binary = cesr.encodeBinary(code, primitive)
7+
expect(() => cesr.raw(code, primitive)).not.toThrow()
8+
expect(() => cesr.text(code, primitive)).not.toThrow()
9+
expect(() => cesr.binary(code, primitive)).not.toThrow()
10+
const raw = cesr.raw(code, primitive)
11+
const text = cesr.text(code, primitive)
12+
const binary = cesr.binary(code, primitive)
1313
expect(() => cesr.toRaw(text)).not.toThrow()
1414
expect(() => cesr.toRaw(binary)).not.toThrow()
1515
expect(() => cesr.toText(binary)).not.toThrow()

src/api/api.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
1-
import { encodeBinary } from './encode/encoders/encodeBinary.js'
2-
import { encodeText } from './encode/encoders/encodeText.js'
3-
import { encodeRaw } from './encode/encoders/encodeRaw.js'
4-
import { toBinary } from './transform/toBinary.js'
5-
import { toText } from './transform/toText.js'
6-
import { toRaw } from './transform/toRaw.js'
7-
8-
export {
9-
encodeBinary,
10-
encodeText,
11-
encodeRaw,
12-
toBinary,
13-
toText,
14-
toRaw
15-
}
1+
export { binary } from './encode/binary.js'
2+
export { text } from './encode/text.js'
3+
export { raw } from './encode/raw.js'
4+
export { toBinary } from './transform/toBinary.js'
5+
export { toText } from './transform/toText.js'
6+
export { toRaw } from './transform/toRaw.js'

src/api/encode/asText.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { Buffer } from 'buffer'
2-
import { encodeBinary } from './encodeBinary.js'
2+
import { binary } from './binary.js'
33

44
describe('Binary encoder', () => {
55
it('processes code "M" correctly', () => {
6-
const result = encodeBinary('M', Buffer.from([0x00, 0x01]))
6+
const result = binary('M', Buffer.from([0x00, 0x01]))
77
expect(result).toStrictEqual(Buffer.from([48, 0, 1])) // 48 is the base64 encoding of 'M'
88
})
99
})

src/api/encode/binary.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { pipe } from '../../lib/util/pipe.js'
2+
import { Code } from '../../core/code/code.js'
3+
import { Binary, Raw } from '../../core/domain/domains.js'
4+
import { make } from '../../implementation/make.js'
5+
import { asText } from './lib/asText.js'
6+
import { asBinary } from './lib/asBinary.js'
7+
8+
// encodes a primitive in the Binary domain. needs the type code and the primitive as a byte array
9+
export const binary = (code: Code, primitive: Uint8Array): Binary => {
10+
const doRaw = rawWith(code) // which code should be used to make the raw?
11+
const doText = textWith(code) // which code should be used to encode the text?
12+
const doBinary = asBinary
13+
14+
// for Binary, go through all three steps...
15+
return pipe(
16+
primitive,
17+
doRaw, // make the Raw
18+
doText, // encode the raw primitive as Text
19+
doBinary // return the encoded string as a Binary
20+
)
21+
}
22+
23+
const rawWith = (code: Code) => (primitive: Uint8Array) => make(code, primitive)
24+
const textWith = (code: Code) => (tuple: Raw) => asText(code, tuple.raw)

src/api/encode/encoders/encodeBinary.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/api/encode/encoders/encodeRaw.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/api/encode/encoders/encodeText.ts

Lines changed: 0 additions & 16 deletions
This file was deleted.
File renamed without changes.

src/api/encode/lib/asText.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { swapInTypeCode } from './swapInTypeCode.js'
2+
import { padUpFront } from './padUpFront.js'
3+
import { convertToBase64 } from './convertToBase64.js'
4+
import { Code } from '../../..//core/code/code.js'
5+
import { pipe } from '../../../lib/util/pipe.js'
6+
7+
// builds the proper text encoding for the given primitive
8+
export const asText = (code: Code, primitive: Buffer): string =>
9+
pipe(
10+
primitive,
11+
padUpFront,
12+
convertToBase64,
13+
swapInTypeCode(code)
14+
)

src/api/encode/encoders/lib/padUpFront.ts renamed to src/api/encode/lib/padUpFront.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Buffer } from 'buffer'
22
import { match } from 'ts-pattern'
3-
import { padSize } from '../../../../lib/keri/padSize.js'
3+
import { padSize } from '../../../lib/keri/padSize.js'
44

55
// only valid pad sizes in KERI are 0, 1, or 2
66
type PadSize = 0 | 1 | 2

src/api/encode/encoders/lib/swapInTypeCode.ts renamed to src/api/encode/lib/swapInTypeCode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { match } from 'ts-pattern'
2-
import { Code, CodeLength } from '../../../../core/code/code.js'
2+
import { Code, CodeLength } from '../../../core/code/code.js'
33

44
type Swapper = (text: string) => string
55

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
import { Buffer } from 'buffer'
2-
import { encodeRaw } from './encodeRaw.js'
2+
import { raw } from './raw.js'
33

44
describe('Raw encoder', () => {
55
it("encodes a one-character primitive: 'A'", () => {
66
// exmaple of 256 bit seed for ED25519 private key
77
const binary = Buffer.from('4f3c811f1d6fa45a9d0b65e2c1e0ddf801d063e3f7e100c3500b6a229a5e9f2a', 'hex') // 32 bytes
8-
const result = encodeRaw('A', binary)
8+
const result = raw('A', binary)
99
expect(result).toStrictEqual({ code: 'A', raw: binary })
1010
})
1111
it("encodes another one-character primitive: 'M'", () => {
12-
const result = encodeRaw('M', Buffer.from([0x00, 0x01]))
12+
const result = raw('M', Buffer.from([0x00, 0x01]))
1313
expect(result).toStrictEqual({ code: 'M', raw: Buffer.from([0, 1]) })
1414
})
1515
it("encodes yet another one-character primitive: 'N'", () => {
16-
const result = encodeRaw('N', Buffer.from([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]))
16+
const result = raw('N', Buffer.from([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]))
1717
expect(result).toStrictEqual({ code: 'N', raw: Buffer.from([0, 0, 0, 0, 0, 0, 0, 1]) })
1818
})
1919
it("encodes a two-character primitive: '0F'", () => {
2020
// randomly generated example of SHA-256 hash
2121
const binary = Buffer.from('ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f', 'hex')
22-
const result = encodeRaw('0F', binary)
22+
const result = raw('0F', binary)
2323
expect(result).toStrictEqual({ code: '0F', raw: binary })
2424
})
2525
it("encodes a four-character primitive: '1AAB'", () => {
2626
// randomly generated example of ecsda secp256k1 public key
2727
const binary = Buffer.from('02b4f97f6e8e9214e9a2021b2c7ad6f2233499f114fed33ea6bfc3e2b1feaf24c1', 'hex')
28-
const result = encodeRaw('1AAB', binary)
28+
const result = raw('1AAB', binary)
2929
expect(result).toStrictEqual({ code: '1AAB', raw: binary })
3030
})
3131
})

src/api/encode/raw.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Buffer } from 'buffer'
2+
import { make } from '../../implementation/make.js'
3+
import { Code } from '../../core/code/code.js'
4+
import { Raw } from '../../core/domain/domains.js'
5+
6+
export const raw = (code: Code, primitive: Uint8Array): Raw =>
7+
make( // make() ensures the primitive is valid
8+
code,
9+
Buffer.from(primitive)
10+
)
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import { Buffer } from 'buffer'
2-
import { encodeText } from './encodeText.js'
2+
import { text } from './text.js'
33

44
describe('Text Encoder', () => {
55
it('gets "MAAB" from ["M", 1]', () => {
6-
const result = encodeText('M', Buffer.from([0x00, 0x01]))
6+
const result = text('M', Buffer.from([0x00, 0x01]))
77
expect(result).toBe('MAAB')
88
})
99
it('gets "NAAAAAAAAAAB" from ["N", 1]', () => {
10-
const result = encodeText('N', Buffer.from([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]))
10+
const result = text('N', Buffer.from([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]))
1111
expect(result).toBe('NAAAAAAAAAAB')
1212
})
1313
it('gets "0FDdrzWhk2F6..." from ["0F", "ddaf35a19..."]', () => {
14-
const result = encodeText('0F', Buffer.from('ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f', 'hex'))
14+
const result = text('0F', Buffer.from('ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f', 'hex'))
1515
expect(result).toBe('0FDdrzWhk2F6usxBc0muIEExEub6TompfqIKnu7mS1XTmiGSmSonT8GoNro8I6P-671FTUQjZDzoDiqayU-lTKSf')
1616
})
1717
// 1AABArT5f26OkhTpogIbLHrW8iM0mfEU_tM-pr_D4rH-ryTB
1818
it('gets "1AABArT5f2..." from ["1AAB", "02b4f97f6e..."]', () => {
19-
const result = encodeText('1AAB', Buffer.from('02b4f97f6e8e9214e9a2021b2c7ad6f2233499f114fed33ea6bfc3e2b1feaf24c1', 'hex'))
19+
const result = text('1AAB', Buffer.from('02b4f97f6e8e9214e9a2021b2c7ad6f2233499f114fed33ea6bfc3e2b1feaf24c1', 'hex'))
2020
expect(result).toBe('1AABArT5f26OkhTpogIbLHrW8iM0mfEU_tM-pr_D4rH-ryTB')
2121
})
2222
})

src/api/encode/text.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { pipe } from '../../lib/util/pipe.js'
2+
import { Code } from '../../core/code/code.js'
3+
import { Raw, Text } from '../../core/domain/domains.js'
4+
import { make } from '../../implementation/make.js'
5+
import { asText } from './lib/asText.js'
6+
7+
export const text = (code: Code, primitive: Uint8Array): Text => {
8+
const doRaw = rawWith(code) // which code should be used to make the raw?
9+
const doText = textWith(code) // which code should be used to encode the text?
10+
11+
// requires two steps...
12+
return pipe(
13+
primitive,
14+
doRaw, // make the Raw
15+
doText // encode the raw primitive as Text
16+
)
17+
}
18+
19+
const rawWith = (code: Code) => (primitive: Uint8Array) => make(code, primitive)
20+
const textWith = (code: Code) => (tuple: Raw) => asText(code, tuple.raw)

src/api/transform/toBinary.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11

22
import { Raw } from '../../core/domain/domains.js'
3-
import { encodeBinary } from '../encode/encoders/encodeBinary.js'
4-
import { encodeText } from '../encode/encoders/encodeText.js'
3+
import { binary } from '../encode/binary.js'
4+
import { text } from '../encode/text.js'
55
import { toBinary } from './toBinary.js'
66

77
describe('Transform to Binary', () => {
88
it('handles Text', () => {
9-
const raw: Raw = {
9+
const rah: Raw = {
1010
code: '1AAB',
1111
raw: Buffer.from('02b4f97f6e8e9214e9a2021b2c7ad6f2233499f114fed33ea6bfc3e2b1feaf24c1', 'hex')
1212
}
13-
const text = encodeText(raw.code, raw.raw)
14-
const binary = encodeBinary(raw.code, raw.raw)
15-
expect(toBinary(text)).toStrictEqual(binary)
13+
const txt = text(rah.code, rah.raw)
14+
const bin = binary(rah.code, rah.raw)
15+
expect(toBinary(txt)).toStrictEqual(bin)
1616
})
1717
it('handles Raw', () => {
18-
const raw: Raw = {
18+
const rah: Raw = {
1919
code: '1AAB',
2020
raw: Buffer.from('02b4f97f6e8e9214e9a2021b2c7ad6f2233499f114fed33ea6bfc3e2b1feaf24c1', 'hex')
2121
}
22-
const binary = encodeBinary(raw.code, raw.raw)
23-
expect(toBinary(raw)).toStrictEqual(binary)
22+
const bin = binary(rah.code, rah.raw)
23+
expect(toBinary(rah)).toStrictEqual(bin)
2424
})
2525
})

src/api/transform/toBinary.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { Buffer } from 'buffer'
22
import { Binary, Text, Raw } from '../../core/domain/domains.js'
3-
import { encodeBinary } from '../encode/encoders/encodeBinary.js'
3+
import { binary } from '../encode/binary.js'
44

55
export function toBinary (text: Text): Binary
66
export function toBinary (Raw: Raw): Binary
77

88
export function toBinary (textOrRaw: Text | Raw): Binary {
99
if (typeof textOrRaw === 'string') return Buffer.from(textOrRaw, 'base64url')
10-
else return encodeBinary(textOrRaw.code, textOrRaw.raw)
10+
else return binary(textOrRaw.code, textOrRaw.raw)
1111
}

0 commit comments

Comments
 (0)