Skip to content

Commit

Permalink
update TezosMessageUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
keefertaylor committed Jul 13, 2020
1 parent 01adeb7 commit bfcaadf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 32 deletions.
25 changes: 15 additions & 10 deletions src/chain/tezos/TezosMessageUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as blakejs from 'blakejs';
import { SignedOperationGroup } from '../../types/tezos/TezosChainTypes';
import { TezosLanguageUtil } from './TezosLanguageUtil';
import { TezosParameterFormat } from '../../types/tezos/TezosChainTypes';
import { SignerCurve } from "../../types/ExternalInterfaces";

/**
* A collection of functions to encode and decode various Tezos P2P message components like amounts, addresses, hashes, etc.
Expand Down Expand Up @@ -295,8 +296,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 @@ -308,11 +309,15 @@ export namespace TezosMessageUtils {
* @param {Buffer | Uint8Array} b Bytes containing signature.
* @param hint Support 'edsig'.
*/
export function readSignatureWithHint(b: Buffer | Uint8Array, hint: string): string {
export function readSignatureWithHint(b: Buffer | Uint8Array, hint: string | SignerCurve): string {
const sig = !(b instanceof Buffer) ? Buffer.from(b) : b;

if (hint === 'edsig') {
if (hint === 'edsig' || hint === SignerCurve.ED25519) {
return base58check.encode(Buffer.from('09f5cd8612' + sig.toString('hex'), 'hex'));
} else if (hint === 'spsig' || hint === SignerCurve.SECP256K1) {
return base58check.encode(Buffer.from('0d7365133f' + sig.toString('hex'), 'hex'));
} else if (hint === 'p2sig' || hint === SignerCurve.SECP256R1) {
return base58check.encode(Buffer.from('36f02c34' + sig.toString('hex'), 'hex'));
} else {
throw new Error(`Unrecognized signature hint, '${hint}'`);
}
Expand Down Expand Up @@ -398,7 +403,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 +447,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 +486,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 +519,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
28 changes: 6 additions & 22 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, SignerCurve } from '../../types/ExternalInterfaces';
import { KeyStore, Signer } 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,7 +10,6 @@ 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;
Expand All @@ -35,6 +34,9 @@ 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 @@ -173,26 +175,8 @@ export namespace TezosNodeWriter {
const opSignature = await signer.signOperation(Buffer.from(TezosConstants.OperationGroupWatermark + forgedOperationGroup, 'hex'));

const signedOpGroup = Buffer.concat([Buffer.from(forgedOperationGroup, 'hex'), opSignature]);

let signaturePrefix = new Uint8Array([9, 245, 205, 134, 18])
switch (signer.getSignerCurve()) {
case SignerCurve.SECP256K1:
signaturePrefix = new Uint8Array([54, 240, 44, 52])
case SignerCurve.SECP256R1:
signaturePrefix = new Uint8Array([13, 115, 101, 19, 63])
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(Buffer.from(prefixedSignatureHex, 'hex'))

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

const base58signature = TezosMessageUtils.readSignatureWithHint(opSignature, signer.getSignerCurve());
const opPair = { bytes: signedOpGroup, signature: base58signature };
const appliedOp = await preapplyOperation(server, blockHead.hash, blockHead.protocol, operations, opPair);
const injectedOperation = await injectOperation(server, opPair);

Expand Down

0 comments on commit bfcaadf

Please sign in to comment.