-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 42a5b65
Showing
7 changed files
with
116 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "../include/api-extractor-base.json" | ||
} |
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,38 @@ | ||
{ | ||
"name": "@sphereon/ssi-sdk-core", | ||
"description": "SSI SDK Core & Interfaces", | ||
"version": "0.0.1", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"scripts": { | ||
"build": "tsc" | ||
}, | ||
"dependencies": { | ||
"@sphereon/did-uni-client": "^0.3.3", | ||
"@veramo/core": "^3.1.0", | ||
"@veramo/did-manager": "^3.1.0", | ||
"debug": "^4.1.1", | ||
"did-jwt-vc": "^2.1.7", | ||
"events": "^3.3.0", | ||
"micro-base58": "^0.5.1", | ||
"z-schema": "^5.0.2" | ||
}, | ||
"devDependencies": { | ||
"@types/debug": "4.1.7", | ||
"did-resolver": "3.1.0", | ||
"typescript": "4.4.3" | ||
}, | ||
"files": [ | ||
"dist/**/*", | ||
"plugin.schema.json", | ||
"README.md", | ||
"LICENSE" | ||
], | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"repository": "[email protected]:Sphereon-OpenSource/ssi-sdk.git", | ||
"author": "Sphereon <[email protected]>", | ||
"license": "Apache-2.0", | ||
"keywords": [] | ||
} |
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 @@ | ||
export * from './utils' |
31 changes: 31 additions & 0 deletions
31
packages/ssi-sdk-core/src/utils/__tests__/encoding.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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import {hexToMultibase, MultibaseFormat, multibaseToHex} from "../encoding"; | ||
|
||
|
||
describe('@sphereon/ssi-sdk-core:encoding', () => { | ||
const BASE58_EXAMPLE = 'C3CPq7c8PY'; | ||
const MULTIBASE_EXAMPLE = `z${BASE58_EXAMPLE}`; | ||
const HEX_EXAMPLE = '0123456789abcdef'; | ||
|
||
// Hex to multibase | ||
it('should encode hex to multibase base58', () => { | ||
expect(hexToMultibase(HEX_EXAMPLE, MultibaseFormat.BASE58)).toEqual({value: BASE58_EXAMPLE, format: MultibaseFormat.BASE58}) | ||
expect(hexToMultibase(HEX_EXAMPLE.toUpperCase(), MultibaseFormat.BASE58)).toEqual({value: BASE58_EXAMPLE, format: MultibaseFormat.BASE58}) | ||
}) | ||
it('should not encode hex to not supported multibase format', () => { | ||
expect(() => hexToMultibase(HEX_EXAMPLE, 'e' as never)).toThrowError() | ||
}) | ||
it('should not encode hex to not multibase base58 with no input', () => { | ||
expect(() => hexToMultibase(undefined as never, MultibaseFormat.BASE58)).toThrowError() | ||
}) | ||
|
||
// Multibase to hex | ||
it('should encode multibase base58 to hex', () => { | ||
expect(multibaseToHex(MULTIBASE_EXAMPLE)).toEqual({value: HEX_EXAMPLE, format: MultibaseFormat.BASE58}) | ||
}) | ||
it('should not encode unsupported multibase encoding to hex', () => { | ||
expect(() => multibaseToHex(`e${BASE58_EXAMPLE}`)).toThrowError() | ||
}) | ||
it('should not encode multibase base58 encoding to hex with no input', () => { | ||
expect(() => multibaseToHex(undefined as never)).toThrowError() | ||
}) | ||
}) |
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,35 @@ | ||
import * as base58 from 'micro-base58' | ||
|
||
|
||
export enum MultibaseFormat { | ||
BASE58 = 'z' | ||
} | ||
|
||
export function hexToMultibase(hex: string, format: MultibaseFormat): { value: string, format: MultibaseFormat } { | ||
if (format !== MultibaseFormat.BASE58) { | ||
throw new Error('Only base58 supported for now using multibase!') | ||
} | ||
return {value: base58.encode(hexToBytes(hex)), format} | ||
|
||
function hexToBytes(hex: string): Uint8Array { | ||
let bytes: number[] = [] | ||
for (let c = 0; c < hex.length; c += 2) bytes.push(parseInt(hex.substr(c, 2), 16)) | ||
return Uint8Array.of(...bytes) | ||
} | ||
} | ||
|
||
export function multibaseToHex(multibase: string): { value: string, format: MultibaseFormat } { | ||
if (!multibase.startsWith(MultibaseFormat.BASE58)) { | ||
throw new Error('Only base58 supported for now using multibase!') | ||
} | ||
return {value: bytesToHex(base58.decode(multibase.substr(1), 'btc')), format: MultibaseFormat.BASE58} | ||
|
||
function bytesToHex(uint8a: Uint8Array): string { | ||
// pre-caching chars could speed this up 6x. | ||
let hex = '' | ||
for (let i = 0; i < uint8a.length; i++) { | ||
hex += uint8a[i].toString(16).padStart(2, '0') | ||
} | ||
return hex | ||
} | ||
} |
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 @@ | ||
export { hexToMultibase, multibaseToHex } from './encoding' |
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,7 @@ | ||
{ | ||
"extends": "../tsconfig-base.json", | ||
"compilerOptions": { | ||
"rootDir": "src", | ||
"outDir": "dist" | ||
} | ||
} |