Skip to content

Commit

Permalink
Merge bef6ed1 into f1672b4
Browse files Browse the repository at this point in the history
  • Loading branch information
keefertaylor committed Jul 12, 2020
2 parents f1672b4 + bef6ed1 commit c9fe4ae
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 37 deletions.
28 changes: 14 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 23 additions & 7 deletions src/chain/tezos/TezosNodeWriter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as blakejs from 'blakejs';

import { KeyStore, Signer } from '../../types/ExternalInterfaces';
import { KeyStore, Signer, SignerCurve } from '../../types/ExternalInterfaces';
import * as TezosTypes from '../../types/tezos/TezosChainTypes';
import { TezosConstants } from '../../types/tezos/TezosConstants';
import * as TezosP2PMessageTypes from '../../types/tezos/TezosP2PMessageTypes';
Expand All @@ -10,11 +10,13 @@ import { TezosMessageCodec } from './TezosMessageCodec';
import { TezosMessageUtils } from './TezosMessageUtil';
import { TezosLanguageUtil } from './TezosLanguageUtil';
import { TezosOperationQueue } from './TezosOperationQueue';
import base58check from "bs58check";

import FetchSelector from '../../utils/FetchSelector'
const fetch = FetchSelector.fetch;

import LogSelector from '../../utils/LoggerSelector';
import { assert } from 'console';
const log = LogSelector.log;

let operationQueues = {}
Expand All @@ -34,9 +36,6 @@ export namespace TezosNodeWriter {
function performPostRequest(server: string, command: string, payload = {}): Promise<Response> {
const url = `${server}/${command}`;
const payloadStr = JSON.stringify(payload);

log.debug(`TezosNodeWriter.performPostRequest sending ${payloadStr}\n->\n${url}`);

return fetch(url, { method: 'post', body: payloadStr, headers: { 'content-type': 'application/json' } });
}

Expand Down Expand Up @@ -175,8 +174,25 @@ export namespace TezosNodeWriter {
const opSignature = await signer.signOperation(Buffer.from(TezosConstants.OperationGroupWatermark + forgedOperationGroup, 'hex'));

const signedOpGroup = Buffer.concat([Buffer.from(forgedOperationGroup, 'hex'), opSignature]);
const hexSignature = TezosMessageUtils.readSignatureWithHint(opSignature, 'edsig');
const opPair = { bytes: signedOpGroup, signature: hexSignature };

let signaturePrefix = new Uint8Array([9, 245, 205, 134, 18])
switch (signer.getSignerCurve()) {
case SignerCurve.SECP256K1:
signaturePrefix = new Uint8Array([13, 115, 101, 19, 63])
case SignerCurve.SECP256R1:
signaturePrefix = new Uint8Array([54, 240, 44, 52])
case SignerCurve.ED25519:
break
default:
break
}

const signaturePrefixHex = Buffer.from(signaturePrefix).toString('hex')
const opSignatureHex = opSignature.toString('hex')
const prefixedSignatureHex = signaturePrefixHex + opSignatureHex
const signature = base58check.encode(prefixedSignatureHex)

const opPair = { bytes: signedOpGroup, signature: signature };

const appliedOp = await preapplyOperation(server, blockHead.hash, blockHead.protocol, operations, opPair);
const injectedOperation = await injectOperation(server, opPair);
Expand All @@ -201,7 +217,7 @@ export namespace TezosNodeWriter {
operationQueues[k].addOperations(...operations);
}

export function getQueueStatus(server: string, keyStore: KeyStore){
export function getQueueStatus(server: string, keyStore: KeyStore) {
const k = blakejs.blake2s(`${server}${keyStore.publicKeyHash}`, null, 16);

if (operationQueues[k]) {
Expand Down
7 changes: 7 additions & 0 deletions src/types/ExternalInterfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
export enum SignerCurve {
ED25519,
SECP256K1,
SECP256R1
}

export interface Signer {
getSignerCurve: () => SignerCurve;
signOperation: (bytes: Buffer) => Promise<Buffer>;
signText: (message: string) => Promise<string>;
signTextHash: (message: string) => Promise<string>;
Expand Down
32 changes: 16 additions & 16 deletions test/chain/tezos/TezosNodeWriter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import fetch from 'node-fetch';
import * as log from 'loglevel';

import { registerFetch, registerLogger } from '../../../src/index';
import { KeyStoreUtils, SoftSigner } from 'conseiljs-softsigner';
import { KeyStoreUtils, SoftSigner } from 'conseiljs-softsigner'

import mochaAsync from '../../mochaTestHelper';
import { accounts, contracts, drips } from "../../_staticData/accounts.json";
Expand Down Expand Up @@ -43,7 +43,7 @@ describe('TezosNodeWriter tests', () => {
server
.filteringRequestBody(body => '*')
.post(`/chains/main/blocks/head/helpers/forge/operations`)
.reply(200, responses[`${accounts[0].publicKeyHash}-forgeOperationsRemotely-success`])
.reply(200, responses[`${accounts[0].publicKeyHash}-forgeOperationsRemotely-success`])

const transaction = {
kind: 'transaction',
Expand Down Expand Up @@ -73,17 +73,17 @@ describe('TezosNodeWriter tests', () => {
const server = nock(serverUrl);
server
.get(`/chains/main/blocks/head`)
.reply(200, responses['sendTransactionOperation-blocks/head']);
.reply(200, responses['sendTransactionOperation-blocks/head']);
server
.get(`/chains/main/blocks/head/context/contracts/${accounts[0].publicKeyHash}/counter`)
.reply(200, responses[`chains/main/blocks/head/context/contracts/${accounts[0].publicKeyHash}/counter`])
.reply(200, responses[`chains/main/blocks/head/context/contracts/${accounts[0].publicKeyHash}/counter`])
.get(`/chains/main/blocks/head/context/contracts/${accounts[0].publicKeyHash}/manager_key`)
.reply(200, responses[`chains/main/blocks/head/context/contracts/${accounts[0].publicKeyHash}/manager_key`])
.reply(200, responses[`chains/main/blocks/head/context/contracts/${accounts[0].publicKeyHash}/manager_key`])
.filteringRequestBody(body => '*')
.post(`/chains/main/blocks/head/helpers/preapply/operations`)
.reply(200, responses[`${keyStore.publicKeyHash}-sendTransactionOperation-preapply-success`])
.reply(200, responses[`${keyStore.publicKeyHash}-sendTransactionOperation-preapply-success`])
.post(`/injection/operation?chain=main`)
.reply(200, responses[`${keyStore.publicKeyHash}-sendTransactionOperation-injection-success`]);
.reply(200, responses[`${keyStore.publicKeyHash}-sendTransactionOperation-injection-success`]);

const destination = 'tz1fX6A2miVXjNyReg2dpt2TsXLkZ4w7zRGa';
const amount = 1_000;
Expand All @@ -99,16 +99,16 @@ describe('TezosNodeWriter tests', () => {
const server = nock(serverUrl);
server
.get(`/chains/main/blocks/head`)
.reply(200, responses['sendIdentityActivationOperation-blocks/head'])
.reply(200, responses['sendIdentityActivationOperation-blocks/head'])
.get(`/chains/main/blocks/head/context/contracts/${faucetKeyStore.publicKeyHash}/counter`)
.reply(200, responses[`chains/main/blocks/head/context/contracts/${faucetKeyStore.publicKeyHash}/counter`])
.reply(200, responses[`chains/main/blocks/head/context/contracts/${faucetKeyStore.publicKeyHash}/counter`])
.get(`/chains/main/blocks/head/context/contracts/${faucetKeyStore.publicKeyHash}/manager_key`)
.reply(200, responses[`chains/main/blocks/head/context/contracts/${faucetKeyStore.publicKeyHash}/manager_key`])
.reply(200, responses[`chains/main/blocks/head/context/contracts/${faucetKeyStore.publicKeyHash}/manager_key`])
.filteringRequestBody(body => '*')
.post(`/chains/main/blocks/head/helpers/preapply/operations`)
.reply(200, responses[`${faucetKeyStore.publicKeyHash}-sendTransactionOperation-preapply-success`])
.reply(200, responses[`${faucetKeyStore.publicKeyHash}-sendTransactionOperation-preapply-success`])
.post(`/injection/operation?chain=main`)
.reply(200, responses[`${faucetKeyStore.publicKeyHash}-sendTransactionOperation-injection-success`]);
.reply(200, responses[`${faucetKeyStore.publicKeyHash}-sendTransactionOperation-injection-success`]);

const destination = accounts[0].publicKeyHash;
const amount = 1_000;
Expand Down Expand Up @@ -136,13 +136,13 @@ describe('TezosNodeWriter tests', () => {
const server = nock(serverUrl);
server
.get(`/chains/main/blocks/head`)
.reply(200, responses['sendIdentityActivationOperation-blocks/head'])
.reply(200, responses['sendIdentityActivationOperation-blocks/head'])
.filteringRequestBody(body => '*')
.post(`/chains/main/blocks/head/helpers/preapply/operations`)
.reply(200, responses[`${faucetKeyStore.publicKeyHash}-sendIdentityActivationOperation-preapply-success`])
.reply(200, responses[`${faucetKeyStore.publicKeyHash}-sendIdentityActivationOperation-preapply-success`])
.post(`/injection/operation?chain=main`)
.reply(200, responses[`${faucetKeyStore.publicKeyHash}-sendIdentityActivationOperation-injection-success`]);
.reply(200, responses[`${faucetKeyStore.publicKeyHash}-sendIdentityActivationOperation-injection-success`]);


const sendResult = await TezosNodeWriter.sendIdentityActivationOperation(serverUrl, faucetSigner, faucetKeyStore, drips[0].secret);

Expand Down

0 comments on commit c9fe4ae

Please sign in to comment.