Skip to content

Commit

Permalink
Merge pull request #16 from Tonomy-Foundation/feature/upgrade-all-pac…
Browse files Browse the repository at this point in the history
…kages

Add utility libraries and use correct signers and algorithms
  • Loading branch information
theblockstalk authored Jul 7, 2024
2 parents aa9daee + 5a4d12e commit 330397b
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 57 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ jobs:
- name: 📦 Install
uses: actions/setup-node@v3
with:
node-version: 20.9.0
node-version: 22.3.0
registry-url: https://registry.npmjs.org/

- name: 🚀 Build and publish SDK
uses: actions/setup-node@v3
with:
node-version: 20.9.0
node-version: 22.3.0
- run: yarn install --immutable
- run: yarn run release
env:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ jobs:
- name: Run lint and test
uses: actions/setup-node@v3
with:
node-version: 20.9.0
node-version: 22.3.0
- run: yarn install --immutable
- run: yarn run test
1 change: 1 addition & 0 deletions jest.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const config = {
{
useESM: true,
tsconfig: './tsconfig.json',
diagnostics: process.env.CI ? true : false
},
],
},
Expand Down
17 changes: 5 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { ResolverRegistry } from 'did-resolver';
import {
createResolver,
} from './resolver';
export {
antelopeChainRegistry,
checkDID,
fetchAccount,
Expand All @@ -8,20 +11,10 @@ import {
REGEX_CHAIN_NAME,
CONDITIONAL_PROOF_2022,
createDIDDocument,
createResolver,
} from './resolver';

export * from './utils';

export function getResolver(options?: { antelopeChainUrl?: string; fetch?: () => Promise<any> }): ResolverRegistry {
return { eosio: createResolver(options), antelope: createResolver(options) } as any;
}

export {
antelopeChainRegistry,
REGEX_ACCOUNT_NAME,
REGEX_CHAIN_ID,
REGEX_CHAIN_NAME,
CONDITIONAL_PROOF_2022,
createDIDDocument,
checkDID,
fetchAccount,
};
12 changes: 4 additions & 8 deletions src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,16 @@ function findServices(service: Array<ServiceEndpoint>, type: string): Array<Serv
return service.filter((s: any) => (Array.isArray(s.type) ? s.type.includes(type) : s.type === type));
}

function createKeyMethod(baseId: string, i: number, did: string, key: PublicKey): VerificationMethod {
const pubKey = key;

const publicKeyJwk = createJWK(pubKey);
function createKeyMethod(baseId: string, i: number, did: string, pubKey: PublicKey): VerificationMethod {
const { verificationMethodType } = getCurveNamesFromType(pubKey);

const keyMethod: VerificationMethod = {
return {
id: baseId + '-' + i,
controller: did,
type: verificationMethodType,
publicKeyJwk,
// NOTE: could also use `util.publicKeyHex(pubKey)` instead, but it is handy to have the kid property of the JWK for debugging
publicKeyJwk: createJWK(pubKey),
};

return keyMethod;
}

function createAccountMethod(
Expand Down
54 changes: 45 additions & 9 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,51 @@
import { PublicKey, PrivateKey, KeyType } from '@wharfkit/antelope';
import { PublicKey, KeyType, PrivateKey } from '@wharfkit/antelope';
import { secp256k1 } from '@noble/curves/secp256k1'
import { p256 } from '@noble/curves/p256'
import { ProjPointType } from '@noble/curves/abstract/weierstrass';
import { bytesToBase64url, hexToBytes } from 'did-jwt';
import { bytesToBase64url, bytesToHex, ES256KSigner, ES256Signer, hexToBytes, Signer } from 'did-jwt';
import { Issuer } from 'did-jwt-vc';

export function createSigner(privateKey: PrivateKey): Signer {
if (privateKey.type === KeyType.K1) {
return ES256KSigner(privateKey.data.array);
}

if (privateKey.type === KeyType.R1 || privateKey.type === KeyType.WA) {
return ES256Signer(privateKey.data.array);
}

throw new Error('Unsupported key type');
}

export function createIssuer(did: string, privateKey: PrivateKey): Issuer {
return {
did,
signer: createSigner(privateKey),
alg: privateKey.type === KeyType.K1 ? 'ES256K' : 'ES256',
}
}

export function toPublicKeyHex(publicKey: PublicKey): string {
return bytesToHex(publicKey.data.array);
}

// Cannot import the following, so copying here
// import { bigintToBytes } from '../node_modules/did-jwt/src/util';
function bigintToBytes(n: bigint, minLength?: number): Uint8Array {
return hexToBytes(n.toString(16), minLength)
}

export function createJWK(publicKey: PublicKey) {
const { jwkCurve } = getCurveNamesFromType(publicKey);
export interface JWK {
crv: string;
kty: string;
x: string;
y: string;
kid: string;

}

export function createJWK(publicKey: PublicKey): JWK {
const { jwkCurve } = getCurveNamesFromType(publicKey);
// copied from:
// secp256k1 - https://github.com/decentralized-identity/did-jwt/pull/280/files#diff-c94d78b633b2fe38397047def271342090b3c8d81adf022a3a745af6d7b1c845R197
// P-256 - https://github.com/decentralized-identity/did-jwt/pull/280/files#diff-c94d78b633b2fe38397047def271342090b3c8d81adf022a3a745af6d7b1c845R57
Expand All @@ -26,7 +59,10 @@ export function createJWK(publicKey: PublicKey) {
}
}

export function getCurveNamesFromType(key: PublicKey): { jwkCurve: "secp256k1" | "P-256"; verificationMethodType: string } {
export function getCurveNamesFromType(key: PublicKey): {
jwkCurve: "secp256k1" | "P-256";
verificationMethodType: "EcdsaSecp256k1VerificationKey2019" | "JsonWebKey2020"
} {
const type = key.type;

switch (type) {
Expand All @@ -44,13 +80,13 @@ export function getCurveNamesFromType(key: PublicKey): { jwkCurve: "secp256k1" |
}
}

export function toNobel(key: PublicKey | PrivateKey): ProjPointType<bigint> {
const { jwkCurve } = getCurveNamesFromType(key instanceof PublicKey ? key : key.toPublic());
export function toNobel(publicKey: PublicKey): ProjPointType<bigint> {
const { jwkCurve } = getCurveNamesFromType(publicKey);
switch (jwkCurve) {
case 'secp256k1':
return secp256k1.ProjectivePoint.fromHex(key.data.array);
return secp256k1.ProjectivePoint.fromHex(publicKey.data.array);
case 'P-256':
return p256.ProjectivePoint.fromHex(key.data.array);
return p256.ProjectivePoint.fromHex(publicKey.data.array);
default:
throw new Error(`Unsupported curve: ${jwkCurve}`);
}
Expand Down
28 changes: 3 additions & 25 deletions test/vc.test.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,11 @@
import { Resolver } from 'did-resolver';
import { getResolver } from '../src/index';
import { createIssuer, getResolver } from '../src/index';
import { jest } from '@jest/globals';
import { Issuer, JwtCredentialPayload, createVerifiableCredentialJwt, verifyCredential } from 'did-jwt-vc'
import { ES256KSigner, ES256Signer, Signer } from 'did-jwt';
import { KeyType, PrivateKey } from '@wharfkit/antelope';
import { JwtCredentialPayload, createVerifiableCredentialJwt, verifyCredential } from 'did-jwt-vc'
import { PrivateKey } from '@wharfkit/antelope';

jest.setTimeout(10000);

function createSigner(privateKey: PrivateKey): Signer {
if (privateKey.type === KeyType.K1) {
return ES256KSigner(privateKey.data.array, true);
}

if (privateKey.type === KeyType.R1 || privateKey.type === KeyType.WA) {
return ES256Signer(privateKey.data.array);
}

throw new Error('Unsupported key type');
}

function createIssuer(did: string, privateKey: PrivateKey): Issuer {
return {
did,
signer: createSigner(privateKey),
alg: 'ES256K-R',
}
}


describe('VC tests', () => {
const resolver = new Resolver(getResolver());
const accountName = "mytest123tes";
Expand Down

0 comments on commit 330397b

Please sign in to comment.