Skip to content

Commit

Permalink
Merge pull request #40 from futureforging/jrayback_240329_general-cle…
Browse files Browse the repository at this point in the history
…anup

General cleanup
  • Loading branch information
jrayback authored Mar 29, 2024
2 parents ec37bdb + 9186979 commit 8ff8d6f
Show file tree
Hide file tree
Showing 31 changed files with 146 additions and 882 deletions.
6 changes: 3 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Note: Main entry point for the library
// This represents the public API of the library
export {
encodeRaw,
encodeText,
encodeBinary,
raw,
text,
binary,
toRaw,
toText,
toBinary
Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default {
transform: {
// allows jest to resolve module paths to a .ts source file using a .js extension
// see dox for more information on this setting: https://kulshekhar.github.io/ts-jest/docs/guides/esm-support/
'^.+\\.[t|j]sx?$': [
'^.+\\.[tj]sx?$': [
'@swc/jest' // use swc swap in for faster tests
// {
// useESM: true // don't seem to need this section when using @swc/jest
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@jrayback/simple-cesr",
"type": "module",
"version": "0.1.2",
"version": "0.1.4",
"description": "A simple, limited, true-to-spec implementation of the CESR protocol.",
"main": "dist/index.js",
"files": [
Expand All @@ -13,8 +13,9 @@
"test": "jest ./src/api ./src/implementation",
"standards-test": "jest ./testing/standards",
"all-tests": "jest",
"formatting": "npx ts-standard --fix",
"full-build": "npm run all-tests && npm run formatting && npm run build"
"formatter": "npx ts-standard --fix",
"emitter" : "tsc --emitDeclarationOnly --declaration --outDir dist",
"full-build": "npm run all-tests && npm run formatter && npm run build && npm run emitter"
},
"repository": {
"type": "git",
Expand Down
12 changes: 6 additions & 6 deletions src/api/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ describe('API', () => {
it('works...', () => {
const code = 'M'
const primitive = new Uint8Array([0, 1])
expect(() => cesr.encodeRaw(code, primitive)).not.toThrow()
expect(() => cesr.encodeText(code, primitive)).not.toThrow()
expect(() => cesr.encodeBinary(code, primitive)).not.toThrow()
const raw = cesr.encodeRaw(code, primitive)
const text = cesr.encodeText(code, primitive)
const binary = cesr.encodeBinary(code, primitive)
expect(() => cesr.raw(code, primitive)).not.toThrow()
expect(() => cesr.text(code, primitive)).not.toThrow()
expect(() => cesr.binary(code, primitive)).not.toThrow()
const raw = cesr.raw(code, primitive)
const text = cesr.text(code, primitive)
const binary = cesr.binary(code, primitive)
expect(() => cesr.toRaw(text)).not.toThrow()
expect(() => cesr.toRaw(binary)).not.toThrow()
expect(() => cesr.toText(binary)).not.toThrow()
Expand Down
21 changes: 6 additions & 15 deletions src/api/api.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import { encodeBinary } from './encode/encoders/encodeBinary.js'
import { encodeText } from './encode/encoders/encodeText.js'
import { encodeRaw } from './encode/encoders/encodeRaw.js'
import { toBinary } from './transform/toBinary.js'
import { toText } from './transform/toText.js'
import { toRaw } from './transform/toRaw.js'

export {
encodeBinary,
encodeText,
encodeRaw,
toBinary,
toText,
toRaw
}
export { binary } from './encode/binary.js'
export { text } from './encode/text.js'
export { raw } from './encode/raw.js'
export { toBinary } from './transform/toBinary.js'
export { toText } from './transform/toText.js'
export { toRaw } from './transform/toRaw.js'
14 changes: 0 additions & 14 deletions src/api/encode/asText.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Buffer } from 'buffer'
import { encodeBinary } from './encodeBinary.js'
import { binary } from './binary.js'

describe('Binary encoder', () => {
it('processes code "M" correctly', () => {
const result = encodeBinary('M', Buffer.from([0x00, 0x01]))
const result = binary('M', Buffer.from([0x00, 0x01]))
expect(result).toStrictEqual(Buffer.from([48, 0, 1])) // 48 is the base64 encoding of 'M'
})
})
24 changes: 24 additions & 0 deletions src/api/encode/binary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { pipe } from '../../lib/util/pipe.js'
import { Code } from '../../core/code/code.js'
import { Binary, Raw } from '../../core/domain/domains.js'
import { make } from '../../implementation/make.js'
import { asText } from './lib/asText.js'
import { asBinary } from './lib/asBinary.js'

// encodes a primitive in the Binary domain. needs the type code and the primitive as a byte array
export const binary = (code: Code, primitive: Uint8Array): Binary => {
const doRaw = rawWith(code) // which code should be used to make the raw?
const doText = textWith(code) // which code should be used to encode the text?
const doBinary = asBinary

// for Binary, go through all three steps...
return pipe(
primitive,
doRaw, // make the Raw
doText, // encode the raw primitive as Text
doBinary // return the encoded string as a Binary
)
}

const rawWith = (code: Code) => (primitive: Uint8Array) => make(code, primitive)
const textWith = (code: Code) => (tuple: Raw) => asText(code, tuple.raw)
20 changes: 0 additions & 20 deletions src/api/encode/encoders/encodeBinary.ts

This file was deleted.

11 changes: 0 additions & 11 deletions src/api/encode/encoders/encodeRaw.ts

This file was deleted.

16 changes: 0 additions & 16 deletions src/api/encode/encoders/encodeText.ts

This file was deleted.

File renamed without changes.
14 changes: 14 additions & 0 deletions src/api/encode/lib/asText.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { swapInTypeCode } from './swapInTypeCode.js'
import { padUpFront } from './padUpFront.js'
import { convertToBase64 } from './convertToBase64.js'
import { Code } from '../../..//core/code/code.js'
import { pipe } from '../../../lib/util/pipe.js'

// builds the proper text encoding for the given primitive
export const asText = (code: Code, primitive: Buffer): string =>
pipe(
primitive,
padUpFront,
convertToBase64,
swapInTypeCode(code)
)
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Buffer } from 'buffer'
import { match } from 'ts-pattern'
import { padSize } from '../../../../lib/keri/padSize.js'
import { padSize } from '../../../lib/keri/padSize.js'

// only valid pad sizes in KERI are 0, 1, or 2
type PadSize = 0 | 1 | 2
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { match } from 'ts-pattern'
import { Code, CodeLength } from '../../../../core/code/code.js'
import { Code, CodeLength } from '../../../core/code/code.js'

type Swapper = (text: string) => string

Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import { Buffer } from 'buffer'
import { encodeRaw } from './encodeRaw.js'
import { raw } from './raw.js'

describe('Raw encoder', () => {
it("encodes a one-character primitive: 'A'", () => {
// exmaple of 256 bit seed for ED25519 private key
const binary = Buffer.from('4f3c811f1d6fa45a9d0b65e2c1e0ddf801d063e3f7e100c3500b6a229a5e9f2a', 'hex') // 32 bytes
const result = encodeRaw('A', binary)
const result = raw('A', binary)
expect(result).toStrictEqual({ code: 'A', raw: binary })
})
it("encodes another one-character primitive: 'M'", () => {
const result = encodeRaw('M', Buffer.from([0x00, 0x01]))
const result = raw('M', Buffer.from([0x00, 0x01]))
expect(result).toStrictEqual({ code: 'M', raw: Buffer.from([0, 1]) })
})
it("encodes yet another one-character primitive: 'N'", () => {
const result = encodeRaw('N', Buffer.from([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]))
const result = raw('N', Buffer.from([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]))
expect(result).toStrictEqual({ code: 'N', raw: Buffer.from([0, 0, 0, 0, 0, 0, 0, 1]) })
})
it("encodes a two-character primitive: '0F'", () => {
// randomly generated example of SHA-256 hash
const binary = Buffer.from('ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f', 'hex')
const result = encodeRaw('0F', binary)
const result = raw('0F', binary)
expect(result).toStrictEqual({ code: '0F', raw: binary })
})
it("encodes a four-character primitive: '1AAB'", () => {
// randomly generated example of ecsda secp256k1 public key
const binary = Buffer.from('02b4f97f6e8e9214e9a2021b2c7ad6f2233499f114fed33ea6bfc3e2b1feaf24c1', 'hex')
const result = encodeRaw('1AAB', binary)
const result = raw('1AAB', binary)
expect(result).toStrictEqual({ code: '1AAB', raw: binary })
})
})
10 changes: 10 additions & 0 deletions src/api/encode/raw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Buffer } from 'buffer'
import { make } from '../../implementation/make.js'
import { Code } from '../../core/code/code.js'
import { Raw } from '../../core/domain/domains.js'

export const raw = (code: Code, primitive: Uint8Array): Raw =>
make( // make() ensures the primitive is valid
code,
Buffer.from(primitive)
)
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { Buffer } from 'buffer'
import { encodeText } from './encodeText.js'
import { text } from './text.js'

describe('Text Encoder', () => {
it('gets "MAAB" from ["M", 1]', () => {
const result = encodeText('M', Buffer.from([0x00, 0x01]))
const result = text('M', Buffer.from([0x00, 0x01]))
expect(result).toBe('MAAB')
})
it('gets "NAAAAAAAAAAB" from ["N", 1]', () => {
const result = encodeText('N', Buffer.from([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]))
const result = text('N', Buffer.from([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]))
expect(result).toBe('NAAAAAAAAAAB')
})
it('gets "0FDdrzWhk2F6..." from ["0F", "ddaf35a19..."]', () => {
const result = encodeText('0F', Buffer.from('ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f', 'hex'))
const result = text('0F', Buffer.from('ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f', 'hex'))
expect(result).toBe('0FDdrzWhk2F6usxBc0muIEExEub6TompfqIKnu7mS1XTmiGSmSonT8GoNro8I6P-671FTUQjZDzoDiqayU-lTKSf')
})
// 1AABArT5f26OkhTpogIbLHrW8iM0mfEU_tM-pr_D4rH-ryTB
it('gets "1AABArT5f2..." from ["1AAB", "02b4f97f6e..."]', () => {
const result = encodeText('1AAB', Buffer.from('02b4f97f6e8e9214e9a2021b2c7ad6f2233499f114fed33ea6bfc3e2b1feaf24c1', 'hex'))
const result = text('1AAB', Buffer.from('02b4f97f6e8e9214e9a2021b2c7ad6f2233499f114fed33ea6bfc3e2b1feaf24c1', 'hex'))
expect(result).toBe('1AABArT5f26OkhTpogIbLHrW8iM0mfEU_tM-pr_D4rH-ryTB')
})
})
20 changes: 20 additions & 0 deletions src/api/encode/text.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { pipe } from '../../lib/util/pipe.js'
import { Code } from '../../core/code/code.js'
import { Raw, Text } from '../../core/domain/domains.js'
import { make } from '../../implementation/make.js'
import { asText } from './lib/asText.js'

export const text = (code: Code, primitive: Uint8Array): Text => {
const doRaw = rawWith(code) // which code should be used to make the raw?
const doText = textWith(code) // which code should be used to encode the text?

// requires two steps...
return pipe(
primitive,
doRaw, // make the Raw
doText // encode the raw primitive as Text
)
}

const rawWith = (code: Code) => (primitive: Uint8Array) => make(code, primitive)
const textWith = (code: Code) => (tuple: Raw) => asText(code, tuple.raw)
18 changes: 9 additions & 9 deletions src/api/transform/toBinary.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@

import { Raw } from '../../core/domain/domains.js'
import { encodeBinary } from '../encode/encoders/encodeBinary.js'
import { encodeText } from '../encode/encoders/encodeText.js'
import { binary } from '../encode/binary.js'
import { text } from '../encode/text.js'
import { toBinary } from './toBinary.js'

describe('Transform to Binary', () => {
it('handles Text', () => {
const raw: Raw = {
const rah: Raw = {
code: '1AAB',
raw: Buffer.from('02b4f97f6e8e9214e9a2021b2c7ad6f2233499f114fed33ea6bfc3e2b1feaf24c1', 'hex')
}
const text = encodeText(raw.code, raw.raw)
const binary = encodeBinary(raw.code, raw.raw)
expect(toBinary(text)).toStrictEqual(binary)
const txt = text(rah.code, rah.raw)
const bin = binary(rah.code, rah.raw)
expect(toBinary(txt)).toStrictEqual(bin)
})
it('handles Raw', () => {
const raw: Raw = {
const rah: Raw = {
code: '1AAB',
raw: Buffer.from('02b4f97f6e8e9214e9a2021b2c7ad6f2233499f114fed33ea6bfc3e2b1feaf24c1', 'hex')
}
const binary = encodeBinary(raw.code, raw.raw)
expect(toBinary(raw)).toStrictEqual(binary)
const bin = binary(rah.code, rah.raw)
expect(toBinary(rah)).toStrictEqual(bin)
})
})
4 changes: 2 additions & 2 deletions src/api/transform/toBinary.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Buffer } from 'buffer'
import { Binary, Text, Raw } from '../../core/domain/domains.js'
import { encodeBinary } from '../encode/encoders/encodeBinary.js'
import { binary } from '../encode/binary.js'

export function toBinary (text: Text): Binary
export function toBinary (Raw: Raw): Binary

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

0 comments on commit 8ff8d6f

Please sign in to comment.