update readme

This commit is contained in:
2021-08-17 14:17:06 -04:00
parent 695d2ff3a0
commit cca82a32da
7 changed files with 342 additions and 23 deletions

View File

@@ -0,0 +1,5 @@
declare function strip0x(hexString: string): string;
declare function add0x(hexString: string): string;
declare function fromHex(hexString: string): Uint8Array;
declare function toHex(bytes: Uint8Array): string;
export { fromHex, toHex, strip0x, add0x, };

View File

@@ -0,0 +1,41 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.add0x = exports.strip0x = exports.toHex = exports.fromHex = void 0;
// improve
function validHex(hexString) {
return hexString;
}
function even(hexString) {
if (hexString.length % 2 != 0) {
hexString = '0' + hexString;
}
return hexString;
}
function strip0x(hexString) {
if (hexString.length < 2) {
throw new Error('invalid hex');
}
else if (hexString.substring(0, 2) == '0x') {
hexString = hexString.substring(2);
}
return validHex(even(hexString));
}
exports.strip0x = strip0x;
function add0x(hexString) {
if (hexString.length < 2) {
throw new Error('invalid hex');
}
else if (hexString.substring(0, 2) != '0x') {
hexString = '0x' + hexString;
}
return validHex(even(hexString));
}
exports.add0x = add0x;
function fromHex(hexString) {
return new Uint8Array(hexString.match(/.{1,2}/g).map(function (byte) { return parseInt(byte, 16); }));
}
exports.fromHex = fromHex;
function toHex(bytes) {
return bytes.reduce(function (str, byte) { return str + byte.toString(16).padStart(2, '0'); }, '');
}
exports.toHex = toHex;

View File

@@ -0,0 +1 @@
export { Tx } from './tx';

View File

@@ -0,0 +1,5 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Tx = void 0;
var tx_1 = require("./tx");
Object.defineProperty(exports, "Tx", { enumerable: true, get: function () { return tx_1.Tx; } });

View File

@@ -0,0 +1,29 @@
declare function toValue(n: number): bigint;
declare function stringToValue(s: string): bigint;
declare function hexToValue(hx: string): bigint;
declare class Tx {
nonce: number;
gasPrice: number;
gasLimit: number;
to: Uint8Array;
value: bigint;
data: Uint8Array;
v: number;
r: Uint8Array;
s: Uint8Array;
chainId: number;
_signatureSet: boolean;
_workBuffer: ArrayBuffer;
_outBuffer: DataView;
_outBufferCursor: number;
constructor(chainId: number);
private serializeNumber;
private write;
serializeBytes(): Uint8Array;
canonicalOrder(): Uint8Array[];
serializeRLP(): Uint8Array;
message(): Uint8Array;
setSignature(r: Uint8Array, s: Uint8Array, v: number): void;
clearSignature(): void;
}
export { Tx, stringToValue, hexToValue, toValue, };

View File

@@ -0,0 +1,121 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.toValue = exports.hexToValue = exports.stringToValue = exports.Tx = void 0;
var hex_1 = require("./hex");
var sha3_1 = require("sha3");
var RLP = require('rlp');
function isAddress(a) {
return a !== undefined && a.length == 20;
}
function toValue(n) {
return BigInt(n);
}
exports.toValue = toValue;
function stringToValue(s) {
return BigInt(s);
}
exports.stringToValue = stringToValue;
function hexToValue(hx) {
return BigInt(hex_1.add0x(hx));
}
exports.hexToValue = hexToValue;
var Tx = /** @class */ (function () {
function Tx(chainId) {
this.chainId = chainId;
this.nonce = 0;
this.gasPrice = 0;
this.gasLimit = 0;
this.to = new Uint8Array(32);
this.data = new Uint8Array(0);
this.value = BigInt(0);
this._workBuffer = new ArrayBuffer(32);
this._outBuffer = new DataView(new ArrayBuffer(1024 * 1024));
this._outBufferCursor = 0;
this.clearSignature();
}
Tx.prototype.serializeNumber = function (n) {
var view = new DataView(this._workBuffer);
view.setBigUint64(0, BigInt(0));
view.setBigUint64(0, n);
var zeroOffset = 0;
for (zeroOffset = 0; zeroOffset < 8; zeroOffset++) {
if (view.getInt8(zeroOffset) > 0) {
break;
}
}
return new Uint8Array(this._workBuffer).slice(zeroOffset, 8);
};
Tx.prototype.write = function (data) {
var _this = this;
data.forEach(function (v) {
_this._outBuffer.setInt8(_this._outBufferCursor, v);
_this._outBufferCursor++;
});
};
Tx.prototype.serializeBytes = function () {
if (!isAddress(this.to)) {
throw new Error('invalid address');
}
var nonce = this.serializeNumber(BigInt(this.nonce));
this.write(nonce);
var gasPrice = this.serializeNumber(BigInt(this.gasPrice));
this.write(gasPrice);
var gasLimit = this.serializeNumber(BigInt(this.gasLimit));
this.write(gasLimit);
this.write(this.to);
var value = this.serializeNumber(this.value);
this.write(value);
this.write(this.data);
var v = this.serializeNumber(BigInt(this.v));
this.write(v);
this.write(this.r);
this.write(this.s);
return new Uint8Array(this._outBuffer.buffer).slice(0, this._outBufferCursor);
};
Tx.prototype.canonicalOrder = function () {
return [
this.serializeNumber(BigInt(this.nonce)),
this.serializeNumber(BigInt(this.gasPrice)),
this.serializeNumber(BigInt(this.gasLimit)),
this.to,
this.serializeNumber(this.value),
this.data,
this.serializeNumber(BigInt(this.v)),
this.r,
this.s,
];
};
Tx.prototype.serializeRLP = function () {
return RLP.encode(this.canonicalOrder());
};
Tx.prototype.message = function () {
// TODO: Can we do without Buffer, pleeease?
var h = new sha3_1.Keccak(256);
var b = new Buffer(this.serializeRLP());
h.update(b);
return h.digest();
};
Tx.prototype.setSignature = function (r, s, v) {
if (this._signatureSet) {
throw new Error('Signature already set');
}
if (r.length != 32 || s.length != 32) {
throw new Error('Invalid signature length');
}
if (v < 0 || v > 3) {
throw new Error('Invalid recid');
}
this.r = r;
this.s = s;
this.v = (this.chainId * 2) + 35 + v;
this._signatureSet = true;
};
Tx.prototype.clearSignature = function () {
this.r = new Uint8Array(0);
this.s = new Uint8Array(0);
this.v = this.chainId;
this._signatureSet = false;
};
return Tx;
}());
exports.Tx = Tx;