-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from futureforging/jrayback_240329_general-cle…
…anup General cleanup
- Loading branch information
Showing
31 changed files
with
146 additions
and
882 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file was deleted.
Oops, something went wrong.
4 changes: 2 additions & 2 deletions
4
src/api/encode/encoders/encodeBinary.test.ts → src/api/encode/binary.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
2 changes: 1 addition & 1 deletion
2
src/api/encode/encoders/lib/padUpFront.ts → src/api/encode/lib/padUpFront.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...api/encode/encoders/lib/swapInTypeCode.ts → src/api/encode/lib/swapInTypeCode.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 6 additions & 6 deletions
12
src/api/encode/encoders/encodeRaw.test.ts → src/api/encode/raw.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) |
10 changes: 5 additions & 5 deletions
10
src/api/encode/encoders/encodeText.test.ts → src/api/encode/text.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.