Skip to content

Commit

Permalink
Merge pull request #17 from Tonomy-Foundation/feature/fetch-option
Browse files Browse the repository at this point in the history
Feature/fetch option
  • Loading branch information
theblockstalk authored Jul 26, 2024
2 parents 2f7cb2d + 9c1f72a commit 41c74c1
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 19 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,6 @@
"cross-fetch": "^4.0.0",
"did-jwt": "^8.0.4",
"did-resolver": "^4.1.0"
}
},
"packageManager": "[email protected]+sha1.4ba7fc5c6e704fce2066ecbfb0b0d8976fe62447"
}
13 changes: 9 additions & 4 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { APIClient, FetchProvider } from '@wharfkit/antelope';
import fetch from 'cross-fetch';
import { FetchType } from './types';

export function getApi(url: string): APIClient {
export interface GetApiOptions {
url: string;
fetch?: FetchType;
}
export function getApi(options: GetApiOptions): APIClient {
const provider = options.fetch ? new FetchProvider(options.url, { fetch: options.fetch }) : new FetchProvider(options.url);
return new APIClient({
url,
provider: new FetchProvider(url, { fetch }),
url: options.url,
provider,
});
}
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ResolverRegistry } from 'did-resolver';
import {
createResolver,
} from './resolver';
import { AntelopeDIDResolutionOptions } from './types';
export {
antelopeChainRegistry,
checkDID,
Expand All @@ -15,6 +16,6 @@ export {

export * from './utils';

export function getResolver(options?: { antelopeChainUrl?: string; fetch?: () => Promise<any> }): ResolverRegistry {
export function getResolver(options?: AntelopeDIDResolutionOptions): ResolverRegistry {
return { eosio: createResolver(options), antelope: createResolver(options) } as any;
}
17 changes: 10 additions & 7 deletions src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { PermissionLevelWeight } from '@wharfkit/antelope/src/chain/authority';
import { Entry, Registry, MethodId, VerificationMethod, AntelopeDIDResolutionOptions } from './types';
import { createJWK, getCurveNamesFromType } from './utils';
import antelopeChainRegistry from './antelope-did-chain-registry.json';
import { getApi } from './api';
import { GetApiOptions, getApi } from './api';

const PATTERN_ACCOUNT_NAME = `([a-z1-5.]{0,12}[a-z1-5])`;
const PATTERN_CHAIN_ID = `([A-Fa-f0-9]{64})`;
Expand Down Expand Up @@ -76,25 +76,28 @@ export async function fetchAccount(

if (options.antelopeChainUrl) {
return await createRpcFetchAccount(methodId, {
serviceEndpoint: options.antelopeChainUrl,
url: options.antelopeChainUrl,
fetch: options.fetch,
});
}

const services = findServices(methodId.chain.service, serviceType);

for (const service of services as any) {
return await createRpcFetchAccount(methodId, service);
return await createRpcFetchAccount(methodId, {
url: service.serviceEndpoint,
fetch: options.fetch
});
}

return null;
}

async function createRpcFetchAccount(methodId: MethodId, service: any): Promise<AccountObject | null> {
const endpoint = service.serviceEndpoint;

async function createRpcFetchAccount(methodId: MethodId, options: GetApiOptions): Promise<AccountObject | null> {
try {
// @ts-expect-error AccountObject is not assignable to @wharfkit/antelope AccountObject
return await getApi(endpoint).v1.chain.get_account(methodId.subject);
return await getApi(options).v1.chain.get_account(methodId.subject);
} catch (e) {
if (e instanceof APIError && e.message.startsWith('Account Query Exception at /v1/chain/get_account')) {
return null;
Expand Down Expand Up @@ -249,7 +252,7 @@ export async function resolve(
};
}

export function createResolver(options: { antelopeChainUrl?: string } = {}): any {
export function createResolver(options: AntelopeDIDResolutionOptions = {}): any {
return function (
did: string,
parsed: ParsedDID,
Expand Down
7 changes: 5 additions & 2 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ interface Entry {
service: Service[];
}


type FetchType = (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;

export interface AntelopeDIDResolutionOptions extends DIDResolutionOptions {
fetch?: any;
antelopeChainRegistry: Registry;
antelopeChainUrl?: string;
fetch?: FetchType;
antelopeChainRegistry?: Registry;
}

export interface Registry {
Expand Down
18 changes: 16 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
import { PublicKey, KeyType, PrivateKey } from '@wharfkit/antelope';
import { PublicKey, KeyType, PrivateKey, NameType } from '@wharfkit/antelope';
import { secp256k1 } from '@noble/curves/secp256k1'
import { p256 } from '@noble/curves/p256'
import { ProjPointType } from '@noble/curves/abstract/weierstrass';
import { bytesToBase64url, bytesToHex, ES256KSigner, ES256Signer, hexToBytes, Signer } from 'did-jwt';
import { Issuer } from 'did-jwt-vc';

/**
* Create a Antelope DID string
*
* @param account {NameType} the account name e.g. "myaccount"
* @param chain {string} the chain name or chain id
* @returns {string} the Antelope DID string
*
* @example createDid("myaccount", "telos") // returns "did:antelope:telos:myaccount"
* @example createDid("myaccount", "eos:testnet:jungle") // returns "did:antelope:eos:testnet:jungle:myaccount"
* @example createDid("myaccount", "2a02a0053e5a8cf73a56ba0fda11e4d92e0238a4a2aa74fccf46d5a910746840") // returns "did:antelope:2a02a0053e5a8cf73a56ba0fda11e4d92e0238a4a2aa74fccf46d5a910746840:myaccount"
*/
export function createDid(account: NameType, chain: string): string {
return `did:antelope:${chain}:${account.toString()}`;
}

export function createSigner(privateKey: PrivateKey): Signer {
if (privateKey.type === KeyType.K1) {
return ES256KSigner(privateKey.data.array);
Expand Down Expand Up @@ -41,7 +56,6 @@ export interface JWK {
x: string;
y: string;
kid: string;

}

export function createJWK(publicKey: PublicKey): JWK {
Expand Down
4 changes: 2 additions & 2 deletions test/vc.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Resolver } from 'did-resolver';
import { createIssuer, getResolver } from '../src/index';
import { createDid, createIssuer, getResolver } from '../src/index';
import { jest } from '@jest/globals';
import { JwtCredentialPayload, createVerifiableCredentialJwt, verifyCredential } from 'did-jwt-vc'
import { PrivateKey } from '@wharfkit/antelope';
Expand All @@ -10,7 +10,7 @@ describe('VC tests', () => {
const resolver = new Resolver(getResolver());
const accountName = "mytest123tes";
const privateKey = PrivateKey.from("PVT_K1_2Yn362S23hWaDuDjLawDB1ZByF8fqsZZXFUDPTHnk6tXX44D2R");
const did = `did:antelope:eos:testnet:jungle:${accountName}`;
const did = createDid(accountName, "eos:testnet:jungle");
const issuer = createIssuer(did, privateKey);

it('create and verify VC', async () => {
Expand Down

0 comments on commit 41c74c1

Please sign in to comment.