Skip to content

Commit

Permalink
Merge pull request #1 from blend/keys-as-buffers
Browse files Browse the repository at this point in the history
Use Buffers in key pairs
  • Loading branch information
Kenadia authored Aug 8, 2019
2 parents 9614cae + c78e2a9 commit d0f16a0
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 28 deletions.
5 changes: 4 additions & 1 deletion src/pactApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 9 additions & 7 deletions src/pactUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ 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
}
}

export class PactExpr {
expr: string

constructor(expr: string) {
this.expr = expr
}
Expand All @@ -22,13 +24,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')] }
}

/**
Expand All @@ -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]
Object.assign(data, keysetData(x.publicKey, x.name))
return `(read-keyset "${x.name}")`
} else if (x instanceof PactExpr) {
return x.expr
Expand Down
4 changes: 2 additions & 2 deletions src/scripts/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 2 additions & 4 deletions src/scripts/deployContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,15 @@ 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.
*/
async function loadContract(pactApi: PactApi, contractFilename: string): Promise<void> {
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}`)
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
*/

export interface IKeyPair {
publicKey: string
privateKey: string
publicKey: Buffer
privateKey: Buffer
}
24 changes: 12 additions & 12 deletions src/typings.d.ts
Original file line number Diff line number Diff line change
@@ -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
}
Expand Down

0 comments on commit d0f16a0

Please sign in to comment.