Support encoding and decoding of NegativeUNL pseudo-transactions (#89)

* Support encoding and decoding of NegativeUNL pseudo-transactions
This commit is contained in:
Nathan Nichols
2020-08-13 12:20:12 -05:00
parent 2e6c68ba73
commit 30ff63e653
8 changed files with 161 additions and 17 deletions

View File

@@ -40,7 +40,8 @@
"Check": 67,
"Nickname": 110,
"Contract": 99,
"GeneratorMap": 103
"GeneratorMap": 103,
"NegativeUNL": 78
},
"FIELDS": [
[
@@ -1023,6 +1024,36 @@
"type": "Blob"
}
],
[
"UNLModifyValidator",
{
"nth": 19,
"isVLEncoded": true,
"isSerialized": true,
"isSigningField": true,
"type": "Blob"
}
],
[
"ValidatorToDisable",
{
"nth": 20,
"isVLEncoded": true,
"isSerialized": true,
"isSigningField": true,
"type": "Blob"
}
],
[
"ValidatorToReEnable",
{
"nth": 20,
"isVLEncoded": true,
"isSerialized": true,
"isSigningField": true,
"type": "Blob"
}
],
[
"Account",
{
@@ -1233,6 +1264,16 @@
"type": "STObject"
}
],
[
"DisabledValidator",
{
"nth": 19,
"isVLEncoded": false,
"isSerialized": true,
"isSigningField": true,
"type": "STObject"
}
],
[
"ArrayEndMarker",
{
@@ -1323,6 +1364,16 @@
"type": "STArray"
}
],
[
"DisabledValidators",
{
"nth": 17,
"isVLEncoded": false,
"isSerialized": true,
"isSigningField": true,
"type": "STArray"
}
],
[
"CloseResolution",
{
@@ -1483,6 +1534,16 @@
"type": "UInt32"
}
],
[
"BeginLedgerSeq",
{
"nth": 40,
"isVLEncoded": false,
"isSerialized": true,
"isSigningField": true,
"type": "UInt32"
}
],
[
"Channel",
{
@@ -1523,6 +1584,16 @@
"type": "UInt8"
}
],
[
"UNLModifyDisabling",
{
"nth": 17,
"isVLEncoded": false,
"isSerialized": true,
"isSigningField": true,
"type": "UInt8"
}
],
[
"DestinationNode",
{
@@ -1685,6 +1756,7 @@
"AccountDelete": 21,
"EnableAmendment": 100,
"SetFee": 101
"SetFee": 101,
"UNLModify": 102
}
}

View File

@@ -7,7 +7,7 @@ import { Hash160 } from "./hash-160";
class AccountID extends Hash160 {
static readonly defaultAccountID: AccountID = new AccountID(Buffer.alloc(20));
constructor(bytes: Buffer) {
constructor(bytes?: Buffer) {
super(bytes ?? AccountID.defaultAccountID.bytes);
}
@@ -23,6 +23,10 @@ class AccountID extends Hash160 {
}
if (typeof value === "string") {
if (value === "") {
return new AccountID();
}
return /^r/.test(value)
? this.fromBase58(value)
: new AccountID(Buffer.from(value, "hex"));

View File

@@ -7,7 +7,11 @@ class Hash160 extends Hash {
static readonly width = 20;
static readonly ZERO_160: Hash160 = new Hash160(Buffer.alloc(Hash160.width));
constructor(bytes: Buffer) {
constructor(bytes?: Buffer) {
if (bytes && bytes.byteLength === 0) {
bytes = Hash160.ZERO_160.bytes;
}
super(bytes ?? Hash160.ZERO_160.bytes);
}
}

View File

@@ -9,24 +9,26 @@ class Hash extends Comparable {
constructor(bytes: Buffer) {
super(bytes);
if (this.bytes.byteLength !== (this.constructor as typeof Hash).width) {
throw new Error(`Invalid Hash length ${this.bytes.byteLength}`);
}
}
/**
* Construct a Hash object from an existing Hash object or a hex-string
*
* @param value A hash object or hex-string of a hash
*/
*/
static from<T extends Hash | string>(value: T): Hash {
if(value instanceof this) {
return value
if (value instanceof this) {
return value;
}
if(typeof value === "string") {
if (typeof value === "string") {
return new this(Buffer.from(value, "hex"));
}
throw new Error("Cannot construct Hash from given value");
}
/**

View File

@@ -29,9 +29,10 @@ interface HopObject extends JsonObject {
* TypeGuard for HopObject
*/
function isHopObject(arg): arg is HopObject {
return (arg.issuer !== undefined ||
arg.account !== undefined ||
arg.currency !== undefined
return (
arg.issuer !== undefined ||
arg.account !== undefined ||
arg.currency !== undefined
);
}
@@ -40,9 +41,9 @@ function isHopObject(arg): arg is HopObject {
*/
function isPathSet(arg): arg is Array<Array<HopObject>> {
return (
Array.isArray(arg) && arg.length === 0 ||
Array.isArray(arg) && Array.isArray(arg[0]) && arg[0].length === 0 ||
Array.isArray(arg) && Array.isArray(arg[0]) && isHopObject(arg[0][0])
(Array.isArray(arg) && arg.length === 0) ||
(Array.isArray(arg) && Array.isArray(arg[0]) && arg[0].length === 0) ||
(Array.isArray(arg) && Array.isArray(arg[0]) && isHopObject(arg[0][0]))
);
}