Skip to content

Commit

Permalink
feat: createIssuer() util
Browse files Browse the repository at this point in the history
  • Loading branch information
theblockstalk committed Jul 7, 2024
1 parent 3678e75 commit e17923f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 27 deletions.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ export {
CONDITIONAL_PROOF_2022,
createDIDDocument,
} from './resolver';

export * from './utils';

export function getResolver(options?: { antelopeChainUrl?: string; fetch?: () => Promise<any> }): ResolverRegistry {
return { eosio: createResolver(options), antelope: createResolver(options) } as any;
}
25 changes: 23 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
import { PublicKey, 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, 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',
}
}

// Cannot import the following, so copying here
// import { bigintToBytes } from '../node_modules/did-jwt/src/util';
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 e17923f

Please sign in to comment.