|
| 1 | +/* |
| 2 | + * Copyright 2019 NEM |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * @module transactions/AccountPropertiesAddressTransaction |
| 19 | + */ |
| 20 | +import { RawAddress as address } from '../../core/format'; |
| 21 | +import AccountPropertiesAddressTransactionBufferPackage from '../buffers/AccountPropertiesAddressTransactionBuffer'; |
| 22 | +import AccountPropertiesAddressModificationTransactionSchema from '../schemas/AccountPropertiesAddressModificationTransactionSchema'; |
| 23 | +import { VerifiableTransaction } from './VerifiableTransaction'; |
| 24 | +const { |
| 25 | + AccountPropertiesAddressTransactionBuffer, |
| 26 | + PropertyAddressModificationBuffer, |
| 27 | +} = AccountPropertiesAddressTransactionBufferPackage.Buffers; |
| 28 | + |
| 29 | +const { |
| 30 | + flatbuffers |
| 31 | +} = require('flatbuffers'); |
| 32 | + |
| 33 | +export default class AccountPropertiesAddressTransaction extends VerifiableTransaction { |
| 34 | + constructor(bytes) { |
| 35 | + super(bytes, AccountPropertiesAddressModificationTransactionSchema); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// tslint:disable-next-line:max-classes-per-file |
| 40 | +export class Builder { |
| 41 | + fee: any; |
| 42 | + version: any; |
| 43 | + type: any; |
| 44 | + deadline: any; |
| 45 | + propertyType: any; |
| 46 | + modifications: any; |
| 47 | + constructor() { |
| 48 | + this.fee = [0, 0]; |
| 49 | + this.version = 36865; |
| 50 | + this.type = 0x4150; |
| 51 | + } |
| 52 | + |
| 53 | + addFee(fee) { |
| 54 | + this.fee = fee; |
| 55 | + return this; |
| 56 | + } |
| 57 | + |
| 58 | + addVersion(version) { |
| 59 | + this.version = version; |
| 60 | + return this; |
| 61 | + } |
| 62 | + |
| 63 | + addType(type) { |
| 64 | + this.type = type; |
| 65 | + return this; |
| 66 | + } |
| 67 | + |
| 68 | + addDeadline(deadline) { |
| 69 | + this.deadline = deadline; |
| 70 | + return this; |
| 71 | + } |
| 72 | + |
| 73 | + addPropertyType(propertyType) { |
| 74 | + this.propertyType = propertyType; |
| 75 | + return this; |
| 76 | + } |
| 77 | + |
| 78 | + addModifications(modifications) { |
| 79 | + this.modifications = modifications; |
| 80 | + return this; |
| 81 | + } |
| 82 | + |
| 83 | + build() { |
| 84 | + const builder = new flatbuffers.Builder(1); |
| 85 | + |
| 86 | + // Create modifications |
| 87 | + const modificationsArray: any = []; |
| 88 | + this.modifications.forEach(modification => { |
| 89 | + const addressModificationVector = PropertyAddressModificationBuffer |
| 90 | + .createValueVector(builder, address.stringToAddress(modification.value)); |
| 91 | + PropertyAddressModificationBuffer.startPropertyAddressModificationBuffer(builder); |
| 92 | + PropertyAddressModificationBuffer.addModificationType(builder, modification.type); |
| 93 | + PropertyAddressModificationBuffer.addValue(builder, addressModificationVector); |
| 94 | + modificationsArray.push(PropertyAddressModificationBuffer.endPropertyAddressModificationBuffer(builder)); |
| 95 | + }); |
| 96 | + |
| 97 | + // Create vectors |
| 98 | + const signatureVector = AccountPropertiesAddressTransactionBuffer |
| 99 | + .createSignatureVector(builder, Array(...Array(64)).map(Number.prototype.valueOf, 0)); |
| 100 | + const signerVector = AccountPropertiesAddressTransactionBuffer |
| 101 | + .createSignerVector(builder, Array(...Array(32)).map(Number.prototype.valueOf, 0)); |
| 102 | + const deadlineVector = AccountPropertiesAddressTransactionBuffer |
| 103 | + .createDeadlineVector(builder, this.deadline); |
| 104 | + const feeVector = AccountPropertiesAddressTransactionBuffer |
| 105 | + .createFeeVector(builder, this.fee); |
| 106 | + const modificationVector = AccountPropertiesAddressTransactionBuffer |
| 107 | + .createModificationsVector(builder, modificationsArray); |
| 108 | + |
| 109 | + AccountPropertiesAddressTransactionBuffer.startAccountPropertiesAddressTransactionBuffer(builder); |
| 110 | + AccountPropertiesAddressTransactionBuffer.addSize(builder, 122 + (26 * this.modifications.length)); |
| 111 | + AccountPropertiesAddressTransactionBuffer.addSignature(builder, signatureVector); |
| 112 | + AccountPropertiesAddressTransactionBuffer.addSigner(builder, signerVector); |
| 113 | + AccountPropertiesAddressTransactionBuffer.addVersion(builder, this.version); |
| 114 | + AccountPropertiesAddressTransactionBuffer.addType(builder, this.type); |
| 115 | + AccountPropertiesAddressTransactionBuffer.addFee(builder, feeVector); |
| 116 | + AccountPropertiesAddressTransactionBuffer.addDeadline(builder, deadlineVector); |
| 117 | + AccountPropertiesAddressTransactionBuffer.addPropertyType(builder, this.propertyType); |
| 118 | + AccountPropertiesAddressTransactionBuffer.addModificationCount(builder, this.modifications.length); |
| 119 | + AccountPropertiesAddressTransactionBuffer.addModifications(builder, modificationVector); |
| 120 | + |
| 121 | + // Calculate size |
| 122 | + const codedAccountPropertiesAddress = |
| 123 | + AccountPropertiesAddressTransactionBuffer.endAccountPropertiesAddressTransactionBuffer(builder); |
| 124 | + builder.finish(codedAccountPropertiesAddress); |
| 125 | + |
| 126 | + const bytes = builder.asUint8Array(); |
| 127 | + |
| 128 | + return new AccountPropertiesAddressTransaction(bytes); |
| 129 | + } |
| 130 | +} |
0 commit comments