Skip to content

Commit

Permalink
chore: update deps, remove cycle from tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nklomp committed Sep 2, 2023
1 parent 80dd076 commit 1408e15
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 22 deletions.
1 change: 0 additions & 1 deletion packages/ssi-sdk-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
},
"dependencies": {
"uint8arrays": "3.1.1",
"multiformats": "^11.0.2",
"@sphereon/ssi-types": "workspace:^",
"@veramo/core": "4.2.0",
"cross-fetch": "^3.1.8",
Expand Down
16 changes: 6 additions & 10 deletions packages/ssi-sdk-core/src/utils/encoding.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { TKeyType } from '@veramo/core'
import { varint } from 'multiformats'
import { base58btc } from 'multiformats/bases/base58'
import { concat as concatArrays, fromString, toString } from 'uint8arrays'
import varint from './varint/varint'

export enum MultibaseFormat {
BASE58 = 'z',
Expand Down Expand Up @@ -199,7 +198,7 @@ export function multibaseKeyToBytes(s: string): Uint8Array {
if (s.charAt(0) !== 'z') {
throw new Error('invalid multibase string: string is not base58 encoded (does not start with "z")')
}
const bytes = base58btc.decode(s)
const bytes = u8a.fromString(s.substring(1), 'base58btc')
const props = multibaseKeyToProps(s)
const keyLength = props.code.length / 2

Expand All @@ -222,11 +221,8 @@ export function multibaseKeyToProps(s: string): MultiCodedLookup {
throw new Error('invalid multibase string: string is not base58 encoded (does not start with "z")')
}

const bytes = base58btc.decode(s)
const vint = varint.decode(bytes)
// console.log(JSON.stringify(vint))

const code = vint[0]
const bytes = u8a.fromString(s.substring(1), 'base58btc')
const code = varint.decode(bytes)
return getMultibasePropsByCode(code)
}

Expand Down Expand Up @@ -282,13 +278,13 @@ export function bytesToMultibase(byteArray: Uint8Array, type: TKeyType): string
const varCode = Number.parseInt(props.code, 16)
const length = props.code.length / 2
const varType = new Uint8Array(length)
varint.encodeTo(varCode, varType)
varint.encode(varCode, varType)
const bytes = u8a.concatArrays([
varType,
props.keyType === 'RSA' || props.keyType === 'Secp256r1' ? new Uint8Array(0) : u8a.fromString(multicodec, 'base16'),
byteArray,
])
return base58btc.encode(bytes)
return 'z' + u8a.toString(bytes, 'base58btc')
}

/**
Expand Down
49 changes: 49 additions & 0 deletions packages/ssi-sdk-core/src/utils/varint/varint.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Type definitions for varint 5.0
// Project: https://github.com/chrisdickinson/varint#readme
// Definitions by: David Brockman Smoliansky <https://github.com/dbrockman>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped

interface Varint {
encode: {
/**
* Encodes `num` into `buffer` starting at `offset`. returns `buffer`, with the encoded varint written into it.
* `varint.encode.bytes` will now be set to the number of bytes modified.
*/
(num: number, buffer: Uint8Array, offset?: number): Buffer

/**
* Encodes `num` into `array` starting at `offset`. returns `array`, with the encoded varint written into it.
* If `array` is not provided, it will default to a new array.
* `varint.encode.bytes` will now be set to the number of bytes modified.
*/
(num: number, array?: number[], offset?: number): number[]

/**
* Similar to `decode.bytes` when encoding a number it can be useful to know how many bytes where written (especially if you pass an output array).
* You can access this via `varint.encode.bytes` which holds the number of bytes written in the last encode.
*/
bytes: number
}

decode: {
/**
* Decodes `data`, which can be either a buffer or array of integers, from position `offset` or default 0 and returns the decoded original integer.
* Throws a `RangeError` when `data` does not represent a valid encoding.
*/
(buf: Uint8Array | number[], offset?: number): number

/**
* If you also require the length (number of bytes) that were required to decode the integer you can access it via `varint.decode.bytes`.
* This is an integer property that will tell you the number of bytes that the last .decode() call had to use to decode.
*/
bytes: number
}

/**
* returns the number of bytes this number will be encoded as, up to a maximum of 8.
*/
encodingLength(num: number): number
}

declare const varint: Varint
export default varint
96 changes: 96 additions & 0 deletions packages/ssi-sdk-core/src/utils/varint/varint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
var encode_1 = encode

var MSB = 0x80,
REST = 0x7f,
MSBALL = ~REST,
INT = Math.pow(2, 31)

function encode(num, out, offset) {
out = out || []
offset = offset || 0
var oldOffset = offset

while (num >= INT) {
out[offset++] = (num & 0xff) | MSB
num /= 128
}
while (num & MSBALL) {
out[offset++] = (num & 0xff) | MSB
num >>>= 7
}
out[offset] = num | 0

encode.bytes = offset - oldOffset + 1

return out
}

var decode = read

var MSB$1 = 0x80,
REST$1 = 0x7f

function read(buf, offset) {
var res = 0,
offset = offset || 0,
shift = 0,
counter = offset,
b,
l = buf.length

do {
if (counter >= l) {
read.bytes = 0
throw new RangeError('Could not decode varint')
}
b = buf[counter++]
res += shift < 28 ? (b & REST$1) << shift : (b & REST$1) * Math.pow(2, shift)
shift += 7
} while (b >= MSB$1)

read.bytes = counter - offset

return res
}

var N1 = Math.pow(2, 7)
var N2 = Math.pow(2, 14)
var N3 = Math.pow(2, 21)
var N4 = Math.pow(2, 28)
var N5 = Math.pow(2, 35)
var N6 = Math.pow(2, 42)
var N7 = Math.pow(2, 49)
var N8 = Math.pow(2, 56)
var N9 = Math.pow(2, 63)

var length = function (value) {
return value < N1
? 1
: value < N2
? 2
: value < N3
? 3
: value < N4
? 4
: value < N5
? 5
: value < N6
? 6
: value < N7
? 7
: value < N8
? 8
: value < N9
? 9
: 10
}

var varint = {
encode: encode_1,
decode: decode,
encodingLength: length,
}

var _brrp_varint = varint

export default _brrp_varint
12 changes: 4 additions & 8 deletions packages/vc-handler-ld-local/src/__tests__/statuslist.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
import {
CredentialHandlerLDLocal,
ICredentialHandlerLDLocal,
LdDefaultContexts,
MethodNames,
SphereonEd25519Signature2018,
SphereonEd25519Signature2020,
} from 'vc-handler-ld-local/src/index'
import { createAgent, ICredentialPlugin, IDIDManager, IIdentifier, IKeyManager, IResolver, TAgent } from '@veramo/core'
import { CredentialPlugin, ICredentialIssuer } from '@veramo/credential-w3c'
import { DIDManager, MemoryDIDStore } from '@veramo/did-manager'
Expand All @@ -18,6 +10,10 @@ import { Resolver } from 'did-resolver'
// @ts-ignore
import nock from 'nock'
import { createNewStatusList } from '@sphereon/ssi-sdk.vc-status-list'
import {CredentialHandlerLDLocal} from "../agent";
import {LdDefaultContexts} from "../ld-default-contexts";
import {SphereonEd25519Signature2018, SphereonEd25519Signature2020} from "../suites";
import {ICredentialHandlerLDLocal, MethodNames} from "../types";

jest.setTimeout(100000)

Expand Down
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1408e15

Please sign in to comment.