phantasma-sdk-ts 0.1.72 → 0.1.74
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/core/vm/ScriptBuilder.d.ts +5 -5
- package/core/vm/ScriptBuilder.d.ts.map +1 -1
- package/core/vm/ScriptBuilder.js +31 -37
- package/justfile +15 -0
- package/package.json +1 -1
- package/src/core/vm/ScriptBuilder.ts +44 -63
- package/tests/tx/Transaction.test.ts +6 -11
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { ISerializable } from
|
|
2
|
-
import { Address, PBinaryWriter, Timestamp } from
|
|
3
|
-
import { Opcode } from
|
|
4
|
-
import { VMObject } from
|
|
5
|
-
import { VMType } from
|
|
1
|
+
import { ISerializable } from '../interfaces';
|
|
2
|
+
import { Address, PBinaryWriter, Timestamp } from '../types';
|
|
3
|
+
import { Opcode } from './Opcode';
|
|
4
|
+
import { VMObject } from './VMObject';
|
|
5
|
+
import { VMType } from './VMType';
|
|
6
6
|
type byte = number;
|
|
7
7
|
export declare class ScriptBuilder {
|
|
8
8
|
_labelLocations: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ScriptBuilder.d.ts","sourceRoot":"","sources":["../../src/core/vm/ScriptBuilder.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAC9C,OAAO,EAAE,OAAO,EAAE,aAAa,EAAiB,SAAS,EAAE,MAAM,UAAU,CAAC;AAQ5E,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAGlC,KAAK,IAAI,GAAG,MAAM,CAAC;AAInB,qBAAa,aAAa;IACxB,eAAe,EAAE;QAAE,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAM;IAC/C,cAAc,EAAE;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ED
|
@@ -89,15 +89,15 @@ var ScriptBuilder = /** @class */ (function () {
|
|
|
89
89
|
function ScriptBuilder() {
|
|
90
90
|
this._labelLocations = {};
|
|
91
91
|
this._jumpLocations = {};
|
|
92
|
-
this.NullAddress =
|
|
93
|
-
this.str =
|
|
92
|
+
this.NullAddress = 'S1111111111111111111111111111111111';
|
|
93
|
+
this.str = '';
|
|
94
94
|
this.writer = new types_1.PBinaryWriter();
|
|
95
95
|
}
|
|
96
96
|
ScriptBuilder.ScriptBuilder = function () {
|
|
97
97
|
return new ScriptBuilder();
|
|
98
98
|
};
|
|
99
99
|
ScriptBuilder.prototype.BeginScript = function () {
|
|
100
|
-
this.str =
|
|
100
|
+
this.str = '';
|
|
101
101
|
this.writer = new types_1.PBinaryWriter();
|
|
102
102
|
return this;
|
|
103
103
|
};
|
|
@@ -139,16 +139,16 @@ var ScriptBuilder = /** @class */ (function () {
|
|
|
139
139
|
};
|
|
140
140
|
ScriptBuilder.prototype.EmitBigInteger = function (value) {
|
|
141
141
|
var bytes = [];
|
|
142
|
-
if (value ==
|
|
142
|
+
if (value == '0') {
|
|
143
143
|
bytes = [0];
|
|
144
144
|
}
|
|
145
|
-
else if (value.startsWith(
|
|
146
|
-
throw new Error(
|
|
145
|
+
else if (value.startsWith('-1')) {
|
|
146
|
+
throw new Error('Unsigned bigint serialization not suppoted');
|
|
147
147
|
}
|
|
148
148
|
else {
|
|
149
149
|
var hex = BigInt(value).toString(16);
|
|
150
150
|
if (hex.length % 2)
|
|
151
|
-
hex =
|
|
151
|
+
hex = '0' + hex;
|
|
152
152
|
var len = hex.length / 2;
|
|
153
153
|
var i = 0;
|
|
154
154
|
var j = 0;
|
|
@@ -178,30 +178,32 @@ var ScriptBuilder = /** @class */ (function () {
|
|
|
178
178
|
ScriptBuilder.prototype.EmitLoad = function (reg, obj) {
|
|
179
179
|
var structType = Object.getPrototypeOf(obj).constructor.name;
|
|
180
180
|
switch (typeof obj) {
|
|
181
|
-
case
|
|
181
|
+
case 'string': {
|
|
182
182
|
var bytes = this.RawString(obj);
|
|
183
183
|
this.EmitLoadBytes(reg, bytes, VMType_1.VMType.String);
|
|
184
184
|
break;
|
|
185
185
|
}
|
|
186
|
-
case
|
|
186
|
+
case 'boolean': {
|
|
187
187
|
var bytes = [obj ? 1 : 0];
|
|
188
188
|
this.EmitLoadBytes(reg, bytes, VMType_1.VMType.Bool);
|
|
189
189
|
break;
|
|
190
190
|
}
|
|
191
|
-
case
|
|
191
|
+
case 'number': {
|
|
192
192
|
// obj is BigInteger
|
|
193
193
|
// var bytes = val.ToSignedByteArray();
|
|
194
194
|
// this.emitLoadBytes(reg, bytes, VMType.Number);
|
|
195
195
|
//let bytes = this.RawString(BigInt(obj).toString());
|
|
196
|
-
if (Object.getPrototypeOf(structType).constructor.name ==
|
|
196
|
+
if (Object.getPrototypeOf(structType).constructor.name == 'enum') {
|
|
197
197
|
this.AppendByte(obj);
|
|
198
198
|
}
|
|
199
199
|
else {
|
|
200
|
-
this.EmitLoadVarInt(reg, obj);
|
|
200
|
+
// this.EmitLoadVarInt(reg, obj);
|
|
201
|
+
var bytes = this.RawString(obj.toString());
|
|
202
|
+
this.EmitLoadBytes(reg, bytes, VMType_1.VMType.String);
|
|
201
203
|
}
|
|
202
204
|
break;
|
|
203
205
|
}
|
|
204
|
-
case
|
|
206
|
+
case 'object':
|
|
205
207
|
if (obj instanceof Uint8Array) {
|
|
206
208
|
this.EmitLoadBytes(reg, Array.from(obj));
|
|
207
209
|
}
|
|
@@ -217,8 +219,7 @@ var ScriptBuilder = /** @class */ (function () {
|
|
|
217
219
|
else if (obj instanceof types_1.Address) {
|
|
218
220
|
this.EmitLoadAddress(reg, obj);
|
|
219
221
|
}
|
|
220
|
-
else if ((typeof obj.UnserializeData ===
|
|
221
|
-
typeof obj.SerializeData === "function") ||
|
|
222
|
+
else if ((typeof obj.UnserializeData === 'function' && typeof obj.SerializeData === 'function') ||
|
|
222
223
|
obj instanceof interfaces_1.ISerializable) {
|
|
223
224
|
this.EmitLoadISerializable(reg, obj);
|
|
224
225
|
}
|
|
@@ -227,19 +228,19 @@ var ScriptBuilder = /** @class */ (function () {
|
|
|
227
228
|
this.EmitLoadArray(reg, obj);
|
|
228
229
|
}
|
|
229
230
|
else {
|
|
230
|
-
throw Error(
|
|
231
|
+
throw Error('Load type ' + typeof obj + ' not supported');
|
|
231
232
|
}
|
|
232
233
|
}
|
|
233
234
|
break;
|
|
234
235
|
default:
|
|
235
|
-
throw Error(
|
|
236
|
+
throw Error('Load type ' + typeof obj + ' not supported');
|
|
236
237
|
}
|
|
237
238
|
return this;
|
|
238
239
|
};
|
|
239
240
|
ScriptBuilder.prototype.EmitLoadBytes = function (reg, bytes, type) {
|
|
240
241
|
if (type === void 0) { type = VMType_1.VMType.Bytes; }
|
|
241
242
|
if (bytes.length > 0xffff)
|
|
242
|
-
throw new Error(
|
|
243
|
+
throw new Error('tried to load too much data');
|
|
243
244
|
this.Emit(Opcode_1.Opcode.LOAD);
|
|
244
245
|
this.AppendByte(reg);
|
|
245
246
|
this.AppendByte(type);
|
|
@@ -277,8 +278,7 @@ var ScriptBuilder = /** @class */ (function () {
|
|
|
277
278
|
this.AppendByte(obj.Type);
|
|
278
279
|
if (result == undefined) {
|
|
279
280
|
//console.log("enter");
|
|
280
|
-
if (obj.Data instanceof Map ||
|
|
281
|
-
obj.Data instanceof Map && obj.Data instanceof VMObject_1.VMObject) {
|
|
281
|
+
if (obj.Data instanceof Map || (obj.Data instanceof Map && obj.Data instanceof VMObject_1.VMObject)) {
|
|
282
282
|
var resultData = obj.Data;
|
|
283
283
|
this.EmitVarInt(resultData.size);
|
|
284
284
|
try {
|
|
@@ -388,7 +388,7 @@ var ScriptBuilder = /** @class */ (function () {
|
|
|
388
388
|
this.Emit(opcode);
|
|
389
389
|
break;
|
|
390
390
|
default:
|
|
391
|
-
throw new Error(
|
|
391
|
+
throw new Error('Invalid jump opcode: ' + opcode);
|
|
392
392
|
}
|
|
393
393
|
if (opcode != Opcode_1.Opcode.JMP) {
|
|
394
394
|
this.AppendByte(reg);
|
|
@@ -400,7 +400,7 @@ var ScriptBuilder = /** @class */ (function () {
|
|
|
400
400
|
};
|
|
401
401
|
ScriptBuilder.prototype.EmitCall = function (label, regCount) {
|
|
402
402
|
if (regCount < 1 || regCount > MaxRegisterCount) {
|
|
403
|
-
throw new Error(
|
|
403
|
+
throw new Error('Invalid number of registers');
|
|
404
404
|
}
|
|
405
405
|
var ofs = this.str.length; //(int)stream.Position;
|
|
406
406
|
ofs += 2;
|
|
@@ -412,7 +412,7 @@ var ScriptBuilder = /** @class */ (function () {
|
|
|
412
412
|
};
|
|
413
413
|
ScriptBuilder.prototype.EmitConditionalJump = function (opcode, src_reg, label) {
|
|
414
414
|
if (opcode != Opcode_1.Opcode.JMPIF && opcode != Opcode_1.Opcode.JMPNOT) {
|
|
415
|
-
throw new Error(
|
|
415
|
+
throw new Error('Opcode is not a conditional jump');
|
|
416
416
|
}
|
|
417
417
|
var ofs = this.str.length;
|
|
418
418
|
ofs += 2;
|
|
@@ -451,20 +451,15 @@ var ScriptBuilder = /** @class */ (function () {
|
|
|
451
451
|
};
|
|
452
452
|
//#region ScriptBuilderExtensions
|
|
453
453
|
ScriptBuilder.prototype.AllowGas = function (from, to, gasPrice, gasLimit) {
|
|
454
|
-
return this.CallContract(Contracts_1.Contracts.GasContractName,
|
|
455
|
-
from,
|
|
456
|
-
to,
|
|
457
|
-
gasPrice,
|
|
458
|
-
gasLimit,
|
|
459
|
-
]);
|
|
454
|
+
return this.CallContract(Contracts_1.Contracts.GasContractName, 'AllowGas', [from, to, gasPrice, gasLimit]);
|
|
460
455
|
};
|
|
461
456
|
ScriptBuilder.prototype.SpendGas = function (address) {
|
|
462
|
-
return this.CallContract(Contracts_1.Contracts.GasContractName,
|
|
457
|
+
return this.CallContract(Contracts_1.Contracts.GasContractName, 'SpendGas', [address]);
|
|
463
458
|
};
|
|
464
459
|
ScriptBuilder.prototype.CallRPC = function (methodName, params) {
|
|
465
460
|
return __awaiter(this, void 0, void 0, function () {
|
|
466
461
|
return __generator(this, function (_a) {
|
|
467
|
-
return [2 /*return*/,
|
|
462
|
+
return [2 /*return*/, 'bla'];
|
|
468
463
|
});
|
|
469
464
|
});
|
|
470
465
|
};
|
|
@@ -475,7 +470,7 @@ var ScriptBuilder = /** @class */ (function () {
|
|
|
475
470
|
switch (_a.label) {
|
|
476
471
|
case 0:
|
|
477
472
|
params = [address, chainInput];
|
|
478
|
-
return [4 /*yield*/, this.CallRPC(
|
|
473
|
+
return [4 /*yield*/, this.CallRPC('getAddressTransactionCount', params)];
|
|
479
474
|
case 1: return [2 /*return*/, _a.sent()];
|
|
480
475
|
}
|
|
481
476
|
});
|
|
@@ -505,7 +500,7 @@ var ScriptBuilder = /** @class */ (function () {
|
|
|
505
500
|
};
|
|
506
501
|
ScriptBuilder.prototype.EmitVarInt = function (value) {
|
|
507
502
|
if (value < 0)
|
|
508
|
-
throw
|
|
503
|
+
throw 'negative value invalid';
|
|
509
504
|
if (value < 0xfd) {
|
|
510
505
|
this.AppendByte(value);
|
|
511
506
|
}
|
|
@@ -543,7 +538,7 @@ var ScriptBuilder = /** @class */ (function () {
|
|
|
543
538
|
};
|
|
544
539
|
ScriptBuilder.prototype.EmitUInt32 = function (value) {
|
|
545
540
|
if (value < 0)
|
|
546
|
-
throw
|
|
541
|
+
throw 'negative value invalid';
|
|
547
542
|
var D = (value & 0xff000000) >> 24;
|
|
548
543
|
var C = (value & 0x00ff0000) >> 16;
|
|
549
544
|
var B = (value & 0x0000ff00) >> 8;
|
|
@@ -564,7 +559,7 @@ var ScriptBuilder = /** @class */ (function () {
|
|
|
564
559
|
};
|
|
565
560
|
//Custom Modified
|
|
566
561
|
ScriptBuilder.prototype.ByteToHex = function (byte) {
|
|
567
|
-
var result = (
|
|
562
|
+
var result = ('0' + (byte & 0xff).toString(16)).slice(-2);
|
|
568
563
|
return result;
|
|
569
564
|
};
|
|
570
565
|
ScriptBuilder.prototype.AppendByte = function (byte) {
|
|
@@ -578,8 +573,7 @@ var ScriptBuilder = /** @class */ (function () {
|
|
|
578
573
|
}
|
|
579
574
|
};
|
|
580
575
|
ScriptBuilder.prototype.AppendUshort = function (ushort) {
|
|
581
|
-
this.str +=
|
|
582
|
-
this.ByteToHex(ushort & 0xff) + this.ByteToHex((ushort >> 8) & 0xff);
|
|
576
|
+
this.str += this.ByteToHex(ushort & 0xff) + this.ByteToHex((ushort >> 8) & 0xff);
|
|
583
577
|
this.writer.writeUnsignedShort(ushort);
|
|
584
578
|
};
|
|
585
579
|
ScriptBuilder.prototype.AppendHexEncoded = function (bytes) {
|
package/justfile
ADDED
package/package.json
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import base58 from
|
|
2
|
-
import { ISerializable } from
|
|
3
|
-
import { Address, PBinaryWriter, Serialization, Timestamp } from
|
|
1
|
+
import base58 from 'bs58';
|
|
2
|
+
import { ISerializable } from '../interfaces';
|
|
3
|
+
import { Address, PBinaryWriter, Serialization, Timestamp } from '../types';
|
|
4
4
|
import {
|
|
5
5
|
bigIntToByteArray,
|
|
6
6
|
numberToByteArray,
|
|
7
7
|
stringToUint8Array,
|
|
8
8
|
uint8ArrayToBytes,
|
|
9
9
|
uint8ArrayToHex,
|
|
10
|
-
} from
|
|
11
|
-
import { Opcode } from
|
|
12
|
-
import { VMObject } from
|
|
13
|
-
import { VMType } from
|
|
14
|
-
import { Contracts } from
|
|
10
|
+
} from '../utils';
|
|
11
|
+
import { Opcode } from './Opcode';
|
|
12
|
+
import { VMObject } from './VMObject';
|
|
13
|
+
import { VMType } from './VMType';
|
|
14
|
+
import { Contracts } from './Contracts';
|
|
15
15
|
|
|
16
16
|
type byte = number;
|
|
17
17
|
|
|
@@ -25,19 +25,19 @@ export class ScriptBuilder {
|
|
|
25
25
|
|
|
26
26
|
public writer: PBinaryWriter;
|
|
27
27
|
|
|
28
|
-
public NullAddress =
|
|
28
|
+
public NullAddress = 'S1111111111111111111111111111111111';
|
|
29
29
|
|
|
30
|
-
public static ScriptBuilder()
|
|
30
|
+
public static ScriptBuilder(): ScriptBuilder {
|
|
31
31
|
return new ScriptBuilder();
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
public constructor() {
|
|
35
|
-
this.str =
|
|
35
|
+
this.str = '';
|
|
36
36
|
this.writer = new PBinaryWriter();
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
public BeginScript() {
|
|
40
|
-
this.str =
|
|
40
|
+
this.str = '';
|
|
41
41
|
this.writer = new PBinaryWriter();
|
|
42
42
|
return this;
|
|
43
43
|
}
|
|
@@ -87,13 +87,13 @@ export class ScriptBuilder {
|
|
|
87
87
|
public EmitBigInteger(value: string) {
|
|
88
88
|
let bytes: number[] = [];
|
|
89
89
|
|
|
90
|
-
if (value ==
|
|
90
|
+
if (value == '0') {
|
|
91
91
|
bytes = [0];
|
|
92
|
-
} else if (value.startsWith(
|
|
93
|
-
throw new Error(
|
|
92
|
+
} else if (value.startsWith('-1')) {
|
|
93
|
+
throw new Error('Unsigned bigint serialization not suppoted');
|
|
94
94
|
} else {
|
|
95
95
|
let hex = BigInt(value).toString(16);
|
|
96
|
-
if (hex.length % 2) hex =
|
|
96
|
+
if (hex.length % 2) hex = '0' + hex;
|
|
97
97
|
const len = hex.length / 2;
|
|
98
98
|
var i = 0;
|
|
99
99
|
var j = 0;
|
|
@@ -127,32 +127,34 @@ export class ScriptBuilder {
|
|
|
127
127
|
let structType = Object.getPrototypeOf(obj).constructor.name;
|
|
128
128
|
|
|
129
129
|
switch (typeof obj) {
|
|
130
|
-
case
|
|
130
|
+
case 'string': {
|
|
131
131
|
let bytes = this.RawString(obj);
|
|
132
132
|
this.EmitLoadBytes(reg, bytes, VMType.String);
|
|
133
133
|
break;
|
|
134
134
|
}
|
|
135
135
|
|
|
136
|
-
case
|
|
136
|
+
case 'boolean': {
|
|
137
137
|
let bytes = [(obj as boolean) ? 1 : 0];
|
|
138
138
|
this.EmitLoadBytes(reg, bytes, VMType.Bool);
|
|
139
139
|
break;
|
|
140
140
|
}
|
|
141
141
|
|
|
142
|
-
case
|
|
142
|
+
case 'number': {
|
|
143
143
|
// obj is BigInteger
|
|
144
144
|
// var bytes = val.ToSignedByteArray();
|
|
145
145
|
// this.emitLoadBytes(reg, bytes, VMType.Number);
|
|
146
146
|
//let bytes = this.RawString(BigInt(obj).toString());
|
|
147
|
-
if (Object.getPrototypeOf(structType).constructor.name ==
|
|
147
|
+
if (Object.getPrototypeOf(structType).constructor.name == 'enum') {
|
|
148
148
|
this.AppendByte(obj);
|
|
149
149
|
} else {
|
|
150
|
-
this.EmitLoadVarInt(reg, obj);
|
|
150
|
+
// this.EmitLoadVarInt(reg, obj);
|
|
151
|
+
let bytes = this.RawString(obj.toString());
|
|
152
|
+
this.EmitLoadBytes(reg, bytes, VMType.String);
|
|
151
153
|
}
|
|
152
154
|
break;
|
|
153
155
|
}
|
|
154
156
|
|
|
155
|
-
case
|
|
157
|
+
case 'object':
|
|
156
158
|
if (obj instanceof Uint8Array) {
|
|
157
159
|
this.EmitLoadBytes(reg, Array.from(obj));
|
|
158
160
|
} else if (obj instanceof VMObject) {
|
|
@@ -164,8 +166,7 @@ export class ScriptBuilder {
|
|
|
164
166
|
} else if (obj instanceof Address) {
|
|
165
167
|
this.EmitLoadAddress(reg, obj);
|
|
166
168
|
} else if (
|
|
167
|
-
(typeof obj.UnserializeData ===
|
|
168
|
-
typeof obj.SerializeData === "function") ||
|
|
169
|
+
(typeof obj.UnserializeData === 'function' && typeof obj.SerializeData === 'function') ||
|
|
169
170
|
obj instanceof ISerializable
|
|
170
171
|
) {
|
|
171
172
|
this.EmitLoadISerializable(reg, obj);
|
|
@@ -173,22 +174,18 @@ export class ScriptBuilder {
|
|
|
173
174
|
if (Array.isArray(obj)) {
|
|
174
175
|
this.EmitLoadArray(reg, obj);
|
|
175
176
|
} else {
|
|
176
|
-
throw Error(
|
|
177
|
+
throw Error('Load type ' + typeof obj + ' not supported');
|
|
177
178
|
}
|
|
178
179
|
}
|
|
179
180
|
break;
|
|
180
181
|
default:
|
|
181
|
-
throw Error(
|
|
182
|
+
throw Error('Load type ' + typeof obj + ' not supported');
|
|
182
183
|
}
|
|
183
184
|
return this;
|
|
184
185
|
}
|
|
185
186
|
|
|
186
|
-
public EmitLoadBytes(
|
|
187
|
-
|
|
188
|
-
bytes: byte[],
|
|
189
|
-
type: VMType = VMType.Bytes
|
|
190
|
-
): this {
|
|
191
|
-
if (bytes.length > 0xffff) throw new Error("tried to load too much data");
|
|
187
|
+
public EmitLoadBytes(reg: number, bytes: byte[], type: VMType = VMType.Bytes): this {
|
|
188
|
+
if (bytes.length > 0xffff) throw new Error('tried to load too much data');
|
|
192
189
|
this.Emit(Opcode.LOAD);
|
|
193
190
|
this.AppendByte(reg);
|
|
194
191
|
this.AppendByte(type);
|
|
@@ -234,10 +231,7 @@ export class ScriptBuilder {
|
|
|
234
231
|
|
|
235
232
|
if (result == undefined) {
|
|
236
233
|
//console.log("enter");
|
|
237
|
-
if (
|
|
238
|
-
obj.Data instanceof Map ||
|
|
239
|
-
obj.Data instanceof Map && obj.Data instanceof VMObject
|
|
240
|
-
) {
|
|
234
|
+
if (obj.Data instanceof Map || (obj.Data instanceof Map && obj.Data instanceof VMObject)) {
|
|
241
235
|
let resultData = obj.Data as Map<VMObject, VMObject>;
|
|
242
236
|
this.EmitVarInt(resultData.size);
|
|
243
237
|
for (let entry of resultData) {
|
|
@@ -296,7 +290,7 @@ export class ScriptBuilder {
|
|
|
296
290
|
let bytes = Array.from(Serialization.Serialize(obj));
|
|
297
291
|
this.EmitLoadBytes(reg, bytes, VMType.Timestamp);
|
|
298
292
|
} else if (obj instanceof Date) {
|
|
299
|
-
let num = (obj.getTime()
|
|
293
|
+
let num = (obj.getTime() / 1000) | 0;
|
|
300
294
|
|
|
301
295
|
let a = (num & 0xff000000) >> 24;
|
|
302
296
|
let b = (num & 0x00ff0000) >> 16;
|
|
@@ -350,7 +344,7 @@ export class ScriptBuilder {
|
|
|
350
344
|
break;
|
|
351
345
|
|
|
352
346
|
default:
|
|
353
|
-
throw new Error(
|
|
347
|
+
throw new Error('Invalid jump opcode: ' + opcode);
|
|
354
348
|
}
|
|
355
349
|
|
|
356
350
|
if (opcode != Opcode.JMP) {
|
|
@@ -365,7 +359,7 @@ export class ScriptBuilder {
|
|
|
365
359
|
|
|
366
360
|
public EmitCall(label: string, regCount: byte): this {
|
|
367
361
|
if (regCount < 1 || regCount > MaxRegisterCount) {
|
|
368
|
-
throw new Error(
|
|
362
|
+
throw new Error('Invalid number of registers');
|
|
369
363
|
}
|
|
370
364
|
|
|
371
365
|
var ofs = this.str.length; //(int)stream.Position;
|
|
@@ -378,13 +372,9 @@ export class ScriptBuilder {
|
|
|
378
372
|
return this;
|
|
379
373
|
}
|
|
380
374
|
|
|
381
|
-
public EmitConditionalJump(
|
|
382
|
-
opcode: Opcode,
|
|
383
|
-
src_reg: byte,
|
|
384
|
-
label: string
|
|
385
|
-
): this {
|
|
375
|
+
public EmitConditionalJump(opcode: Opcode, src_reg: byte, label: string): this {
|
|
386
376
|
if (opcode != Opcode.JMPIF && opcode != Opcode.JMPNOT) {
|
|
387
|
-
throw new Error(
|
|
377
|
+
throw new Error('Opcode is not a conditional jump');
|
|
388
378
|
}
|
|
389
379
|
|
|
390
380
|
var ofs = this.str.length;
|
|
@@ -440,28 +430,20 @@ export class ScriptBuilder {
|
|
|
440
430
|
gasPrice: number,
|
|
441
431
|
gasLimit: number
|
|
442
432
|
): this {
|
|
443
|
-
return this.CallContract(Contracts.GasContractName,
|
|
444
|
-
from,
|
|
445
|
-
to,
|
|
446
|
-
gasPrice,
|
|
447
|
-
gasLimit,
|
|
448
|
-
]);
|
|
433
|
+
return this.CallContract(Contracts.GasContractName, 'AllowGas', [from, to, gasPrice, gasLimit]);
|
|
449
434
|
}
|
|
450
435
|
|
|
451
436
|
public SpendGas(address: string | Address): this {
|
|
452
|
-
return this.CallContract(Contracts.GasContractName,
|
|
437
|
+
return this.CallContract(Contracts.GasContractName, 'SpendGas', [address]);
|
|
453
438
|
}
|
|
454
439
|
|
|
455
440
|
async CallRPC<T>(methodName: string, params: any[]): Promise<T> {
|
|
456
|
-
return
|
|
441
|
+
return 'bla' as unknown as T;
|
|
457
442
|
}
|
|
458
443
|
|
|
459
|
-
async GetAddressTransactionCount(
|
|
460
|
-
address: string,
|
|
461
|
-
chainInput: string
|
|
462
|
-
): Promise<number> {
|
|
444
|
+
async GetAddressTransactionCount(address: string, chainInput: string): Promise<number> {
|
|
463
445
|
let params = [address, chainInput];
|
|
464
|
-
return await this.CallRPC<number>(
|
|
446
|
+
return await this.CallRPC<number>('getAddressTransactionCount', params);
|
|
465
447
|
}
|
|
466
448
|
|
|
467
449
|
//#endregion
|
|
@@ -493,7 +475,7 @@ export class ScriptBuilder {
|
|
|
493
475
|
}
|
|
494
476
|
|
|
495
477
|
public EmitVarInt(value: number): this {
|
|
496
|
-
if (value < 0) throw
|
|
478
|
+
if (value < 0) throw 'negative value invalid';
|
|
497
479
|
|
|
498
480
|
if (value < 0xfd) {
|
|
499
481
|
this.AppendByte(value);
|
|
@@ -532,7 +514,7 @@ export class ScriptBuilder {
|
|
|
532
514
|
}
|
|
533
515
|
|
|
534
516
|
public EmitUInt32(value: number): this {
|
|
535
|
-
if (value < 0) throw
|
|
517
|
+
if (value < 0) throw 'negative value invalid';
|
|
536
518
|
|
|
537
519
|
let D = (value & 0xff000000) >> 24;
|
|
538
520
|
let C = (value & 0x00ff0000) >> 16;
|
|
@@ -558,7 +540,7 @@ export class ScriptBuilder {
|
|
|
558
540
|
|
|
559
541
|
//Custom Modified
|
|
560
542
|
ByteToHex(byte: number) {
|
|
561
|
-
let result = (
|
|
543
|
+
let result = ('0' + (byte & 0xff).toString(16)).slice(-2);
|
|
562
544
|
return result;
|
|
563
545
|
}
|
|
564
546
|
|
|
@@ -575,8 +557,7 @@ export class ScriptBuilder {
|
|
|
575
557
|
}
|
|
576
558
|
|
|
577
559
|
AppendUshort(ushort: number) {
|
|
578
|
-
this.str +=
|
|
579
|
-
this.ByteToHex(ushort & 0xff) + this.ByteToHex((ushort >> 8) & 0xff);
|
|
560
|
+
this.str += this.ByteToHex(ushort & 0xff) + this.ByteToHex((ushort >> 8) & 0xff);
|
|
580
561
|
this.writer.writeUnsignedShort(ushort);
|
|
581
562
|
}
|
|
582
563
|
|
|
@@ -45,7 +45,7 @@ describe('test phantasma_ts', function () {
|
|
|
45
45
|
done();
|
|
46
46
|
});
|
|
47
47
|
|
|
48
|
-
test('
|
|
48
|
+
test('signature', function (done) {
|
|
49
49
|
let writer = new PBinaryWriter();
|
|
50
50
|
let keys = PhantasmaKeys.generate();
|
|
51
51
|
|
|
@@ -141,7 +141,7 @@ describe('test phantasma_ts', function () {
|
|
|
141
141
|
.EndScript();
|
|
142
142
|
|
|
143
143
|
expect(script).toBe(
|
|
144
|
-
'
|
|
144
|
+
'0D00040632313030303003000D000405313030303003000D000223220000000000000000000000000000000000000000000000000000000000000000000003000D000223220100AA53BE71FC41BC0889B694F4D6D03F7906A3D9A21705943CAF9632EEAFBB489503000D000408416C6C6F7747617303000D0004036761732D00012E010D0004013003000D00041D73797374656D2E6E657875732E70726F746F636F6C2E76657273696F6E03000D00042F50324B464579466576705166536157384734566A536D6857555A585234517247395951523148624D7054554370434C03000D00040A53696E676C65566F746503000D000409636F6E73656E7375732D00012E010D000223220100AA53BE71FC41BC0889B694F4D6D03F7906A3D9A21705943CAF9632EEAFBB489503000D0004085370656E6447617303000D0004036761732D00012E010B'
|
|
145
145
|
);
|
|
146
146
|
|
|
147
147
|
let tx = new Transaction(nexusName, chainName, script, date, payload);
|
|
@@ -149,7 +149,7 @@ describe('test phantasma_ts', function () {
|
|
|
149
149
|
tx.signWithKeys(keys);
|
|
150
150
|
|
|
151
151
|
expect(uint8ArrayToHex(tx.ToByteAray(true)).toUpperCase()).toBe(
|
|
152
|
-
'
|
|
152
|
+
'07746573746E6574046D61696EFD48010D00040632313030303003000D000405313030303003000D000223220000000000000000000000000000000000000000000000000000000000000000000003000D000223220100AA53BE71FC41BC0889B694F4D6D03F7906A3D9A21705943CAF9632EEAFBB489503000D000408416C6C6F7747617303000D0004036761732D00012E010D0004013003000D00041D73797374656D2E6E657875732E70726F746F636F6C2E76657273696F6E03000D00042F50324B464579466576705166536157384734566A536D6857555A585234517247395951523148624D7054554370434C03000D00040A53696E676C65566F746503000D000409636F6E73656E7375732D00012E010D000223220100AA53BE71FC41BC0889B694F4D6D03F7906A3D9A21705943CAF9632EEAFBB489503000D0004085370656E6447617303000D0004036761732D00012E010BD202964909436F6E73656E737573010140016F0F8D6C38E37F00C9CE9969104F42AF933BEB8C4291CBC9107CD11FDC6CBBDA86ACCD731742EA01642A26D14CA7E56361E73997BB3BEA55BAA3911AB62002'
|
|
153
153
|
);
|
|
154
154
|
done();
|
|
155
155
|
});
|
|
@@ -185,7 +185,7 @@ describe('test phantasma_ts', function () {
|
|
|
185
185
|
.EndScript();
|
|
186
186
|
|
|
187
187
|
expect(script).toBe(
|
|
188
|
-
'
|
|
188
|
+
'0E0000000D01042F50324B464579466576705166536157384734566A536D6857555A585234517247395951523148624D7054554370434C0D020401302F0100020D01042F50324B464579466576705166536157384734566A536D6857555A585234517247395951523148624D7054554370434C0D020401312F01000203000D0004013103000D0004114372656174655472616E73616374696F6E03000D000409636F6E73656E7375732D00012E010B'
|
|
189
189
|
);
|
|
190
190
|
|
|
191
191
|
done();
|
|
@@ -233,7 +233,7 @@ describe('test phantasma_ts', function () {
|
|
|
233
233
|
.EndScript();
|
|
234
234
|
|
|
235
235
|
expect(script).toBe(
|
|
236
|
-
'
|
|
236
|
+
'0D00040632313030303003000D00040631303030303003000D000223220000000000000000000000000000000000000000000000000000000000000000000003000D000223220100AA53BE71FC41BC0889B694F4D6D03F7906A3D9A21705943CAF9632EEAFBB489503000D000408416C6C6F7747617303000D0004036761732D00012E010E0000000D010223220100AA53BE71FC41BC0889B694F4D6D03F7906A3D9A21705943CAF9632EEAFBB48950D020401302F0100020D010223220100AA53BE71FC41BC0889B694F4D6D03F7906A3D9A21705943CAF9632EEAFBB48950D020401312F01000203000D00021907746573746E6574046D61696E00D20296490574657374650003000D000405746573746503000D00042F50324B464579466576705166536157384734566A536D6857555A585234517247395951523148624D7054554370434C03000D0004114372656174655472616E73616374696F6E03000D000409636F6E73656E7375732D00012E010D000223220100AA53BE71FC41BC0889B694F4D6D03F7906A3D9A21705943CAF9632EEAFBB489503000D0004085370656E6447617303000D0004036761732D00012E010B'
|
|
237
237
|
);
|
|
238
238
|
|
|
239
239
|
done();
|
|
@@ -258,12 +258,7 @@ describe('test phantasma_ts', function () {
|
|
|
258
258
|
let amount = 10000000;
|
|
259
259
|
let script = sb
|
|
260
260
|
.AllowGas(keys.Address.Text, Address.NullText, 10000, 21000)
|
|
261
|
-
.CallInterop('Runtime.TransferTokens', [
|
|
262
|
-
keys.Address.Text,
|
|
263
|
-
keys.Address.Text,
|
|
264
|
-
'SOUL',
|
|
265
|
-
String(amount),
|
|
266
|
-
])
|
|
261
|
+
.CallInterop('Runtime.TransferTokens', [keys.Address.Text, keys.Address.Text, 'SOUL', amount])
|
|
267
262
|
.SpendGas(keys.Address.Text)
|
|
268
263
|
.EndScript();
|
|
269
264
|
|