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