Skip to content

Commit

Permalink
Merge d83acc5 into f1672b4
Browse files Browse the repository at this point in the history
  • Loading branch information
keefertaylor authored Jul 12, 2020
2 parents f1672b4 + d83acc5 commit f818dc7
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 30 deletions.
2 changes: 0 additions & 2 deletions dist-web/conseiljs.min.js

This file was deleted.

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.

17 changes: 8 additions & 9 deletions src/chain/tezos/TezosMessageUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ export namespace TezosMessageUtils {
export function writeKeyWithHint(key: string, hint: string): Buffer {
if (hint === 'edsk' || hint === 'edpk') { // ed25519
return base58check.decode(key).slice(4);
//} else if (hint === 'sppk') { // secp256k1
//} else if (hint === 'p2pk') { // secp256r1
//} else if (hint === 'sppk') { // secp256k1
//} else if (hint === 'p2pk') { // secp256r1
} else {
throw new Error(`Unrecognized key hint, '${hint}'`);
}
Expand All @@ -310,7 +310,6 @@ export namespace TezosMessageUtils {
*/
export function readSignatureWithHint(b: Buffer | Uint8Array, hint: string): string {
const sig = !(b instanceof Buffer) ? Buffer.from(b) : b;

if (hint === 'edsig') {
return base58check.encode(Buffer.from('09f5cd8612' + sig.toString('hex'), 'hex'));
} else {
Expand Down Expand Up @@ -398,7 +397,7 @@ export namespace TezosMessageUtils {
* @param format value format, this argument is used to encode complex values, Michelson and Micheline encoding is supported with the internal parser.
*/
export function writePackedData(value: string | number | Buffer, type: string, format: TezosParameterFormat = TezosParameterFormat.Micheline): string {
switch(type) {
switch (type) {
case 'int': {
return '0500' + writeSignedInt(value as number);
}
Expand Down Expand Up @@ -442,8 +441,8 @@ export namespace TezosMessageUtils {
* @param hex
* @param type
*/
export function readPackedData(hex: string, type: string) : string | number {
switch(type) {
export function readPackedData(hex: string, type: string): string | number {
switch (type) {
case 'int': {
return readSignedInt(hex.slice(4));
}
Expand Down Expand Up @@ -481,15 +480,15 @@ export namespace TezosMessageUtils {
* @param {Buffer} payload Buffer to hash
* @param {number} length Length of hash to produce
*/
export function simpleHash(payload: Buffer, length: number) : Buffer {
export function simpleHash(payload: Buffer, length: number): Buffer {
return Buffer.from(blakejs.blake2b(payload, null, length)); // Same as libsodium.crypto_generichash
}

/**
* Encodes the provided number as base128.
* @param n
*/
function twoByteHex(n: number) : string {
function twoByteHex(n: number): string {
if (n < 128) { return ('0' + n.toString(16)).slice(-2); }

let h = '';
Expand All @@ -514,7 +513,7 @@ export namespace TezosMessageUtils {
* Decodes the provided base128 string into a number
* @param s
*/
function fromByteHex(s: string) : number {
function fromByteHex(s: string): number {
if (s.length === 2) { return parseInt(s, 16); }

if (s.length <= 8) {
Expand Down
35 changes: 30 additions & 5 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 @@ -35,7 +37,8 @@ export namespace TezosNodeWriter {
const url = `${server}/${command}`;
const payloadStr = JSON.stringify(payload);

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

return fetch(url, { method: 'post', body: payloadStr, headers: { 'content-type': 'application/json' } });
}
Expand Down Expand Up @@ -169,14 +172,36 @@ export namespace TezosNodeWriter {
* @returns {Promise<OperationResult>} The ID of the created operation group
*/
export async function sendOperation(server: string, operations: TezosP2PMessageTypes.Operation[], signer: Signer): Promise<TezosTypes.OperationResult> {

log.debug("Got to send ")

const blockHead = await TezosNodeReader.getBlockHead(server);
const forgedOperationGroup = forgeOperations(blockHead.hash, operations);

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
}
assert(1 + 1 === 3, "WRONG")

const signaturePrefixHex = Buffer.from(signaturePrefix).toString('hex')
const opSignatureHex = opSignature.toString('hex')
const prefixedSignatureHex = signaturePrefixHex + opSignatureHex
const signature = base58check.encode(prefixedSignatureHex)
log.debug("Computed signature as " + signature)

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 +226,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

0 comments on commit f818dc7

Please sign in to comment.