From 8b9b92148f359241218c8997054c52c0211fb41a Mon Sep 17 00:00:00 2001 From: ken Date: Wed, 7 Aug 2019 18:09:44 -0700 Subject: [PATCH 1/3] Use Buffers in key pairs --- src/pactApi.ts | 5 ++++- src/pactUtils.ts | 8 ++++---- src/scripts/config.ts | 4 ++-- src/types.ts | 4 ++-- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/pactApi.ts b/src/pactApi.ts index 05dc204..75b528c 100644 --- a/src/pactApi.ts +++ b/src/pactApi.ts @@ -112,7 +112,10 @@ export default class PactApi { const nonce = options.nonce || generateNonce() const code: string = (options.codeFile ? await loadFile(options.codeFile) : options.code) || '' const data: {} = (options.dataFile ? JSON.parse(await loadFile(options.dataFile)) : options.data) || {} - const keyPair = { publicKey: options.keyPair.publicKey, secretKey: options.keyPair.privateKey } + const keyPair = { + publicKey: options.keyPair.publicKey.toString('hex'), + secretKey: options.keyPair.privateKey.toString('hex'), + } return pact.simple.exec.createCommand( keyPair, nonce, diff --git a/src/pactUtils.ts b/src/pactUtils.ts index 8082e65..25a1002 100644 --- a/src/pactUtils.ts +++ b/src/pactUtils.ts @@ -22,13 +22,13 @@ export class PactExpr { export function generateKeyPair(): IKeyPair { const { publicKey, secretKey } = pactLang.crypto.genKeyPair() return { - publicKey, - privateKey: secretKey, + publicKey: Buffer.from(publicKey, 'hex'), + privateKey: Buffer.from(secretKey, 'hex'), } } -export function keysetData(publicKey: string, name: string) { - return { [name]: [publicKey] } +export function keysetData(publicKey: Buffer, name: string) { + return { [name]: [publicKey.toString('hex')] } } /** diff --git a/src/scripts/config.ts b/src/scripts/config.ts index 8ef2c3a..f1d0b92 100644 --- a/src/scripts/config.ts +++ b/src/scripts/config.ts @@ -7,8 +7,8 @@ import { IKeyPair } from '../types' export function getAdminKeyPair(): IKeyPair { if (process.env.PACT_ADMIN_PUBLIC && process.env.PACT_ADMIN_PRIVATE) { return { - publicKey: process.env.PACT_ADMIN_PUBLIC, - privateKey: process.env.PACT_ADMIN_PRIVATE, + publicKey: Buffer.from(process.env.PACT_ADMIN_PUBLIC, 'hex'), + privateKey: Buffer.from(process.env.PACT_ADMIN_PRIVATE, 'hex'), } } return pactUtils.generateKeyPair() diff --git a/src/types.ts b/src/types.ts index a63ac35..8c8b107 100644 --- a/src/types.ts +++ b/src/types.ts @@ -3,6 +3,6 @@ */ export interface IKeyPair { - publicKey: string - privateKey: string + publicKey: Buffer + privateKey: Buffer } From 4c234b46f5add64f495098836aeb8dcb008cb095 Mon Sep 17 00:00:00 2001 From: ken Date: Wed, 7 Aug 2019 18:13:33 -0700 Subject: [PATCH 2/3] Use Buffer in Keyset --- src/pactUtils.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/pactUtils.ts b/src/pactUtils.ts index 25a1002..a7c94a3 100644 --- a/src/pactUtils.ts +++ b/src/pactUtils.ts @@ -4,9 +4,9 @@ import { IKeyPair } from './types' // TODO: Support keysets other than just a single key. export class Keyset { - publicKey: string + publicKey: Buffer name: string - constructor(publicKey: string, name: string = publicKey) { + constructor(publicKey: Buffer, name: string = publicKey.toString('hex')) { this.publicKey = publicKey this.name = name } @@ -38,7 +38,7 @@ export function makeExprAndData(functionName: string, args: {}[]): [string, {}] const data: {[name: string]: {}} = {} const argsString = args.map(x => { if (x instanceof Keyset) { - data[x.name] = [x.publicKey] + data[x.name] = [x.publicKey.toString('hex')] return `(read-keyset "${x.name}")` } else if (x instanceof PactExpr) { return x.expr From c78e2a9c948e3dfc32962d365ef62f9c1798e1d2 Mon Sep 17 00:00:00 2001 From: ken Date: Wed, 7 Aug 2019 18:26:13 -0700 Subject: [PATCH 3/3] Make minor fixes --- src/pactUtils.ts | 4 +++- src/scripts/deployContracts.ts | 6 ++---- src/typings.d.ts | 24 ++++++++++++------------ 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/pactUtils.ts b/src/pactUtils.ts index a7c94a3..7dbd195 100644 --- a/src/pactUtils.ts +++ b/src/pactUtils.ts @@ -6,6 +6,7 @@ import { IKeyPair } from './types' export class Keyset { publicKey: Buffer name: string + constructor(publicKey: Buffer, name: string = publicKey.toString('hex')) { this.publicKey = publicKey this.name = name @@ -14,6 +15,7 @@ export class Keyset { export class PactExpr { expr: string + constructor(expr: string) { this.expr = expr } @@ -38,7 +40,7 @@ export function makeExprAndData(functionName: string, args: {}[]): [string, {}] const data: {[name: string]: {}} = {} const argsString = args.map(x => { if (x instanceof Keyset) { - data[x.name] = [x.publicKey.toString('hex')] + Object.assign(data, keysetData(x.publicKey, x.name)) return `(read-keyset "${x.name}")` } else if (x instanceof PactExpr) { return x.expr diff --git a/src/scripts/deployContracts.ts b/src/scripts/deployContracts.ts index b17df51..59ab0ea 100644 --- a/src/scripts/deployContracts.ts +++ b/src/scripts/deployContracts.ts @@ -10,7 +10,7 @@ import * as config from './config' // Set up keys required to deploy and/or initialize the contract. const adminKeysetName = 'admin-keyset' // Must match the name used in the contract. const adminKeyPair = config.getAdminKeyPair() -const adminKeyData = JSON.stringify({ [adminKeysetName]: [adminKeyPair.publicKey] }) +const adminKeyData = JSON.stringify({ [adminKeysetName]: [adminKeyPair.publicKey.toString('hex')] }) /** * Helper function to load a contract using the admin keyset. @@ -18,9 +18,7 @@ const adminKeyData = JSON.stringify({ [adminKeysetName]: [adminKeyPair.publicKey async function loadContract(pactApi: PactApi, contractFilename: string): Promise { const result = await pactApi.eval({ codeFile: contractFilename, - data: JSON.stringify({ - [adminKeysetName]: [adminKeyPair.publicKey], - }), + data: adminKeyData, keyPair: adminKeyPair, }) console.log(`Deployed contract '${contractFilename}' with result: ${result}`) diff --git a/src/typings.d.ts b/src/typings.d.ts index 3834b3d..1fd2d23 100644 --- a/src/typings.d.ts +++ b/src/typings.d.ts @@ -1,17 +1,17 @@ -interface KeyPair { - publicKey: string, - secretKey: string, -} +declare module 'pact-lang-api' { + interface KeyPair { + publicKey: string, + secretKey: string, + } -interface SendRequest { - cmds: { - hash: string - sigs: {}[] - cmd: string - }[] -} + interface SendRequest { + cmds: { + hash: string + sigs: {}[] + cmd: string + }[] + } -declare module 'pact-lang-api' { var crypto: { genKeyPair: () => KeyPair }