Skip to content

Commit

Permalink
Merge 7aadfaf into f1672b4
Browse files Browse the repository at this point in the history
  • Loading branch information
keefertaylor committed Jul 13, 2020
2 parents f1672b4 + 7aadfaf commit fd43ff1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 31 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.

35 changes: 22 additions & 13 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 All @@ -324,9 +329,13 @@ export namespace TezosMessageUtils {
* @param key Key to encode, input is expected to be a base58-check encoded string.
* @param hint Key type, usually the curve it was generated from, eg: 'edsig'.
*/
export function writeSignatureWithHint(sig: string, hint: string): Buffer {
if (hint === 'edsig') {
return base58check.decode(sig).slice(5);
export function writeSignatureWithHint(sig: string, hint: string | SignerCurve): Buffer {
if (hint === 'edsig' || hint === SignerCurve.ED25519) {
return base58check.decode(sig).slice("edsig".length);
} else if (hint === 'spsig' || hint === SignerCurve.SECP256K1) {
return base58check.decode(sig).slice("spsig".length);
} else if (hint === 'p2sig' || hint === SignerCurve.SECP256R1) {
return base58check.decode(sig).slice("p2sig".length);
} else {
throw new Error(`Unrecognized key hint, '${hint}'`);
}
Expand Down Expand Up @@ -398,7 +407,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 +451,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 +490,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 +523,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
7 changes: 3 additions & 4 deletions src/chain/tezos/TezosNodeWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +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]);
const hexSignature = TezosMessageUtils.readSignatureWithHint(opSignature, 'edsig');
const opPair = { bytes: signedOpGroup, signature: hexSignature };

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 All @@ -201,7 +200,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 fd43ff1

Please sign in to comment.