Skip to content

Commit

Permalink
Merge pull request #27 from Polymarket/feat/new-api-keys
Browse files Browse the repository at this point in the history
Feat: new api key structure
  • Loading branch information
JonathanAmenechi authored Apr 28, 2022
2 parents 41f09ea + 63877df commit edbb79b
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 8 deletions.
23 changes: 23 additions & 0 deletions examples/deriveApiKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ethers } from "ethers";
import { config as dotenvConfig } from "dotenv";
import { resolve } from "path";
import { ClobClient } from "../src";

dotenvConfig({ path: resolve(__dirname, "../.env") });

async function main() {
const provider = new ethers.providers.JsonRpcProvider(process.env.RPC_URL);
const pk = new ethers.Wallet(`${process.env.PK}`);
const wallet = pk.connect(provider);
console.log(`Address: ${await wallet.getAddress()}`);

const host = "http://localhost:8080";
const clobClient = new ClobClient(host, wallet);

console.log(`Response: `);
const resp = await clobClient.deriveApiKey();
console.log(resp);
console.log(`Complete!`);
}

main();
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@polymarket/clob-client",
"description": "Typescript client for Polymarket's CLOB",
"version": "1.0.21",
"version": "1.0.22",
"contributors": [
{
"name": "Jonathan Amenechi",
Expand Down
27 changes: 23 additions & 4 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
OrderHistory,
} from "./types";
import { createL1Headers, createL2Headers } from "./headers";
import { CREDS_CREATION_WARNING } from "./constants";
import { del, DELETE, GET, get, POST, post } from "./http_helpers";
import { L1_AUTH_UNAVAILABLE_ERROR, L2_AUTH_NOT_AVAILABLE } from "./errors";
import { marketOrderToJson, limitOrderToJson, addQueryParamsToUrl } from "./utilities";
Expand All @@ -35,6 +34,7 @@ import {
PRICE,
OPEN_ORDERS,
ORDER_HISTORY,
DERIVE_API_KEY,
} from "./endpoints";
import { OrderBuilder } from "./order-builder/builder";

Expand Down Expand Up @@ -92,13 +92,32 @@ export class ClobClient {
}

// L1 Authed
public async createApiKey(): Promise<ApiKeyCreds> {

/**
* Creates a new API key for a user
* @param nonce
* @returns ApiKeyCreds
*/
public async createApiKey(nonce?: number): Promise<ApiKeyCreds> {
this.canL1Auth();

const endpoint = `${this.host}${CREATE_API_KEY}`;
const headers = await createL1Headers(this.signer as Wallet | JsonRpcSigner);
const headers = await createL1Headers(this.signer as Wallet | JsonRpcSigner, nonce);
const resp = await post(endpoint, headers);
console.log(CREDS_CREATION_WARNING);
return resp;
}

/**
* Derives an existing API key for a user
* @param nonce
* @returns ApiKeyCreds
*/
public async deriveApiKey(nonce?: number): Promise<ApiKeyCreds> {
this.canL1Auth();

const endpoint = `${this.host}${DERIVE_API_KEY}`;
const headers = await createL1Headers(this.signer as Wallet | JsonRpcSigner, nonce);
const resp = await get(endpoint, headers);
return resp;
}

Expand Down
2 changes: 2 additions & 0 deletions src/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export const GET_API_KEYS = "/get-api-keys";

export const DELETE_API_KEY = "/delete-api-key";

export const DERIVE_API_KEY = "/derive-api-key";

export const TRADE_HISTORY = "/trade-history";

export const ORDER_HISTORY = "/order-history";
Expand Down
10 changes: 8 additions & 2 deletions src/headers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@ import { Wallet } from "@ethersproject/wallet";
import { buildClobEip712Signature, buildPolyHmacSignature } from "../signing";
import { ApiKeyCreds, L1PolyHeader, L2HeaderArgs, L2PolyHeader } from "../types";

export const createL1Headers = async (signer: Wallet | JsonRpcSigner): Promise<L1PolyHeader> => {
export const createL1Headers = async (signer: Wallet | JsonRpcSigner, nonce?: number): Promise<L1PolyHeader> => {
const now = Math.floor(Date.now() / 1000);
const sig = await buildClobEip712Signature(signer, now);
let n = 0; // Default nonce is 0
if (nonce !== undefined) {
n = nonce;
}

const sig = await buildClobEip712Signature(signer, now, n);
const address = await signer.getAddress();

const headers = {
POLY_ADDRESS: address,
POLY_SIGNATURE: sig,
POLY_TIMESTAMP: `${now}`,
POLY_NONCE: `${n}`,
};
return headers;
};
Expand Down
8 changes: 7 additions & 1 deletion src/signing/eip712.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import { MSG_TO_SIGN } from "./constants";
* @param ts
* @returns string
*/
export const buildClobEip712Signature = async (signer: Wallet | JsonRpcSigner, timestamp: number): Promise<string> => {
export const buildClobEip712Signature = async (
signer: Wallet | JsonRpcSigner,
timestamp: number,
nonce: number,
): Promise<string> => {
const address = await signer.getAddress();
const chainID = await signer.getChainId();
const ts = `${timestamp}`;
Expand All @@ -24,12 +28,14 @@ export const buildClobEip712Signature = async (signer: Wallet | JsonRpcSigner, t
ClobAuth: [
{ name: "address", type: "address" },
{ name: "timestamp", type: "string" },
{ name: "nonce", type: "uint256" },
{ name: "message", type: "string" },
],
};
const value = {
address,
timestamp: ts,
nonce,
message: MSG_TO_SIGN,
};
// eslint-disable-next-line no-underscore-dangle
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface L1PolyHeader {
POLY_ADDRESS: string;
POLY_SIGNATURE: string;
POLY_TIMESTAMP: string;
POLY_NONCE: string;
}

// API key verification
Expand Down

0 comments on commit edbb79b

Please sign in to comment.