|
| 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 | +} |
0 commit comments