-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathdoge.ts
More file actions
132 lines (115 loc) · 4.77 KB
/
doge.ts
File metadata and controls
132 lines (115 loc) · 4.77 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
import { BitGoBase, HalfSignedUtxoTransaction, SignedTransaction } from '@bitgo/sdk-core';
import { bitgo } from '@bitgo/utxo-lib';
import {
AbstractUtxoCoin,
SignTransactionOptions,
ExplainTransactionOptions,
ParseTransactionOptions,
VerifyTransactionOptions,
RecoverFromWrongChainOptions,
TransactionInfo,
TransactionPrebuild,
} from '../../abstractUtxoCoin';
import { UtxoCoinName } from '../../names';
import { ParsedTransaction } from '../../transaction/types';
import type { TransactionExplanation } from '../../transaction/fixedScript/explainTransaction';
import type { CrossChainRecoverySigned, CrossChainRecoveryUnsigned } from '../../recovery/crossChainRecovery';
import type { Unspent } from '../../unspent';
type UnspentJSON = Unspent<number> & { valueString: string };
type TransactionInfoJSON = TransactionInfo<number> & { unspents: UnspentJSON[] };
type TransactionPrebuildJSON = TransactionPrebuild<number> & { txInfo: TransactionInfoJSON };
function parseUnspents<TNumber extends number | bigint>(
unspents: UnspentJSON[] | Unspent<TNumber>[]
): Unspent<bigint>[] {
return unspents.map((unspent: Unspent<TNumber> | UnspentJSON): Unspent<bigint> => {
if (typeof unspent.value === 'bigint') {
return unspent as Unspent<bigint>;
}
if ('valueString' in unspent) {
return { ...unspent, value: BigInt(unspent.valueString) };
}
if (typeof unspent.value === 'number') {
throw new Error(`received Unspent<number> where Unspent<bigint> or UnspentJSON was expected`);
}
throw new Error('invalid unspent');
});
}
function parseTransactionInfo<TNumber extends number | bigint>(
txInfo: TransactionInfo<TNumber> | TransactionInfoJSON
): TransactionInfo<bigint> {
if (txInfo.unspents) {
return { ...txInfo, unspents: parseUnspents(txInfo.unspents) };
}
return { ...txInfo, unspents: undefined };
}
function parseTransactionPrebuild<TNumber extends number | bigint>(
txPrebuild: TransactionPrebuild<TNumber> | TransactionPrebuildJSON
): TransactionPrebuild<bigint> {
if (txPrebuild?.txInfo) {
return { ...txPrebuild, txInfo: parseTransactionInfo(txPrebuild.txInfo) };
}
return txPrebuild as TransactionPrebuild<bigint>;
}
export class Doge extends AbstractUtxoCoin {
readonly name: UtxoCoinName = 'doge';
constructor(bitgo: BitGoBase) {
super(bitgo, 'bigint');
}
static createInstance(bitgo: BitGoBase): Doge {
return new Doge(bitgo);
}
supportsBlockTarget(): boolean {
return true;
}
/* amountType is set in constructor. Functions below override the default TNumber of AbstractUtxoCoin to bigint */
/* postProcessPrebuild, isBitGoTaintedUnspent, verifyCustomChangeKeySignatures do not care whether they receive number or bigint */
createTransactionFromHex<TNumber extends number | bigint = bigint>(hex: string): bitgo.UtxoTransaction<TNumber> {
return super.createTransactionFromHex<TNumber>(hex);
}
async parseTransaction<TNumber extends number | bigint = bigint>(
params: ParseTransactionOptions<TNumber>
): /*
the actual return type is Promise<ParsedTransaction<bigint>>,
but the superclass signature currently requires TNumber
*/
Promise<ParsedTransaction<TNumber>> {
return (await super.parseTransaction({
...params,
txPrebuild: parseTransactionPrebuild(params.txPrebuild),
})) as ParsedTransaction<TNumber> /* cast to satisfy superclass signature */;
}
async verifyTransaction<TNumber extends number | bigint = bigint>(
params:
| VerifyTransactionOptions<TNumber>
| (VerifyTransactionOptions<TNumber> & { txPrebuild: TransactionPrebuildJSON })
): Promise<boolean> {
return super.verifyTransaction({
...params,
txPrebuild: parseTransactionPrebuild(params.txPrebuild),
});
}
async signTransaction<TNumber extends number | bigint = bigint>(
params: SignTransactionOptions<TNumber>
): Promise<SignedTransaction | HalfSignedUtxoTransaction> {
return super.signTransaction({
...params,
txPrebuild: {
...params.txPrebuild,
txInfo: params.txPrebuild.txInfo === undefined ? undefined : parseTransactionInfo(params.txPrebuild.txInfo),
},
});
}
async explainTransaction<TNumber extends number | bigint = bigint>(
params: ExplainTransactionOptions<TNumber> | (ExplainTransactionOptions<TNumber> & { txInfo: TransactionInfoJSON })
): Promise<TransactionExplanation> {
return super.explainTransaction({
...params,
txInfo: params.txInfo ? parseTransactionInfo(params.txInfo as TransactionInfoJSON) : undefined,
});
}
async recoverFromWrongChain<TNumber extends number | bigint = bigint>(
params: RecoverFromWrongChainOptions
): Promise<CrossChainRecoverySigned<TNumber> | CrossChainRecoveryUnsigned<TNumber>> {
return super.recoverFromWrongChain(params);
}
}