Skip to content

Commit

Permalink
fix prettier stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
HananINouman committed Jul 2, 2024
1 parent 42dbe44 commit 818fbfc
Show file tree
Hide file tree
Showing 14 changed files with 244 additions and 209 deletions.
8 changes: 4 additions & 4 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ export abstract class Base {
try {
const response = await fetch(url, config);
if (response.ok) {
return await response.json()
return await response.json();
} else {
const errorResponse = await response.json()
throw errorResponse
const errorResponse = await response.json();
throw errorResponse;
}
} catch (e: any) {
throw e
throw e;
}
}
}
29 changes: 16 additions & 13 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ export const EIP712_DOMAIN_NAME = 'Obol';
export const EIP712_DOMAIN_VERSION = '1';
export const CreatorConfigHashSigningTypes = {
CreatorConfigHash: [{ name: 'creator_config_hash', type: 'string' }],
}
};
export const TermsAndConditionsSigningTypes = {
TermsAndConditions: [
{ name: 'terms_and_conditions_hash', type: 'string' },
{ name: 'version', type: 'uint256' },
]
}
],
};

const EIP712Domain = [
{ name: 'name', type: 'string' },
Expand All @@ -26,12 +26,12 @@ export const Domain = (chainId?: number): TypedDataDomain => {
const typeDataDomain: any = {
name: EIP712_DOMAIN_NAME,
version: EIP712_DOMAIN_VERSION,
}
};
if (chainId) {
typeDataDomain.chainId = chainId
typeDataDomain.chainId = chainId;
}
return typeDataDomain
}
return typeDataDomain;
};

export const CreatorTypedMessage = {
EIP712Domain,
Expand Down Expand Up @@ -123,13 +123,16 @@ export enum DefinitionFlow {
Charon = 'Charon-Command',
}

export const DEFAULT_BASE_URL = 'https://api.obol.tech'
export const DEFAULT_BASE_VERSION = 'v1'
export const DEFAULT_CHAIN_ID = 17000
export const DEFAULT_BASE_URL = 'https://api.obol.tech';
export const DEFAULT_BASE_VERSION = 'v1';
export const DEFAULT_CHAIN_ID = 17000;

export const ETHER_TO_GWEI = 10 ** 9
export const ETHER_TO_GWEI = 10 ** 9;

export const TERMS_AND_CONDITIONS_VERSION = 1
export const TERMS_AND_CONDITIONS_URL = (TERMS_AND_CONDITIONS_VERSION === 1) ? 'https://obol.org/terms.pdf' : `https://obol.org/${TERMS_AND_CONDITIONS_VERSION as number}/terms.pdf`
export const TERMS_AND_CONDITIONS_VERSION = 1;
export const TERMS_AND_CONDITIONS_URL =
TERMS_AND_CONDITIONS_VERSION === 1
? 'https://obol.org/terms.pdf'
: `https://obol.org/${TERMS_AND_CONDITIONS_VERSION as number}/terms.pdf`;
export const TERMS_AND_CONDITIONS_HASH =
'0xd33721644e8f3afab1495a74abe3523cec12d48b8da6cb760972492ca3f1a273';
78 changes: 43 additions & 35 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ import {
TermsAndConditionsSigningTypes,
DEFAULT_BASE_VERSION,
TERMS_AND_CONDITIONS_HASH,
} from './constants.js'
import { ConflictError } from './errors.js'
} from './constants.js';
import { ConflictError } from './errors.js';
import {
type ClusterDefinition,
type ClusterLock,
type ClusterPayload,
type OperatorPayload,
} from './types.js'
import { clusterConfigOrDefinitionHash } from './verification/common.js'
import { validatePayload } from './ajv.js'
import { definitionSchema, operatorPayloadSchema } from './schema.js'
export * from './types.js'
export * from './services.js'
} from './types.js';
import { clusterConfigOrDefinitionHash } from './verification/common.js';
import { validatePayload } from './ajv.js';
import { definitionSchema, operatorPayloadSchema } from './schema.js';
export * from './types.js';
export * from './services.js';

/**
* Obol sdk Client can be used for creating, managing and activating distributed validators.
Expand Down Expand Up @@ -56,17 +56,19 @@ export class Client extends Base {
* An example of how to use acceptObolLatestTermsAndConditions:
* [acceptObolLatestTermsAndConditions](https://github.com/ObolNetwork/obol-sdk-examples/blob/main/TS-Example/index.ts#L44)
*/
async acceptObolLatestTermsAndConditions (): Promise<string> {
if (!this.signer) { throw new Error('Signer is required in acceptObolTermsAndConditions') }
async acceptObolLatestTermsAndConditions(): Promise<string> {
if (!this.signer) {
throw new Error('Signer is required in acceptObolTermsAndConditions');
}

try {
const termsAndConditionsHash = TERMS_AND_CONDITIONS_HASH
const address = await this.signer.getAddress()
const termsAndConditionsHash = TERMS_AND_CONDITIONS_HASH;
const address = await this.signer.getAddress();
const termsAndConditionsPayload = {
address,
version: TERMS_AND_CONDITIONS_VERSION,
terms_and_conditions_hash: termsAndConditionsHash
}
terms_and_conditions_hash: termsAndConditionsHash,
};

const termsAndConditionsSignature = await this.signer.signTypedData(
Domain(),
Expand All @@ -75,21 +77,22 @@ export class Client extends Base {
terms_and_conditions_hash: termsAndConditionsHash,
version: TERMS_AND_CONDITIONS_VERSION,
},
)
);

const termsAndConditionsResponse: { message: string, success: boolean } = await this.request(`/${DEFAULT_BASE_VERSION}/termsAndConditions`, {
method: 'POST',
body: JSON.stringify(termsAndConditionsPayload),
headers: {
Authorization: `Bearer ${termsAndConditionsSignature}`,
},
})
return termsAndConditionsResponse?.message
const termsAndConditionsResponse: { message: string; success: boolean } =
await this.request(`/${DEFAULT_BASE_VERSION}/termsAndConditions`, {
method: 'POST',
body: JSON.stringify(termsAndConditionsPayload),
headers: {
Authorization: `Bearer ${termsAndConditionsSignature}`,
},
});
return termsAndConditionsResponse?.message;
} catch (err: any) {
if (err?.message === CONFLICT_ERROR_MSG) {
throw new ConflictError()
throw new ConflictError();
}
throw err
throw err;
}
}

Expand Down Expand Up @@ -137,14 +140,17 @@ export class Client extends Base {
{ creator_config_hash: clusterConfig.config_hash },
);

const clusterDefinition: ClusterDefinition = await this.request(`/${DEFAULT_BASE_VERSION}/definition`, {
method: 'POST',
body: JSON.stringify(clusterConfig),
headers: {
Authorization: `Bearer ${creatorConfigSignature}`,
'fork-version': this.fork_version,
const clusterDefinition: ClusterDefinition = await this.request(
`/${DEFAULT_BASE_VERSION}/definition`,
{
method: 'POST',
body: JSON.stringify(clusterConfig),
headers: {
Authorization: `Bearer ${creatorConfigSignature}`,
'fork-version': this.fork_version,
},
},
});
);
return clusterDefinition?.config_hash;
} catch (err: any) {
if (err?.message === CONFLICT_ERROR_MSG) {
Expand All @@ -168,7 +174,9 @@ export class Client extends Base {
operatorPayload: OperatorPayload,
configHash: string,
): Promise<ClusterDefinition> {
if (!this.signer) { throw new Error('Signer is required in acceptClusterDefinition') }
if (!this.signer) {
throw new Error('Signer is required in acceptClusterDefinition');
}

validatePayload(operatorPayload, operatorPayloadSchema);

Expand All @@ -191,7 +199,7 @@ export class Client extends Base {
address,
enr_signature: operatorENRSignature,
fork_version: this.fork_version,
}
};
const clusterDefinition: ClusterDefinition = await this.request(
`/${DEFAULT_BASE_VERSION}/definition/${configHash}`,
{
Expand All @@ -216,7 +224,7 @@ export class Client extends Base {
* An example of how to use getClusterDefinition:
* [getObolClusterDefinition](https://github.com/ObolNetwork/obol-sdk-examples/blob/main/TS-Example/index.ts#L74)
*/
async getClusterDefinition (configHash: string): Promise<ClusterDefinition> {
async getClusterDefinition(configHash: string): Promise<ClusterDefinition> {
const clusterDefinition: ClusterDefinition = await this.request(
`/${DEFAULT_BASE_VERSION}/definition/${configHash}`,
{
Expand Down
20 changes: 10 additions & 10 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export type ClusterOperator = {

/** The operator configuration signature. */
config_signature?: string;
}
};

/**
* A partial view of `ClusterOperator` with `enr` and `version` as required properties.
Expand All @@ -52,7 +52,7 @@ export type ClusterCreator = {
address: string;
/** The cluster configuration signature. */
config_signature?: string;
}
};

/**
* Validator withdrawal configuration
Expand All @@ -63,7 +63,7 @@ export type ClusterValidator = {

/** The validator reward address. */
withdrawal_address: string;
}
};

/**
* Cluster configuration
Expand All @@ -80,7 +80,7 @@ export type ClusterPayload = {

/** The cluster partial deposits in gwei or 32000000000. */
deposit_amounts?: string[];
}
};

/**
* Cluster definition data needed for dkg
Expand Down Expand Up @@ -135,7 +135,7 @@ export type BuilderRegistrationMessage = {

/** The public key of the DV. */
pubkey: string;
}
};

/**
* Pre-generated Signed Validator Builder Registration
Expand All @@ -146,7 +146,7 @@ export type BuilderRegistration = {

/** BLS signature of the builder registration message. */
signature: string;
}
};

/**
* Required deposit data for validator activation
Expand All @@ -166,7 +166,7 @@ export type DepositData = {

/** BLS signature of the deposit message. */
signature: string;
}
};

/**
* Required deposit data for validator activation
Expand All @@ -186,14 +186,14 @@ export type DistributedValidator = {

/** pre-generated signed validator builder registration to be sent to builder network. */
builder_registration?: BuilderRegistration;
}
};

/**
* Cluster Details after DKG is complete
*/
export type ClusterLock = {
/** The cluster definition. */
cluster_definition: ClusterDefinition
cluster_definition: ClusterDefinition;

/** The cluster distributed validators. */
distributed_validators: DistributedValidator[];
Expand All @@ -206,4 +206,4 @@ export type ClusterLock = {

/** Node Signature for the lock hash by the node secp256k1 key. */
node_signatures?: string[];
}
};
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DefinitionFlow } from './constants'
import { type ClusterDefinition } from './types'
import { DefinitionFlow } from './constants';
import { type ClusterDefinition } from './types';

export const hexWithout0x = (hex: string): string => {
return hex.slice(2, hex.length);
Expand Down
8 changes: 4 additions & 4 deletions src/verification/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ import {
* @returns The config hash or the definition hash in of the corresponding cluster
*/
export const clusterConfigOrDefinitionHash = (
cluster: ClusterDefinition,
configOnly: boolean,
cluster: ClusterDefinition,
configOnly: boolean,
): string => {
let definitionType, val;

Expand Down Expand Up @@ -176,8 +176,8 @@ const getEnrSigner = (
};

const verifyDefinitionSignatures = (
clusterDefinition: ClusterDefinition,
definitionType: DefinitionFlow,
clusterDefinition: ClusterDefinition,
definitionType: DefinitionFlow,
): boolean => {
if (definitionType === DefinitionFlow.Charon) {
return true;
Expand Down
30 changes: 16 additions & 14 deletions src/verification/termsAndConditions.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
import pdf from 'pdf-parse'
import { ByteListType, ContainerType } from '@chainsafe/ssz'
import { TERMS_AND_CONDITIONS_URL } from '../constants'
import { strToUint8Array } from '../utils'
import pdf from 'pdf-parse';
import { ByteListType, ContainerType } from '@chainsafe/ssz';
import { TERMS_AND_CONDITIONS_URL } from '../constants';
import { strToUint8Array } from '../utils';

export const hashTermsAndConditions = async (): Promise<string | null> => {
try {
// read the pdf
const response = await fetch(TERMS_AND_CONDITIONS_URL)
const pdfBuffarrayBuffer = await response.arrayBuffer()
const pdfBuffer = Buffer.from(pdfBuffarrayBuffer)
const data = await pdf(pdfBuffer)
const response = await fetch(TERMS_AND_CONDITIONS_URL);
const pdfBuffarrayBuffer = await response.arrayBuffer();
const pdfBuffer = Buffer.from(pdfBuffarrayBuffer);
const data = await pdf(pdfBuffer);

// ssz hash
const termsType = new ContainerType({
terms_and_conditions_hash: new ByteListType(Number.MAX_SAFE_INTEGER),
})
});

const termsHasVal = termsType.defaultValue()
const termsHasVal = termsType.defaultValue();

termsHasVal.terms_and_conditions_hash = strToUint8Array(data?.text.replace(/[^a-zA-Z0-9]/g, ''))
termsHasVal.terms_and_conditions_hash = strToUint8Array(
data?.text.replace(/[^a-zA-Z0-9]/g, ''),
);

return (
'0x' +
Buffer.from(termsType.hashTreeRoot(termsHasVal).buffer).toString('hex')
)
);
} catch (err) {
return null
return null;
}
}
};
4 changes: 2 additions & 2 deletions src/verification/v1.6.0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ export const clusterDefinitionContainerTypeV1X6 = (
};

export const hashClusterDefinitionV1X6 = (
cluster: ClusterDefinition,
configOnly: boolean,
cluster: ClusterDefinition,
configOnly: boolean,
): ValueOfFields<DefinitionFieldsV1X6> => {
const definitionType = clusterDefinitionContainerTypeV1X6(configOnly);

Expand Down
Loading

0 comments on commit 818fbfc

Please sign in to comment.