diff --git a/ts/examples/keys/owner.json b/ts/examples/keys/owner.json index aaf3bae..6efbded 100644 --- a/ts/examples/keys/owner.json +++ b/ts/examples/keys/owner.json @@ -1 +1 @@ -[63,119,147,199,137,61,136,169,123,210,247,207,78,58,122,92,55,60,132,202,209,139,39,68,2,30,223,35,162,46,222,138,74,163,230,64,70,155,29,186,90,31,150,80,228,105,44,190,124,110,139,226,110,161,225,203,65,63,155,88,252,125,157,33] \ No newline at end of file +[57,148,214,188,143,219,211,199,225,245,55,202,253,17,223,236,32,230,237,163,195,6,34,129,37,135,148,101,4,186,199,99,43,62,254,163,2,193,37,219,162,74,188,92,115,219,111,88,223,149,203,13,71,113,6,83,106,63,228,63,102,198,107,225] \ No newline at end of file diff --git a/ts/examples/marketFilter.ts b/ts/examples/marketFilter.ts index 61770d7..918edfe 100644 --- a/ts/examples/marketFilter.ts +++ b/ts/examples/marketFilter.ts @@ -11,7 +11,7 @@ import { Dex, DexMarket, FileKeypair } from "../src"; const main = async () => { const connection = new Connection("http://localhost:8899", "confirmed"); - const owner = FileKeypair.generate("./scripts/keys/owner.json"); + const owner = FileKeypair.loadOrGenerate("./examples/keys/owner.json"); console.log("Owner: ", owner.keypair.publicKey.toString()); const airdropSig = await connection.requestAirdrop( diff --git a/ts/examples/marketMaking.ts b/ts/examples/marketMaking.ts index 814fb5d..1563fdd 100644 --- a/ts/examples/marketMaking.ts +++ b/ts/examples/marketMaking.ts @@ -7,7 +7,7 @@ const main = async () => { console.log("Process: ", process.pid); const connection = new Connection("http://localhost:8899", "confirmed"); - const owner = FileKeypair.generate("./scripts/keys/owner.json"); + const owner = FileKeypair.loadOrGenerate("./scripts/keys/owner.json"); console.log("Owner: ", owner.keypair.publicKey.toString()); const airdropSig = await connection.requestAirdrop( diff --git a/ts/src/dex.ts b/ts/src/dex.ts index 80583ce..9b3b130 100644 --- a/ts/src/dex.ts +++ b/ts/src/dex.ts @@ -311,7 +311,7 @@ export class Dex { marketAddress: market.address.toString(), programID: this.address.toString(), rpcEndpoint: this.connection.rpcEndpoint, - ownerFilePath: owner.filePath, + ownerFilePath: owner.absoluteFilePath, duration: opts.durationInSecs * 1000, orderCount: opts.orderCount, initialBidSize: opts.initialBidSize, @@ -357,7 +357,7 @@ export class Dex { marketAddress: market.address.toString(), programID: this.address.toString(), rpcEndpoint: this.connection.rpcEndpoint, - ownerFilePath: owner.filePath, + ownerFilePath: owner.absoluteFilePath, duration: opts.durationInSecs * 1000, verbose: opts.verbose ? "true" : "false", }, diff --git a/ts/src/fileKeypair.ts b/ts/src/fileKeypair.ts index 31b5c25..45286fb 100644 --- a/ts/src/fileKeypair.ts +++ b/ts/src/fileKeypair.ts @@ -1,52 +1,59 @@ import { Keypair } from "@solana/web3.js"; import fs from "fs"; +import path from "path"; /** * A wrapper class around @solana/web3.js `Keypair` that allows persisting key-pairs in your local filesystem. */ export class FileKeypair { - public filePath: string; + public absoluteFilePath: string; public keypair: Keypair; - private constructor(filePath: string, keypair: Keypair) { - this.filePath = filePath; + private constructor(absoluteFilePath: string, keypair: Keypair) { + this.absoluteFilePath = absoluteFilePath; this.keypair = keypair; } static load(filePath: string): FileKeypair { - const fileBuffer = fs.readFileSync(filePath); + const absolutePath = path.resolve(filePath); + const fileBuffer = fs.readFileSync(absolutePath); const secretKey: number[] = JSON.parse(fileBuffer.toString()); const keypair = Keypair.fromSecretKey(Uint8Array.from(secretKey)); - return new FileKeypair(filePath, keypair); + return new FileKeypair(absolutePath, keypair); } - static generate(filepath: string): FileKeypair { + static generate(filePath: string): FileKeypair { + const absolutePath = path.resolve(filePath); const keypair = Keypair.generate(); const secretKey = Array.from(keypair.secretKey); - fs.writeFileSync(filepath, JSON.stringify(secretKey)); - return new FileKeypair(filepath, keypair); + fs.writeFileSync(absolutePath, JSON.stringify(secretKey)); + return new FileKeypair(absolutePath, keypair); } static loadOrGenerate(filePath: string): FileKeypair { let keypair: Keypair; let secretKey: number[]; + const absolutePath = path.resolve(filePath); + try { - const fileBuffer = fs.readFileSync(filePath); + const fileBuffer = fs.readFileSync(absolutePath); secretKey = JSON.parse(fileBuffer.toString()); keypair = Keypair.fromSecretKey(Uint8Array.from(secretKey)); } catch (e) { keypair = Keypair.generate(); secretKey = Array.from(keypair.secretKey); - fs.writeFileSync(filePath, JSON.stringify(secretKey)); + fs.writeFileSync(absolutePath, JSON.stringify(secretKey)); } - return new FileKeypair(filePath, keypair); + return new FileKeypair(absolutePath, keypair); } static withKeypair(filePath: string, keypair: Keypair): FileKeypair { - const secretKey = Array.from(keypair.secretKey); - fs.writeFileSync(filePath, JSON.stringify(secretKey)); + const absolutePath = path.resolve(filePath); - return new FileKeypair(filePath, keypair); + const secretKey = Array.from(keypair.secretKey); + fs.writeFileSync(absolutePath, JSON.stringify(secretKey)); + + return new FileKeypair(absolutePath, keypair); } }