Skip to content

Commit dedfd9f

Browse files
authored
Merge pull request #142 from ltonetwork/set-script
SetScript transaction type
2 parents 1d32d07 + 8af253b commit dedfd9f

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

src/node/PublicNode.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Transaction from '../transactions/Transaction';
22
import { txFromData } from '../transactions';
33
import { ITxJSON } from '../types';
44
import { RequestError } from '../errors';
5+
import SetScript from '../transactions/SetScript';
56

67
export default class PublicNode {
78
readonly url: string;
@@ -49,6 +50,11 @@ export default class PublicNode {
4950
return txFromData(data as ITxJSON) as T;
5051
}
5152

53+
async compile(code: string): Promise<SetScript> {
54+
const compiled = await this.post('/utils/script/compile', code);
55+
return new SetScript(compiled.script);
56+
}
57+
5258
status(): Promise<{
5359
blockchainHeight: number;
5460
stateHeight: number;

src/transactions/SetScript.ts

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import Transaction from './Transaction';
2+
import { base58, base64 } from '@scure/base';
3+
import * as convert from '../utils/convert';
4+
import { concatBytes } from '@noble/hashes/utils';
5+
import { keyTypeId } from '../utils/crypto';
6+
import { ITxJSON } from '../types';
7+
8+
const BASE_FEE = 500000000;
9+
const DEFAULT_VERSION = 3;
10+
11+
export default class SetScript extends Transaction {
12+
static readonly TYPE = 13;
13+
14+
script?: string;
15+
16+
constructor(compiledScript?: string) {
17+
super(SetScript.TYPE, DEFAULT_VERSION);
18+
19+
this.script = compiledScript;
20+
this.fee = BASE_FEE;
21+
}
22+
23+
private toBinaryV1(): Uint8Array {
24+
const scriptBytes = this.script ? base64.decode(this.script.slice(7)) : undefined;
25+
26+
return concatBytes(
27+
Uint8Array.from([this.type, 1]),
28+
convert.stringToByteArray(this.networkId),
29+
base58.decode(this.senderPublicKey!),
30+
scriptBytes
31+
? concatBytes(Uint8Array.from([1]), convert.shortToByteArray(scriptBytes.length), scriptBytes)
32+
: Uint8Array.from([0]),
33+
convert.longToByteArray(this.fee),
34+
convert.longToByteArray(this.timestamp!),
35+
);
36+
}
37+
38+
private toBinaryV3(): Uint8Array {
39+
const scriptBytes = this.script ? base64.decode(this.script.slice(7)) : new Uint8Array();
40+
41+
return concatBytes(
42+
Uint8Array.from([this.type, this.version]),
43+
convert.stringToByteArray(this.networkId),
44+
convert.longToByteArray(this.timestamp!),
45+
Uint8Array.from([keyTypeId(this.senderKeyType)]),
46+
base58.decode(this.senderPublicKey!),
47+
convert.longToByteArray(this.fee),
48+
convert.shortToByteArray(scriptBytes.length),
49+
scriptBytes,
50+
);
51+
}
52+
53+
toBinary(): Uint8Array {
54+
if (!this.sender) throw Error('Transaction sender not set');
55+
56+
switch (this.version) {
57+
case 1:
58+
return this.toBinaryV1();
59+
case 3:
60+
return this.toBinaryV3();
61+
default:
62+
throw new Error('Incorrect version');
63+
}
64+
}
65+
66+
toJSON(): ITxJSON {
67+
return {
68+
id: this.id,
69+
type: this.type,
70+
version: this.version,
71+
sender: this.sender,
72+
senderKeyType: this.senderKeyType,
73+
senderPublicKey: this.senderPublicKey,
74+
script: this.script,
75+
timestamp: this.timestamp,
76+
fee: this.fee,
77+
sponsor: this.sponsor,
78+
sponsorKeyType: this.sponsorKeyType,
79+
sponsorPublicKey: this.sponsorPublicKey,
80+
proofs: this.proofs,
81+
height: this.height,
82+
};
83+
}
84+
85+
static from(data: ITxJSON): SetScript {
86+
const tx = new SetScript(data.script);
87+
return tx.initFrom(data);
88+
}
89+
}

src/transactions/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Burn from './Burn';
1414
import MappedAnchor from './MappedAnchor';
1515
import Statement from './Statement';
1616
import { ITxJSON } from '../types';
17+
import SetScript from "./SetScript";
1718

1819
export {
1920
Transaction,
@@ -45,6 +46,8 @@ export function txFromData(data: ITxJSON): Transaction {
4546
return MassTransfer.from(data);
4647
case 12:
4748
return Data.from(data);
49+
case 13:
50+
return SetScript.from(data);
4851
case 15:
4952
return Anchor.from(data);
5053
case 16:

0 commit comments

Comments
 (0)