|
| 1 | +import { |
| 2 | + BaseKey, |
| 3 | + BaseTransaction, |
| 4 | + InvalidTransactionError, |
| 5 | + PublicKey, |
| 6 | + Signature, |
| 7 | + TransactionRecipient, |
| 8 | + TransactionType, |
| 9 | +} from '@bitgo/sdk-core'; |
| 10 | +import { TransactionExplanation, TxData } from '../iface'; |
| 11 | +import { BaseCoin as CoinConfig } from '@bitgo/statics'; |
| 12 | +import { |
| 13 | + AccountAddress, |
| 14 | + AccountAuthenticatorEd25519, |
| 15 | + Ed25519PublicKey, |
| 16 | + Ed25519Signature, |
| 17 | + generateUserTransactionHash, |
| 18 | + RawTransaction, |
| 19 | + SignedTransaction, |
| 20 | + SimpleTransaction, |
| 21 | + TransactionAuthenticatorEd25519, |
| 22 | +} from '@aptos-labs/ts-sdk'; |
| 23 | +import { UNAVAILABLE_TEXT } from '../constants'; |
| 24 | +import utils from '../utils'; |
| 25 | + |
| 26 | +export abstract class Transaction extends BaseTransaction { |
| 27 | + protected _rawTransaction: RawTransaction; |
| 28 | + protected _signature: Signature; |
| 29 | + |
| 30 | + static DEFAULT_PUBLIC_KEY = Buffer.alloc(32); |
| 31 | + static DEFAULT_SIGNATURE = Buffer.alloc(64); |
| 32 | + |
| 33 | + constructor(coinConfig: Readonly<CoinConfig>) { |
| 34 | + super(coinConfig); |
| 35 | + } |
| 36 | + |
| 37 | + /** @inheritDoc **/ |
| 38 | + public get id(): string { |
| 39 | + return this._id ?? UNAVAILABLE_TEXT; |
| 40 | + } |
| 41 | + |
| 42 | + set transactionType(transactionType: TransactionType) { |
| 43 | + this._type = transactionType; |
| 44 | + } |
| 45 | + |
| 46 | + public get signablePayload(): Buffer { |
| 47 | + const rawTxnHex = this._rawTransaction.bcsToHex().toString(); |
| 48 | + return Buffer.from(rawTxnHex, 'hex'); |
| 49 | + } |
| 50 | + |
| 51 | + public get sender(): string { |
| 52 | + return this._rawTransaction.sender.toString(); |
| 53 | + } |
| 54 | + |
| 55 | + set sender(senderAddress: string) { |
| 56 | + // Cannot assign to 'sender' because it is a read-only property in RawTransaction. |
| 57 | + const { sequence_number, payload, max_gas_amount, gas_unit_price, expiration_timestamp_secs, chain_id } = |
| 58 | + this._rawTransaction; |
| 59 | + this._rawTransaction = new RawTransaction( |
| 60 | + AccountAddress.fromString(senderAddress), |
| 61 | + sequence_number, |
| 62 | + payload, |
| 63 | + max_gas_amount, |
| 64 | + gas_unit_price, |
| 65 | + expiration_timestamp_secs, |
| 66 | + chain_id |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + public get recipient(): TransactionRecipient { |
| 71 | + return utils.getRecipientFromTransactionPayload(this._rawTransaction.payload); |
| 72 | + } |
| 73 | + |
| 74 | + canSign(_key: BaseKey): boolean { |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
| 78 | + toBroadcastFormat(): string { |
| 79 | + if (!this._rawTransaction) { |
| 80 | + throw new InvalidTransactionError('Empty transaction'); |
| 81 | + } |
| 82 | + return this.serialize(); |
| 83 | + } |
| 84 | + |
| 85 | + serialize(): string { |
| 86 | + let publicKeyBuffer = Transaction.DEFAULT_PUBLIC_KEY; |
| 87 | + let signatureBuffer = Transaction.DEFAULT_SIGNATURE; |
| 88 | + if (this._signature && this._signature.publicKey && this._signature.signature) { |
| 89 | + publicKeyBuffer = Buffer.from(this._signature.publicKey.pub, 'hex'); |
| 90 | + signatureBuffer = this._signature.signature; |
| 91 | + } |
| 92 | + const publicKey = new Ed25519PublicKey(publicKeyBuffer); |
| 93 | + const signature = new Ed25519Signature(signatureBuffer); |
| 94 | + const txnAuthenticator = new TransactionAuthenticatorEd25519(publicKey, signature); |
| 95 | + const signedTxn = new SignedTransaction(this._rawTransaction, txnAuthenticator); |
| 96 | + return signedTxn.toString(); |
| 97 | + } |
| 98 | + |
| 99 | + abstract toJson(): TxData; |
| 100 | + |
| 101 | + addSignature(publicKey: PublicKey, signature: Buffer): void { |
| 102 | + const publicKeyBuffer = Buffer.from(publicKey.pub, 'hex'); |
| 103 | + if (!Transaction.DEFAULT_PUBLIC_KEY.equals(publicKeyBuffer) && !Transaction.DEFAULT_SIGNATURE.equals(signature)) { |
| 104 | + this._signatures.push(signature.toString('hex')); |
| 105 | + this._signature = { publicKey, signature }; |
| 106 | + this.serialize(); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + async build(): Promise<void> { |
| 111 | + this.loadInputsAndOutputs(); |
| 112 | + if (this._signature && this._signature.publicKey && this._signature.signature) { |
| 113 | + const transaction = new SimpleTransaction(this._rawTransaction); |
| 114 | + const publicKey = new Ed25519PublicKey(Buffer.from(this._signature.publicKey.pub, 'hex')); |
| 115 | + const signature = new Ed25519Signature(this._signature.signature); |
| 116 | + const senderAuthenticator = new AccountAuthenticatorEd25519(publicKey, signature); |
| 117 | + this._id = generateUserTransactionHash({ transaction, senderAuthenticator }); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + loadInputsAndOutputs(): void { |
| 122 | + const txRecipient = this.recipient; |
| 123 | + this._inputs = [ |
| 124 | + { |
| 125 | + address: this.sender, |
| 126 | + value: txRecipient.amount as string, |
| 127 | + coin: this._coinConfig.name, |
| 128 | + }, |
| 129 | + ]; |
| 130 | + this._outputs = [ |
| 131 | + { |
| 132 | + address: txRecipient.address, |
| 133 | + value: txRecipient.amount as string, |
| 134 | + coin: this._coinConfig.name, |
| 135 | + }, |
| 136 | + ]; |
| 137 | + } |
| 138 | + |
| 139 | + fromRawTransaction(rawTransaction: string): void { |
| 140 | + try { |
| 141 | + const signedTxn = utils.deserializeSignedTransaction(rawTransaction); |
| 142 | + this._rawTransaction = signedTxn.raw_txn; |
| 143 | + |
| 144 | + this.loadInputsAndOutputs(); |
| 145 | + |
| 146 | + const authenticator = signedTxn.authenticator as TransactionAuthenticatorEd25519; |
| 147 | + const publicKey = Buffer.from(authenticator.public_key.toUint8Array()); |
| 148 | + const signature = Buffer.from(authenticator.signature.toUint8Array()); |
| 149 | + this.addSignature({ pub: publicKey.toString() }, signature); |
| 150 | + } catch (e) { |
| 151 | + console.error('invalid raw transaction', e); |
| 152 | + throw new Error('invalid raw transaction'); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + /** @inheritDoc */ |
| 157 | + explainTransaction(): TransactionExplanation { |
| 158 | + const displayOrder = ['id', 'outputs', 'outputAmount', 'changeOutputs', 'changeAmount', 'fee', 'withdrawAmount']; |
| 159 | + |
| 160 | + const outputs: TransactionRecipient[] = [this.recipient]; |
| 161 | + const outputAmount = outputs[0].amount; |
| 162 | + return { |
| 163 | + displayOrder, |
| 164 | + id: this.id, |
| 165 | + outputs, |
| 166 | + outputAmount, |
| 167 | + changeOutputs: [], |
| 168 | + changeAmount: '0', |
| 169 | + fee: { fee: 'UNKNOWN' }, |
| 170 | + }; |
| 171 | + } |
| 172 | + |
| 173 | + static deserializeRawTransaction(rawTransaction: string): RawTransaction { |
| 174 | + try { |
| 175 | + return utils.deserializeRawTransaction(rawTransaction); |
| 176 | + } catch (e) { |
| 177 | + console.error('invalid raw transaction', e); |
| 178 | + throw new Error('invalid raw transaction'); |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments