-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathwalletInitTransaction.ts
More file actions
151 lines (132 loc) · 4.54 KB
/
walletInitTransaction.ts
File metadata and controls
151 lines (132 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { BaseKey, BaseTransaction, InvalidTransactionError, TransactionType } from '@bitgo/sdk-core';
import { BaseCoin as CoinConfig } from '@bitgo/statics';
import {
MultiHashSignature,
OnboardingTransaction,
PreparedParty,
TransactionExplanation,
WalletInitBroadcastData,
WalletInitTxData,
} from '../iface';
import { SIGNATURE_ALGORITHM_SPEC, SIGNATURE_FORMAT } from '../constant';
export class WalletInitTransaction extends BaseTransaction {
private _preparedParty: PreparedParty;
constructor(coinConfig: Readonly<CoinConfig>) {
super(coinConfig);
}
get preparedParty(): PreparedParty {
return this._preparedParty;
}
set preparedParty(transaction: PreparedParty) {
this._preparedParty = transaction;
this._id = transaction.multiHash;
this._type = TransactionType.WalletInitialization;
}
canSign(key: BaseKey): boolean {
return false;
}
set signatures(signature: string) {
this._signatures.push(signature);
}
toBroadcastFormat(): string {
if (!this._preparedParty) {
throw new InvalidTransactionError('Empty transaction data');
}
const multiHashSignatures: MultiHashSignature[] = [];
if (this.signature.length > 0) {
this.signature.map((signature) => {
const multiHashSignature: MultiHashSignature = {
format: SIGNATURE_FORMAT,
signature: signature,
signedBy: this._preparedParty.publicKeyFingerprint,
signingAlgorithmSpec: SIGNATURE_ALGORITHM_SPEC,
};
multiHashSignatures.push(multiHashSignature);
});
}
const walletInitBroadcastData: WalletInitBroadcastData = {
preparedParty: this._preparedParty,
onboardingTransactions: this._preparedParty.topologyTransactions.map((txn) => {
const onboardingTransaction: OnboardingTransaction = {
transaction: txn,
};
return onboardingTransaction;
}),
multiHashSignatures: multiHashSignatures,
};
return Buffer.from(JSON.stringify(walletInitBroadcastData)).toString('base64');
}
toJson(): WalletInitTxData {
if (!this._preparedParty) {
throw new InvalidTransactionError('Empty transaction data');
}
const result: WalletInitTxData = {
id: this.id,
type: this._type as TransactionType,
preparedParty: this._preparedParty,
};
return result;
}
get signablePayload(): Buffer {
if (!this._preparedParty) {
throw new InvalidTransactionError('Empty transaction data');
}
const multiHash = Buffer.from(this._preparedParty.multiHash, 'base64');
const topologyTxs = this._preparedParty.topologyTransactions;
// If no topology transactions, fall back to multiHash only
if (!topologyTxs || topologyTxs.length === 0) {
return multiHash;
}
const shouldIncludeTxnType = this._preparedParty.shouldIncludeTxnType ?? false;
const itemCount = topologyTxs.length + 1;
const parts: Buffer[] = [];
// Optional txnType for version >0.5.x
if (shouldIncludeTxnType) {
const txnTypeBuf = Buffer.alloc(4);
txnTypeBuf.writeUInt32LE(0, 0);
parts.push(txnTypeBuf);
}
// Item count
const itemCountBuf = Buffer.alloc(4);
itemCountBuf.writeUInt32LE(itemCount, 0);
parts.push(itemCountBuf);
// Topology transactions with length prefixes
for (const tx of topologyTxs) {
const txBuf = Buffer.from(tx, 'base64');
const lenBuf = Buffer.alloc(4);
lenBuf.writeUInt32LE(txBuf.length, 0);
parts.push(lenBuf, txBuf);
}
// Append multiHash
parts.push(multiHash);
return Buffer.concat(parts);
}
fromRawTransaction(rawTx: string): void {
try {
const decoded: WalletInitBroadcastData = JSON.parse(Buffer.from(rawTx, 'base64').toString('utf8'));
this._preparedParty = decoded.preparedParty;
this._type = TransactionType.WalletInitialization;
this._id = decoded.preparedParty.multiHash;
if (decoded.multiHashSignatures.length > 0) {
decoded.multiHashSignatures.map((multiHashSignature: MultiHashSignature) => {
this.signatures = multiHashSignature.signature;
});
}
} catch (e) {
throw new InvalidTransactionError('Unable to parse raw transaction data');
}
}
explainTransaction(): TransactionExplanation {
const displayOrder = ['id', 'outputs', 'outputAmount', 'changeOutputs', 'changeAmount', 'fee', 'type'];
return {
id: this.id,
displayOrder,
outputs: [],
outputAmount: '0',
changeOutputs: [],
changeAmount: '0',
fee: { fee: '0' },
type: this.type,
};
}
}