Skip to content

Commit

Permalink
fix all transaction stuff, rpc calls, more
Browse files Browse the repository at this point in the history
  • Loading branch information
spencercorwin committed Nov 18, 2020
1 parent a25b2ec commit c3045e1
Show file tree
Hide file tree
Showing 31 changed files with 376 additions and 323 deletions.
2 changes: 2 additions & 0 deletions packages/neo-one-client-common/src/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ const createInvocationScript = (message: Buffer, privateKey: PrivateKey): Buffer
const createVerificationScript = (publicKey: ECPoint): Buffer => {
const builder = new ScriptBuilder();
builder.emitPushECPoint(publicKey);
builder.emitOp('PUSHNULL');
builder.emitSysCall('Neo.Crypto.VerifyWithECDsaSecp256r1');

return builder.build();
Expand Down Expand Up @@ -320,6 +321,7 @@ const createMultiSignatureVerificationScript = (mIn: number, publicKeys: readonl
builder.emitPushECPoint(ecPoint);
});
builder.emitPushInt(publicKeysSorted.length);
builder.emitOp('PUSHNULL');
builder.emitSysCall('Neo.Crypto.CheckMultisigWithECDsaSecp256r1');

return builder.build();
Expand Down
9 changes: 1 addition & 8 deletions packages/neo-one-client-common/src/models/Serializable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,7 @@ export interface SerializableJSON<TJSON> {
readonly serializeJSON: () => TJSON;
}

/** TODO: revist how the `magic` gets in here. Previously it was passed only as a `DeserializeContext` option
* but now its needed for getting the hash of certain models. Really this is something that should get passed in
* from `Blockchain` or `Settings`
*/

// mainnet: 5195086
// testnet: 1951352142
export const createGetHashData = (serializeWire: () => Buffer, magic = 1951352142) => () => {
export const createGetHashData = (serializeWire: () => Buffer, magic: number) => () => {
const writer = new BinaryWriter();
writer.writeUInt32LE(magic);
writer.writeBytes(serializeWire());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const assertVerifyResult = (value: number): VerifyResultModel => {
return value;
};

export const toJSONVerifyResult = (reason: VerifyResultModel): VerifyResultJSON =>
export const toVerifyResultJSON = (reason: VerifyResultModel): VerifyResultJSON =>
assertVerifyResultJSON(VerifyResultModel[reason]);

export const isVerifyResultJSON = (reason: string): reason is VerifyResultJSON =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import BN from 'bn.js';
import { BinaryWriter } from '../../BinaryWriter';
import { common, ECPoint, InvalidFormatError, PrivateKey, UInt160, UInt256, UInt256Hex } from '../../common';
import { crypto } from '../../crypto';
import { utils } from '../../utils';
import { createGetHashData, createSerializeWire, SerializableWire, SerializeWire } from '../Serializable';
import { SignerModel } from '../SignerModel';
import { WitnessModel } from '../WitnessModel';
Expand All @@ -12,6 +11,7 @@ import { AttributeModel } from './attribute';
export interface TransactionConsensusOptions {
readonly nonce: number;
readonly validUntilBlock: number;
readonly messageMagic: number;
}

export interface TransactionFeesAdd {
Expand All @@ -26,7 +26,7 @@ export interface FeelessTransactionModelAdd<
> {
readonly version?: number;
readonly nonce?: number;
readonly validUntilBlock?: number;
readonly validUntilBlock: number;
readonly attributes?: readonly TAttribute[];
readonly signers?: readonly TSigner[];
readonly script: Buffer;
Expand Down Expand Up @@ -61,17 +61,15 @@ export class FeelessTransactionModel<

public readonly serializeWire: SerializeWire = createSerializeWire(this.serializeWireBase.bind(this));
public readonly serializeUnsigned: SerializeWire = createSerializeWire(this.serializeUnsignedBase.bind(this));
private readonly hashInternal: () => UInt256;
private readonly hashHexInternal = utils.lazy(() => common.uInt256ToHex(this.hash));
private readonly messageInternal = utils.lazy(() => createGetHashData(this.serializeUnsigned)());
private readonly hashInternal: (magic: number) => UInt256;

public constructor({
version,
nonce = 0,
attributes = [],
witnesses = [],
signers = [],
validUntilBlock = 0,
validUntilBlock,
script,
hash,
}: FeelessTransactionModelAdd<TAttribute, TWitness, TSigner>) {
Expand All @@ -84,7 +82,7 @@ export class FeelessTransactionModel<
this.validUntilBlock = validUntilBlock;
this.script = script;
const hashIn = hash;
this.hashInternal = hashIn === undefined ? utils.lazy(() => crypto.hash256(this.message)) : () => hashIn;
this.hashInternal = hashIn === undefined ? (magic) => crypto.hash256(this.getMessage(magic)) : () => hashIn;

if (this.attributes.length > MAX_TRANSACTION_ATTRIBUTES) {
throw new InvalidFormatError(
Expand All @@ -93,16 +91,16 @@ export class FeelessTransactionModel<
}
}

public get hash(): UInt256 {
return this.hashInternal();
public getHash(magic: number): UInt256 {
return this.hashInternal(magic);
}

public get hashHex(): UInt256Hex {
return this.hashHexInternal();
public getHashHex(magic: number): UInt256Hex {
return this.hashHexInternal(magic);
}

public get message(): Buffer {
return this.messageInternal();
public getMessage(magic: number): Buffer {
return this.messageInternal(magic);
}

public cloneWithConsensusOptions(
Expand All @@ -113,20 +111,21 @@ export class FeelessTransactionModel<
attributes: this.attributes,
signers: this.signers,
script: this.script,
hash: this.hash,
hash: this.getHash(options.messageMagic),
witnesses: this.witnesses,
nonce: options.nonce,
validUntilBlock: options.validUntilBlock,
});
}

public sign(_key: PrivateKey): FeelessTransactionModel<TAttribute, TWitness, TSigner> {
public sign(_key: PrivateKey, _magic: number): FeelessTransactionModel<TAttribute, TWitness, TSigner> {
throw new Error('clone to a fee transaction before signing');
}

public signWithSignature(
_signature: Buffer,
_publicKey: ECPoint,
_magic: number,
): FeelessTransactionModel<TAttribute, TWitness, TSigner> {
throw new Error('clone to a fee transaction before signing');
}
Expand All @@ -152,4 +151,6 @@ export class FeelessTransactionModel<
witness.serializeWireBase(writer);
});
}
private readonly hashHexInternal = (magic: number) => common.uInt256ToHex(this.getHash(magic));
private readonly messageInternal = (magic: number) => createGetHashData(this.serializeUnsigned, magic)();
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,16 @@ export class TransactionModel<
this.networkFee = networkFee;
}

public clone(
options: { readonly witnesses?: readonly TWitness[] } = {},
): TransactionModel<TAttribute, TWitness, TSigner> {
public clone(options: {
readonly witnesses?: readonly TWitness[];
readonly messageMagic: number;
}): TransactionModel<TAttribute, TWitness, TSigner> {
return new TransactionModel<TAttribute, TWitness, TSigner>({
version: this.version,
attributes: this.attributes,
signers: this.signers,
script: this.script,
hash: this.hash,
hash: this.getHash(options.messageMagic),
witnesses: options.witnesses ? options.witnesses : this.witnesses,
systemFee: this.systemFee,
networkFee: this.networkFee,
Expand All @@ -87,7 +88,7 @@ export class TransactionModel<
attributes: this.attributes,
signers: this.signers,
script: this.script,
hash: this.hash,
hash: this.getHash(options.messageMagic),
witnesses: this.witnesses,
systemFee: this.systemFee,
networkFee: this.networkFee,
Expand All @@ -96,17 +97,23 @@ export class TransactionModel<
});
}

public sign(key: PrivateKey): TransactionModel<TAttribute, TWitness, TSigner> {
public sign(key: PrivateKey, messageMagic: number): TransactionModel<TAttribute, TWitness, TSigner> {
return this.clone({
messageMagic,
witnesses: this.witnesses.concat([
// tslint:disable-next-line no-any
crypto.createWitness(this.serializeUnsigned(), key, (this.constructor as any).WitnessConstructor),
]),
});
}

public signWithSignature(signature: Buffer, publicKey: ECPoint): TransactionModel<TAttribute, TWitness, TSigner> {
public signWithSignature(
signature: Buffer,
publicKey: ECPoint,
messageMagic: number,
): TransactionModel<TAttribute, TWitness, TSigner> {
return this.clone({
messageMagic,
witnesses: this.witnesses.concat([
// tslint:disable-next-line no-any
crypto.createWitnessForSignature(signature, publicKey, (this.constructor as any).WitnessConstructor),
Expand Down
25 changes: 10 additions & 15 deletions packages/neo-one-client-common/src/models/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ export interface TransactionJSON {
export interface VerboseTransactionJSON extends TransactionJSON {
readonly blockhash: UInt256Hex;
readonly confirmations: number;
readonly blocktime: string;
readonly blocktime: number;
readonly vmstate: VMStateJSON;
}

Expand Down Expand Up @@ -366,22 +366,14 @@ export interface ContractGroupJSON {
readonly signature: string;
}

export interface ContractPermissionDescriptorJSON {
readonly hashOrGroup: string;
readonly isHash: boolean;
readonly isGroup: boolean;
readonly isWildcard: boolean;
}
export type ContractPermissionDescriptorJSON = string;

export interface ContractPermissionJSON {
readonly contract: ContractPermissionDescriptorJSON;
readonly methods: WildcardContainerJSON;
}

export interface ContractManifestJSON {
readonly hash: string;
// TODO: only hash is included in old `ContractJSON` definition. Remove hashHex?
readonly hashHex: string;
readonly abi: ContractABIJSON;
readonly groups: readonly ContractGroupJSON[];
readonly permissions: readonly ContractPermissionJSON[];
Expand All @@ -402,10 +394,9 @@ export interface ContractParameterDefinitionJSON {

export interface ContractJSON {
readonly id: number;
readonly hash: string;
readonly script: string;
readonly manifest: ContractManifestJSON;
readonly hasStorage: boolean;
readonly payable: boolean;
}

export interface Nep5TransfersJSON {
Expand Down Expand Up @@ -439,7 +430,7 @@ export interface BlockBaseJSON {
readonly version: number;
readonly previousblockhash: string;
readonly merkleroot: string;
readonly time: string;
readonly time: number;
readonly index: number;
readonly nextconsensus: string;
readonly nextblockhash?: string;
Expand Down Expand Up @@ -467,7 +458,7 @@ export interface TrimmedBlockJSON extends BlockBaseJSON {
}

export interface NetworkSettingsJSON {
readonly decrementinternal: number;
readonly decrementinterval: number;
readonly generationamount: readonly number[];
readonly privatekeyversion: number;
readonly standbyvalidators: readonly string[];
Expand Down Expand Up @@ -516,9 +507,13 @@ export interface RelayTransactionResultJSON {
readonly verifyResult?: VerifyResultJSON;
}

export interface SendRawTransactionResultJSON {
readonly hash: string;
}

export interface ValidatorJSON {
readonly active: boolean;
readonly publicKey: string;
readonly publickey: string;
readonly votes: string;
}

Expand Down
Loading

0 comments on commit c3045e1

Please sign in to comment.