@onekeyfe/cardano-coin-selection-asmjs 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.MD +25 -0
- package/README.md +183 -0
- package/lib/cjs/constants/index.d.ts +40 -0
- package/lib/cjs/constants/index.js +69 -0
- package/lib/cjs/index.d.ts +6 -0
- package/lib/cjs/index.js +101 -0
- package/lib/cjs/methods/largestFirst.d.ts +2 -0
- package/lib/cjs/methods/largestFirst.js +201 -0
- package/lib/cjs/methods/randomImprove.d.ts +2 -0
- package/lib/cjs/methods/randomImprove.js +187 -0
- package/lib/cjs/types/trezor.d.ts +45 -0
- package/lib/cjs/types/trezor.js +8 -0
- package/lib/cjs/types/types.d.ts +116 -0
- package/lib/cjs/types/types.js +17 -0
- package/lib/cjs/utils/common.d.ts +46 -0
- package/lib/cjs/utils/common.js +514 -0
- package/lib/cjs/utils/errors.d.ts +8 -0
- package/lib/cjs/utils/errors.js +13 -0
- package/lib/cjs/utils/logger.d.ts +5 -0
- package/lib/cjs/utils/logger.js +27 -0
- package/lib/cjs/utils/onekey/dapp.d.ts +14 -0
- package/lib/cjs/utils/onekey/dapp.js +247 -0
- package/lib/cjs/utils/onekey/error.d.ts +28 -0
- package/lib/cjs/utils/onekey/error.js +25 -0
- package/lib/cjs/utils/onekey/index.d.ts +48 -0
- package/lib/cjs/utils/onekey/index.js +15 -0
- package/lib/cjs/utils/onekey/signTx.d.ts +26 -0
- package/lib/cjs/utils/onekey/signTx.js +296 -0
- package/lib/cjs/utils/onekey/transaction.d.ts +5 -0
- package/lib/cjs/utils/onekey/transaction.js +49 -0
- package/lib/cjs/utils/onekey/txToOneKey.d.ts +67 -0
- package/lib/cjs/utils/onekey/txToOneKey.js +504 -0
- package/lib/cjs/utils/onekey/types.d.ts +79 -0
- package/lib/cjs/utils/onekey/types.js +16 -0
- package/lib/cjs/utils/trezor/index.d.ts +3 -0
- package/lib/cjs/utils/trezor/index.js +9 -0
- package/lib/cjs/utils/trezor/sign.d.ts +5 -0
- package/lib/cjs/utils/trezor/sign.js +69 -0
- package/lib/cjs/utils/trezor/transformations.d.ts +17 -0
- package/lib/cjs/utils/trezor/transformations.js +66 -0
- package/lib/esm/constants/index.d.ts +40 -0
- package/lib/esm/constants/index.js +43 -0
- package/lib/esm/index.d.ts +6 -0
- package/lib/esm/index.js +73 -0
- package/lib/esm/methods/largestFirst.d.ts +2 -0
- package/lib/esm/methods/largestFirst.js +174 -0
- package/lib/esm/methods/randomImprove.d.ts +2 -0
- package/lib/esm/methods/randomImprove.js +160 -0
- package/lib/esm/types/trezor.d.ts +45 -0
- package/lib/esm/types/trezor.js +5 -0
- package/lib/esm/types/types.d.ts +116 -0
- package/lib/esm/types/types.js +14 -0
- package/lib/esm/utils/common.d.ts +46 -0
- package/lib/esm/utils/common.js +460 -0
- package/lib/esm/utils/errors.d.ts +8 -0
- package/lib/esm/utils/errors.js +9 -0
- package/lib/esm/utils/logger.d.ts +5 -0
- package/lib/esm/utils/logger.js +23 -0
- package/lib/esm/utils/onekey/dapp.d.ts +14 -0
- package/lib/esm/utils/onekey/dapp.js +216 -0
- package/lib/esm/utils/onekey/error.d.ts +28 -0
- package/lib/esm/utils/onekey/error.js +22 -0
- package/lib/esm/utils/onekey/index.d.ts +48 -0
- package/lib/esm/utils/onekey/index.js +11 -0
- package/lib/esm/utils/onekey/signTx.d.ts +26 -0
- package/lib/esm/utils/onekey/signTx.js +266 -0
- package/lib/esm/utils/onekey/transaction.d.ts +5 -0
- package/lib/esm/utils/onekey/transaction.js +42 -0
- package/lib/esm/utils/onekey/txToOneKey.d.ts +67 -0
- package/lib/esm/utils/onekey/txToOneKey.js +477 -0
- package/lib/esm/utils/onekey/types.d.ts +79 -0
- package/lib/esm/utils/onekey/types.js +15 -0
- package/lib/esm/utils/trezor/index.d.ts +3 -0
- package/lib/esm/utils/trezor/index.js +3 -0
- package/lib/esm/utils/trezor/sign.d.ts +5 -0
- package/lib/esm/utils/trezor/sign.js +42 -0
- package/lib/esm/utils/trezor/transformations.d.ts +17 -0
- package/lib/esm/utils/trezor/transformations.js +60 -0
- package/package.json +61 -0
|
@@ -0,0 +1,504 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.txToOneKey = exports.CardanoPoolRelayType = exports.CardanoCertificateType = exports.CardanoTxSigningMode = exports.CardanoAddressType = void 0;
|
|
27
|
+
// @ts-nocheck
|
|
28
|
+
const CardanoWasm = __importStar(require("@emurgo/cardano-serialization-lib-asmjs"));
|
|
29
|
+
var CardanoAddressType;
|
|
30
|
+
(function (CardanoAddressType) {
|
|
31
|
+
CardanoAddressType[CardanoAddressType["BASE"] = 0] = "BASE";
|
|
32
|
+
CardanoAddressType[CardanoAddressType["BASE_SCRIPT_KEY"] = 1] = "BASE_SCRIPT_KEY";
|
|
33
|
+
CardanoAddressType[CardanoAddressType["BASE_KEY_SCRIPT"] = 2] = "BASE_KEY_SCRIPT";
|
|
34
|
+
CardanoAddressType[CardanoAddressType["BASE_SCRIPT_SCRIPT"] = 3] = "BASE_SCRIPT_SCRIPT";
|
|
35
|
+
CardanoAddressType[CardanoAddressType["POINTER"] = 4] = "POINTER";
|
|
36
|
+
CardanoAddressType[CardanoAddressType["POINTER_SCRIPT"] = 5] = "POINTER_SCRIPT";
|
|
37
|
+
CardanoAddressType[CardanoAddressType["ENTERPRISE"] = 6] = "ENTERPRISE";
|
|
38
|
+
CardanoAddressType[CardanoAddressType["ENTERPRISE_SCRIPT"] = 7] = "ENTERPRISE_SCRIPT";
|
|
39
|
+
CardanoAddressType[CardanoAddressType["BYRON"] = 8] = "BYRON";
|
|
40
|
+
CardanoAddressType[CardanoAddressType["REWARD"] = 14] = "REWARD";
|
|
41
|
+
CardanoAddressType[CardanoAddressType["REWARD_SCRIPT"] = 15] = "REWARD_SCRIPT";
|
|
42
|
+
})(CardanoAddressType = exports.CardanoAddressType || (exports.CardanoAddressType = {}));
|
|
43
|
+
var CardanoTxSigningMode;
|
|
44
|
+
(function (CardanoTxSigningMode) {
|
|
45
|
+
CardanoTxSigningMode[CardanoTxSigningMode["ORDINARY_TRANSACTION"] = 0] = "ORDINARY_TRANSACTION";
|
|
46
|
+
CardanoTxSigningMode[CardanoTxSigningMode["POOL_REGISTRATION_AS_OWNER"] = 1] = "POOL_REGISTRATION_AS_OWNER";
|
|
47
|
+
CardanoTxSigningMode[CardanoTxSigningMode["MULTISIG_TRANSACTION"] = 2] = "MULTISIG_TRANSACTION";
|
|
48
|
+
CardanoTxSigningMode[CardanoTxSigningMode["PLUTUS_TRANSACTION"] = 3] = "PLUTUS_TRANSACTION";
|
|
49
|
+
})(CardanoTxSigningMode = exports.CardanoTxSigningMode || (exports.CardanoTxSigningMode = {}));
|
|
50
|
+
var CardanoCertificateType;
|
|
51
|
+
(function (CardanoCertificateType) {
|
|
52
|
+
CardanoCertificateType[CardanoCertificateType["STAKE_REGISTRATION"] = 0] = "STAKE_REGISTRATION";
|
|
53
|
+
CardanoCertificateType[CardanoCertificateType["STAKE_DEREGISTRATION"] = 1] = "STAKE_DEREGISTRATION";
|
|
54
|
+
CardanoCertificateType[CardanoCertificateType["STAKE_DELEGATION"] = 2] = "STAKE_DELEGATION";
|
|
55
|
+
CardanoCertificateType[CardanoCertificateType["STAKE_POOL_REGISTRATION"] = 3] = "STAKE_POOL_REGISTRATION";
|
|
56
|
+
})(CardanoCertificateType = exports.CardanoCertificateType || (exports.CardanoCertificateType = {}));
|
|
57
|
+
var CardanoPoolRelayType;
|
|
58
|
+
(function (CardanoPoolRelayType) {
|
|
59
|
+
CardanoPoolRelayType[CardanoPoolRelayType["SINGLE_HOST_IP"] = 0] = "SINGLE_HOST_IP";
|
|
60
|
+
CardanoPoolRelayType[CardanoPoolRelayType["SINGLE_HOST_NAME"] = 1] = "SINGLE_HOST_NAME";
|
|
61
|
+
CardanoPoolRelayType[CardanoPoolRelayType["MULTIPLE_HOST_NAME"] = 2] = "MULTIPLE_HOST_NAME";
|
|
62
|
+
})(CardanoPoolRelayType = exports.CardanoPoolRelayType || (exports.CardanoPoolRelayType = {}));
|
|
63
|
+
const generateKeys = (keys, xpub) => {
|
|
64
|
+
const publicKey = CardanoWasm.Bip32PublicKey.from_bytes(Buffer.from(xpub, 'hex'));
|
|
65
|
+
const paymentKeyHashRaw = publicKey.derive(0).derive(0).to_raw_key().hash();
|
|
66
|
+
const stakeKeyHashRaw = publicKey.derive(2).derive(0).to_raw_key().hash();
|
|
67
|
+
const paymentKeyHash = Buffer.from(paymentKeyHashRaw.to_bytes()).toString('hex');
|
|
68
|
+
const stakeKeyHash = Buffer.from(stakeKeyHashRaw.to_bytes()).toString('hex');
|
|
69
|
+
return {
|
|
70
|
+
payment: Object.assign(Object.assign({}, keys.payment), { hash: paymentKeyHash }),
|
|
71
|
+
stake: Object.assign(Object.assign({}, keys.stake), { hash: stakeKeyHash }),
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
|
+
const outputsToOneKey = (outputs, changeAddress) => {
|
|
75
|
+
var _a, _b;
|
|
76
|
+
const onekeyOutputs = [];
|
|
77
|
+
for (let i = 0; i < outputs.len(); i++) {
|
|
78
|
+
const output = outputs.get(i);
|
|
79
|
+
const multiAsset = output.amount().multiasset();
|
|
80
|
+
let tokenBundle = null;
|
|
81
|
+
if (multiAsset) {
|
|
82
|
+
tokenBundle = [];
|
|
83
|
+
for (let j = 0; j < multiAsset.keys().len(); j++) {
|
|
84
|
+
const policy = multiAsset.keys().get(j);
|
|
85
|
+
const assets = multiAsset.get(policy);
|
|
86
|
+
const tokens = [];
|
|
87
|
+
for (let k = 0; k < assets.keys().len(); k++) {
|
|
88
|
+
const assetName = assets.keys().get(k);
|
|
89
|
+
const amount = assets.get(assetName).to_str();
|
|
90
|
+
tokens.push({
|
|
91
|
+
assetNameBytes: Buffer.from(assetName.name()).toString('hex'),
|
|
92
|
+
amount,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
// sort canonical
|
|
96
|
+
tokens.sort((a, b) => {
|
|
97
|
+
if (a.assetNameBytes.length == b.assetNameBytes.length) {
|
|
98
|
+
return a.assetNameBytes > b.assetNameBytes ? 1 : -1;
|
|
99
|
+
}
|
|
100
|
+
else if (a.assetNameBytes.length > b.assetNameBytes.length)
|
|
101
|
+
return 1;
|
|
102
|
+
else
|
|
103
|
+
return -1;
|
|
104
|
+
});
|
|
105
|
+
tokenBundle.push({
|
|
106
|
+
policyId: Buffer.from(policy.to_bytes()).toString('hex'),
|
|
107
|
+
tokenAmounts: tokens,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
const outputAddressBech32 = output.address().to_bech32();
|
|
112
|
+
const outputAddressHuman = (() => {
|
|
113
|
+
try {
|
|
114
|
+
return CardanoWasm.BaseAddress.from_address(output.address())
|
|
115
|
+
.to_address()
|
|
116
|
+
.to_bech32();
|
|
117
|
+
}
|
|
118
|
+
catch (e) {
|
|
119
|
+
// ignore
|
|
120
|
+
}
|
|
121
|
+
try {
|
|
122
|
+
return CardanoWasm.EnterpriseAddress.from_address(output.address())
|
|
123
|
+
.to_address()
|
|
124
|
+
.to_bech32();
|
|
125
|
+
}
|
|
126
|
+
catch (e) {
|
|
127
|
+
// ignore
|
|
128
|
+
}
|
|
129
|
+
try {
|
|
130
|
+
return CardanoWasm.PointerAddress.from_address(output.address())
|
|
131
|
+
.to_address()
|
|
132
|
+
.to_bech32();
|
|
133
|
+
}
|
|
134
|
+
catch (e) {
|
|
135
|
+
// ignore
|
|
136
|
+
}
|
|
137
|
+
return CardanoWasm.ByronAddress.from_address(output.address()).to_base58();
|
|
138
|
+
})();
|
|
139
|
+
const destination = outputAddressBech32 === changeAddress.address
|
|
140
|
+
? {
|
|
141
|
+
addressParameters: {
|
|
142
|
+
addressType: CardanoAddressType.BASE,
|
|
143
|
+
path: changeAddress.addressParameters.path,
|
|
144
|
+
stakingPath: changeAddress.addressParameters.stakingPath,
|
|
145
|
+
},
|
|
146
|
+
}
|
|
147
|
+
: {
|
|
148
|
+
address: outputAddressHuman,
|
|
149
|
+
};
|
|
150
|
+
const datumHash = (!output.has_plutus_data() ||
|
|
151
|
+
(output.has_plutus_data() && ((_a = output.plutus_data()) === null || _a === void 0 ? void 0 : _a.kind()) === 0)) &&
|
|
152
|
+
output.has_data_hash()
|
|
153
|
+
? output.data_hash().to_hex()
|
|
154
|
+
: null;
|
|
155
|
+
const inlineDatum = output.has_data_hash() &&
|
|
156
|
+
output.has_plutus_data() &&
|
|
157
|
+
((_b = output.plutus_data()) === null || _b === void 0 ? void 0 : _b.kind()) === 1
|
|
158
|
+
? output.data_hash().to_hex()
|
|
159
|
+
: null;
|
|
160
|
+
// const datumHash =
|
|
161
|
+
// output.datum() && output.datum().kind() === 0
|
|
162
|
+
// ? Buffer.from(output.datum().as_data_hash().to_bytes()).toString('hex')
|
|
163
|
+
// : null;
|
|
164
|
+
// const inlineDatum =
|
|
165
|
+
// output.datum() && output.datum().kind() === 1
|
|
166
|
+
// ? Buffer.from(output.datum().as_data().get().to_bytes()).toString('hex')
|
|
167
|
+
// : null;
|
|
168
|
+
const referenceScript = output.script_ref()
|
|
169
|
+
? Buffer.from(output.script_ref().get().to_bytes()).toString('hex')
|
|
170
|
+
: null;
|
|
171
|
+
const outputRes = Object.assign({ amount: output.amount().coin().to_str(), tokenBundle,
|
|
172
|
+
datumHash, format: inlineDatum || referenceScript ? 1 : 0, inlineDatum,
|
|
173
|
+
referenceScript }, destination);
|
|
174
|
+
if (!tokenBundle)
|
|
175
|
+
delete outputRes.tokenBundle;
|
|
176
|
+
if (!datumHash)
|
|
177
|
+
delete outputRes.datumHash;
|
|
178
|
+
if (!inlineDatum)
|
|
179
|
+
delete outputRes.inlineDatum;
|
|
180
|
+
if (!referenceScript)
|
|
181
|
+
delete outputRes.referenceScript;
|
|
182
|
+
onekeyOutputs.push(outputRes);
|
|
183
|
+
}
|
|
184
|
+
return onekeyOutputs;
|
|
185
|
+
};
|
|
186
|
+
/**
|
|
187
|
+
*
|
|
188
|
+
* @param {Transaction} tx
|
|
189
|
+
*/
|
|
190
|
+
const txToOneKey = async (rawTx, network, initKeys, xpub, changeAddress) => {
|
|
191
|
+
var _a;
|
|
192
|
+
const keys = generateKeys(initKeys, xpub);
|
|
193
|
+
const tx = CardanoWasm.Transaction.from_bytes(Buffer.from(rawTx, 'hex'));
|
|
194
|
+
let signingMode = CardanoTxSigningMode.ORDINARY_TRANSACTION;
|
|
195
|
+
const outputs = tx.body().outputs();
|
|
196
|
+
const onekeyOutputs = outputsToOneKey(outputs, changeAddress);
|
|
197
|
+
let onekeyCertificates = null;
|
|
198
|
+
const certificates = tx.body().certs();
|
|
199
|
+
if (certificates) {
|
|
200
|
+
onekeyCertificates = [];
|
|
201
|
+
for (let i = 0; i < certificates.len(); i++) {
|
|
202
|
+
const cert = certificates.get(i);
|
|
203
|
+
const certificate = {};
|
|
204
|
+
if (cert.kind() === 0) {
|
|
205
|
+
const credential = (_a = cert.as_stake_registration()) === null || _a === void 0 ? void 0 : _a.stake_credential();
|
|
206
|
+
certificate.type = CardanoCertificateType.STAKE_REGISTRATION;
|
|
207
|
+
if ((credential === null || credential === void 0 ? void 0 : credential.kind()) === 0) {
|
|
208
|
+
certificate.path = keys.stake.path;
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
const scriptHash = Buffer.from(credential.to_scripthash().to_bytes()).toString('hex');
|
|
212
|
+
certificate.scriptHash = scriptHash;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
else if (cert.kind() === 1) {
|
|
216
|
+
const credential = cert.as_stake_deregistration().stake_credential();
|
|
217
|
+
certificate.type = CardanoCertificateType.STAKE_DEREGISTRATION;
|
|
218
|
+
if (credential.kind() === 0) {
|
|
219
|
+
certificate.path = keys.stake.path;
|
|
220
|
+
}
|
|
221
|
+
else {
|
|
222
|
+
const scriptHash = Buffer.from(credential.to_scripthash().to_bytes()).toString('hex');
|
|
223
|
+
certificate.scriptHash = scriptHash;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
else if (cert.kind() === 2) {
|
|
227
|
+
const delegation = cert.as_stake_delegation();
|
|
228
|
+
const credential = delegation.stake_credential();
|
|
229
|
+
const poolKeyHashHex = Buffer.from(delegation.pool_keyhash().to_bytes()).toString('hex');
|
|
230
|
+
certificate.type = CardanoCertificateType.STAKE_DELEGATION;
|
|
231
|
+
if (credential.kind() === 0) {
|
|
232
|
+
certificate.path = keys.stake.path;
|
|
233
|
+
}
|
|
234
|
+
else {
|
|
235
|
+
const scriptHash = Buffer.from(credential.to_scripthash().to_bytes()).toString('hex');
|
|
236
|
+
certificate.scriptHash = scriptHash;
|
|
237
|
+
}
|
|
238
|
+
certificate.pool = poolKeyHashHex;
|
|
239
|
+
}
|
|
240
|
+
else if (cert.kind() === 3) {
|
|
241
|
+
const params = cert.as_pool_registration().pool_params();
|
|
242
|
+
certificate.type = CardanoCertificateType.STAKE_POOL_REGISTRATION;
|
|
243
|
+
const owners = params.pool_owners();
|
|
244
|
+
const poolOwners = [];
|
|
245
|
+
for (let i = 0; i < owners.len(); i++) {
|
|
246
|
+
const keyHash = Buffer.from(owners.get(i).to_bytes()).toString('hex');
|
|
247
|
+
if (keyHash == keys.stake.hash) {
|
|
248
|
+
signingMode = CardanoTxSigningMode.POOL_REGISTRATION_AS_OWNER;
|
|
249
|
+
poolOwners.push({
|
|
250
|
+
stakingKeyPath: keys.stake.path,
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
else {
|
|
254
|
+
poolOwners.push({
|
|
255
|
+
stakingKeyHash: keyHash,
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
const relays = params.relays();
|
|
260
|
+
const onekeyRelays = [];
|
|
261
|
+
for (let i = 0; i < relays.len(); i++) {
|
|
262
|
+
const relay = relays.get(i);
|
|
263
|
+
if (relay.kind() === 0) {
|
|
264
|
+
const singleHostAddr = relay.as_single_host_addr();
|
|
265
|
+
const type = CardanoPoolRelayType.SINGLE_HOST_IP;
|
|
266
|
+
const port = singleHostAddr.port();
|
|
267
|
+
const ipv4Address = singleHostAddr.ipv4()
|
|
268
|
+
? bytesToIp(singleHostAddr.ipv4().ip())
|
|
269
|
+
: null;
|
|
270
|
+
const ipv6Address = singleHostAddr.ipv6()
|
|
271
|
+
? bytesToIp(singleHostAddr.ipv6().ip())
|
|
272
|
+
: null;
|
|
273
|
+
onekeyRelays.push({ type, port, ipv4Address, ipv6Address });
|
|
274
|
+
}
|
|
275
|
+
else if (relay.kind() === 1) {
|
|
276
|
+
const type = CardanoPoolRelayType.SINGLE_HOST_NAME;
|
|
277
|
+
const singleHostName = relay.as_single_host_name();
|
|
278
|
+
const port = singleHostName.port();
|
|
279
|
+
const hostName = singleHostName.dns_name().record();
|
|
280
|
+
onekeyRelays.push({
|
|
281
|
+
type,
|
|
282
|
+
port,
|
|
283
|
+
hostName,
|
|
284
|
+
});
|
|
285
|
+
}
|
|
286
|
+
else if (relay.kind() === 2) {
|
|
287
|
+
const type = CardanoPoolRelayType.MULTIPLE_HOST_NAME;
|
|
288
|
+
const multiHostName = relay.as_multi_host_name();
|
|
289
|
+
const hostName = multiHostName.dns_name();
|
|
290
|
+
onekeyRelays.push({
|
|
291
|
+
type,
|
|
292
|
+
hostName,
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
const cost = params.cost().to_str();
|
|
297
|
+
const margin = params.margin();
|
|
298
|
+
const pledge = params.pledge().to_str();
|
|
299
|
+
const poolId = Buffer.from(params.operator().to_bytes()).toString('hex');
|
|
300
|
+
const metadata = params.pool_metadata()
|
|
301
|
+
? {
|
|
302
|
+
url: params.pool_metadata().url().url(),
|
|
303
|
+
hash: Buffer.from(params.pool_metadata().pool_metadata_hash().to_bytes()).toString('hex'),
|
|
304
|
+
}
|
|
305
|
+
: null;
|
|
306
|
+
const rewardAccount = params.reward_account().to_address().to_bech32();
|
|
307
|
+
const vrfKeyHash = Buffer.from(params.vrf_keyhash().to_bytes()).toString('hex');
|
|
308
|
+
certificate.poolParameters = {
|
|
309
|
+
poolId,
|
|
310
|
+
vrfKeyHash,
|
|
311
|
+
pledge,
|
|
312
|
+
cost,
|
|
313
|
+
margin: {
|
|
314
|
+
numerator: margin.numerator().to_str(),
|
|
315
|
+
denominator: margin.denominator().to_str(),
|
|
316
|
+
},
|
|
317
|
+
rewardAccount,
|
|
318
|
+
owners: poolOwners,
|
|
319
|
+
relays: onekeyRelays,
|
|
320
|
+
metadata,
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
onekeyCertificates.push(certificate);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
const fee = tx.body().fee().to_str();
|
|
327
|
+
const ttl = tx.body().ttl();
|
|
328
|
+
const withdrawals = tx.body().withdrawals();
|
|
329
|
+
let onekeyWithdrawals = null;
|
|
330
|
+
if (withdrawals) {
|
|
331
|
+
onekeyWithdrawals = [];
|
|
332
|
+
for (let i = 0; i < withdrawals.keys().len(); i++) {
|
|
333
|
+
const withdrawal = {};
|
|
334
|
+
const rewardAddress = withdrawals.keys().get(i);
|
|
335
|
+
if (rewardAddress.payment_cred().kind() === 0) {
|
|
336
|
+
withdrawal.path = keys.stake.path;
|
|
337
|
+
}
|
|
338
|
+
else {
|
|
339
|
+
withdrawal.scriptHash = Buffer.from(rewardAddress.payment_cred().to_scripthash().to_bytes()).toString('hex');
|
|
340
|
+
}
|
|
341
|
+
withdrawal.amount = withdrawals.get(rewardAddress).to_str();
|
|
342
|
+
onekeyWithdrawals.push(withdrawal);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
const auxiliaryData = tx.body().auxiliary_data_hash()
|
|
346
|
+
? {
|
|
347
|
+
hash: Buffer.from(tx.body().auxiliary_data_hash().to_bytes()).toString('hex'),
|
|
348
|
+
}
|
|
349
|
+
: null;
|
|
350
|
+
const validityIntervalStart = tx.body().validity_start_interval()
|
|
351
|
+
? tx.body().validity_start_interval().to_str()
|
|
352
|
+
: null;
|
|
353
|
+
const mint = tx.body().mint();
|
|
354
|
+
let additionalWitnessRequests = null;
|
|
355
|
+
let mintBundle = null;
|
|
356
|
+
if (mint) {
|
|
357
|
+
mintBundle = [];
|
|
358
|
+
for (let j = 0; j < mint.keys().len(); j++) {
|
|
359
|
+
const policy = mint.keys().get(j);
|
|
360
|
+
const assets = mint.get(policy);
|
|
361
|
+
const tokens = [];
|
|
362
|
+
for (let k = 0; k < assets.keys().len(); k++) {
|
|
363
|
+
const assetName = assets.keys().get(k);
|
|
364
|
+
const amount = assets.get(assetName);
|
|
365
|
+
tokens.push({
|
|
366
|
+
assetNameBytes: Buffer.from(assetName.name()).toString('hex'),
|
|
367
|
+
mintAmount: amount.is_positive()
|
|
368
|
+
? amount.as_positive().to_str()
|
|
369
|
+
: '-' + amount.as_negative().to_str(),
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
// sort canonical
|
|
373
|
+
tokens.sort((a, b) => {
|
|
374
|
+
if (a.assetNameBytes.length == b.assetNameBytes.length) {
|
|
375
|
+
return a.assetNameBytes > b.assetNameBytes ? 1 : -1;
|
|
376
|
+
}
|
|
377
|
+
else if (a.assetNameBytes.length > b.assetNameBytes.length)
|
|
378
|
+
return 1;
|
|
379
|
+
else
|
|
380
|
+
return -1;
|
|
381
|
+
});
|
|
382
|
+
mintBundle.push({
|
|
383
|
+
policyId: Buffer.from(policy.to_bytes()).toString('hex'),
|
|
384
|
+
tokenAmounts: tokens,
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
additionalWitnessRequests = [];
|
|
388
|
+
if (keys.payment.path)
|
|
389
|
+
additionalWitnessRequests.push(keys.payment.path);
|
|
390
|
+
if (keys.stake.path)
|
|
391
|
+
additionalWitnessRequests.push(keys.stake.path);
|
|
392
|
+
}
|
|
393
|
+
// Plutus
|
|
394
|
+
const scriptDataHash = tx.body().script_data_hash()
|
|
395
|
+
? Buffer.from(tx.body().script_data_hash().to_bytes()).toString('hex')
|
|
396
|
+
: null;
|
|
397
|
+
let collateralInputs = null;
|
|
398
|
+
if (tx.body().collateral()) {
|
|
399
|
+
collateralInputs = [];
|
|
400
|
+
const coll = tx.body().collateral();
|
|
401
|
+
for (let i = 0; i < coll.len(); i++) {
|
|
402
|
+
const input = coll.get(i);
|
|
403
|
+
if (keys.payment.path) {
|
|
404
|
+
collateralInputs.push({
|
|
405
|
+
prev_hash: Buffer.from(input.transaction_id().to_bytes()).toString('hex'),
|
|
406
|
+
prev_index: parseInt(input.index()),
|
|
407
|
+
path: keys.payment.path, // needed to include payment key witness if available
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
else {
|
|
411
|
+
collateralInputs.push({
|
|
412
|
+
prev_hash: Buffer.from(input.transaction_id().to_bytes()).toString('hex'),
|
|
413
|
+
prev_index: parseInt(input.index()),
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
signingMode = CardanoTxSigningMode.PLUTUS_TRANSACTION;
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
let requiredSigners = null;
|
|
420
|
+
if (tx.body().required_signers()) {
|
|
421
|
+
requiredSigners = [];
|
|
422
|
+
const r = tx.body().required_signers();
|
|
423
|
+
for (let i = 0; i < r.len(); i++) {
|
|
424
|
+
const signer = Buffer.from(r.get(i).to_bytes()).toString('hex');
|
|
425
|
+
if (signer === keys.payment.hash) {
|
|
426
|
+
requiredSigners.push({
|
|
427
|
+
keyPath: keys.payment.path,
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
else {
|
|
431
|
+
requiredSigners.push({
|
|
432
|
+
keyHash: signer,
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
signingMode = CardanoTxSigningMode.PLUTUS_TRANSACTION;
|
|
437
|
+
}
|
|
438
|
+
let referenceInputs = null;
|
|
439
|
+
if (tx.body().reference_inputs()) {
|
|
440
|
+
referenceInputs = [];
|
|
441
|
+
const ri = tx.body().reference_inputs();
|
|
442
|
+
for (let i = 0; i < ri.len(); i++) {
|
|
443
|
+
referenceInputs.push({
|
|
444
|
+
prev_hash: ri.get(i).transaction_id().to_hex(),
|
|
445
|
+
prev_index: parseInt(ri.get(i).index().to_str()),
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
signingMode = CardanoTxSigningMode.PLUTUS_TRANSACTION;
|
|
449
|
+
}
|
|
450
|
+
const totalCollateral = tx.body().total_collateral()
|
|
451
|
+
? tx.body().total_collateral().to_str()
|
|
452
|
+
: null;
|
|
453
|
+
const collateralReturn = (() => {
|
|
454
|
+
if (tx.body().collateral_return()) {
|
|
455
|
+
const outputs = CardanoWasm.TransactionOutputs.new();
|
|
456
|
+
outputs.add(tx.body().collateral_return());
|
|
457
|
+
const [out] = outputsToOneKey(outputs, changeAddress);
|
|
458
|
+
return out;
|
|
459
|
+
}
|
|
460
|
+
return null;
|
|
461
|
+
})();
|
|
462
|
+
const includeNetworkId = !!tx.body().network_id();
|
|
463
|
+
const onekeyTx = {
|
|
464
|
+
signingMode,
|
|
465
|
+
outputs: onekeyOutputs,
|
|
466
|
+
fee,
|
|
467
|
+
ttl: ttl ? `${ttl}` : null,
|
|
468
|
+
validityIntervalStart,
|
|
469
|
+
certificates: onekeyCertificates,
|
|
470
|
+
withdrawals: onekeyWithdrawals,
|
|
471
|
+
auxiliaryData,
|
|
472
|
+
mint: mintBundle,
|
|
473
|
+
scriptDataHash,
|
|
474
|
+
collateralInputs,
|
|
475
|
+
requiredSigners,
|
|
476
|
+
protocolMagic: network === 1 ? 764824073 : 42,
|
|
477
|
+
networkId: network,
|
|
478
|
+
includeNetworkId,
|
|
479
|
+
additionalWitnessRequests,
|
|
480
|
+
collateralReturn,
|
|
481
|
+
totalCollateral,
|
|
482
|
+
referenceInputs,
|
|
483
|
+
};
|
|
484
|
+
Object.keys(onekeyTx).forEach(key => !onekeyTx[key] && onekeyTx[key] != 0 && delete onekeyTx[key]);
|
|
485
|
+
console.log('format onekey cardano hardware Tx =====>>> : ', onekeyTx);
|
|
486
|
+
return Promise.resolve(onekeyTx);
|
|
487
|
+
};
|
|
488
|
+
exports.txToOneKey = txToOneKey;
|
|
489
|
+
const bytesToIp = bytes => {
|
|
490
|
+
if (!bytes)
|
|
491
|
+
return null;
|
|
492
|
+
if (bytes.length === 4) {
|
|
493
|
+
return { ipv4: bytes.join('.') };
|
|
494
|
+
}
|
|
495
|
+
else if (bytes.length === 16) {
|
|
496
|
+
let ipv6 = '';
|
|
497
|
+
for (let i = 0; i < bytes.length; i += 2) {
|
|
498
|
+
ipv6 += bytes[i].toString(16) + bytes[i + 1].toString(16) + ':';
|
|
499
|
+
}
|
|
500
|
+
ipv6 = ipv6.slice(0, -1);
|
|
501
|
+
return { ipv6 };
|
|
502
|
+
}
|
|
503
|
+
return null;
|
|
504
|
+
};
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
export declare type ITransferInfo = {
|
|
2
|
+
from: string;
|
|
3
|
+
to: string;
|
|
4
|
+
amount: string;
|
|
5
|
+
token?: string;
|
|
6
|
+
isNFT?: boolean;
|
|
7
|
+
tokenId?: string;
|
|
8
|
+
type?: string;
|
|
9
|
+
};
|
|
10
|
+
export declare type IAdaAmount = {
|
|
11
|
+
unit: string;
|
|
12
|
+
quantity: string;
|
|
13
|
+
};
|
|
14
|
+
export declare type IAdaUTXO = {
|
|
15
|
+
path: string;
|
|
16
|
+
address: string;
|
|
17
|
+
tx_hash: string;
|
|
18
|
+
tx_index: number;
|
|
19
|
+
output_index: number;
|
|
20
|
+
amount: IAdaAmount[];
|
|
21
|
+
};
|
|
22
|
+
export declare type IOutput = {
|
|
23
|
+
address: string;
|
|
24
|
+
amount: string;
|
|
25
|
+
assets: [];
|
|
26
|
+
};
|
|
27
|
+
export declare type IEncodeInput = {
|
|
28
|
+
address: string;
|
|
29
|
+
amount: IAdaAmount[];
|
|
30
|
+
block: string;
|
|
31
|
+
data_hash: string;
|
|
32
|
+
outputIndex: number;
|
|
33
|
+
txHash: string;
|
|
34
|
+
tx_hash: string;
|
|
35
|
+
tx_index: number;
|
|
36
|
+
};
|
|
37
|
+
export declare type IEncodeOutput = {
|
|
38
|
+
address: string;
|
|
39
|
+
amount: string;
|
|
40
|
+
assets: IAdaAmount[];
|
|
41
|
+
isChange?: boolean;
|
|
42
|
+
};
|
|
43
|
+
declare type ITxInfo = {
|
|
44
|
+
body: string;
|
|
45
|
+
hash: string;
|
|
46
|
+
size: number;
|
|
47
|
+
};
|
|
48
|
+
export declare type IEncodedTxADA = {
|
|
49
|
+
inputs: IEncodeInput[];
|
|
50
|
+
outputs: IEncodeOutput[];
|
|
51
|
+
fee: string;
|
|
52
|
+
totalSpent: string;
|
|
53
|
+
totalFeeInNative: string;
|
|
54
|
+
transferInfo: ITransferInfo;
|
|
55
|
+
tx: ITxInfo;
|
|
56
|
+
signOnly?: boolean;
|
|
57
|
+
};
|
|
58
|
+
declare enum CardanoAddressType {
|
|
59
|
+
BASE = 0,
|
|
60
|
+
BASE_SCRIPT_KEY = 1,
|
|
61
|
+
BASE_KEY_SCRIPT = 2,
|
|
62
|
+
BASE_SCRIPT_SCRIPT = 3,
|
|
63
|
+
POINTER = 4,
|
|
64
|
+
POINTER_SCRIPT = 5,
|
|
65
|
+
ENTERPRISE = 6,
|
|
66
|
+
ENTERPRISE_SCRIPT = 7,
|
|
67
|
+
BYRON = 8,
|
|
68
|
+
REWARD = 14,
|
|
69
|
+
REWARD_SCRIPT = 15
|
|
70
|
+
}
|
|
71
|
+
export declare type IChangeAddress = {
|
|
72
|
+
address: string;
|
|
73
|
+
addressParameters: {
|
|
74
|
+
path: string;
|
|
75
|
+
addressType: CardanoAddressType;
|
|
76
|
+
stakingPath: string;
|
|
77
|
+
};
|
|
78
|
+
};
|
|
79
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var CardanoAddressType;
|
|
4
|
+
(function (CardanoAddressType) {
|
|
5
|
+
CardanoAddressType[CardanoAddressType["BASE"] = 0] = "BASE";
|
|
6
|
+
CardanoAddressType[CardanoAddressType["BASE_SCRIPT_KEY"] = 1] = "BASE_SCRIPT_KEY";
|
|
7
|
+
CardanoAddressType[CardanoAddressType["BASE_KEY_SCRIPT"] = 2] = "BASE_KEY_SCRIPT";
|
|
8
|
+
CardanoAddressType[CardanoAddressType["BASE_SCRIPT_SCRIPT"] = 3] = "BASE_SCRIPT_SCRIPT";
|
|
9
|
+
CardanoAddressType[CardanoAddressType["POINTER"] = 4] = "POINTER";
|
|
10
|
+
CardanoAddressType[CardanoAddressType["POINTER_SCRIPT"] = 5] = "POINTER_SCRIPT";
|
|
11
|
+
CardanoAddressType[CardanoAddressType["ENTERPRISE"] = 6] = "ENTERPRISE";
|
|
12
|
+
CardanoAddressType[CardanoAddressType["ENTERPRISE_SCRIPT"] = 7] = "ENTERPRISE_SCRIPT";
|
|
13
|
+
CardanoAddressType[CardanoAddressType["BYRON"] = 8] = "BYRON";
|
|
14
|
+
CardanoAddressType[CardanoAddressType["REWARD"] = 14] = "REWARD";
|
|
15
|
+
CardanoAddressType[CardanoAddressType["REWARD_SCRIPT"] = 15] = "REWARD_SCRIPT";
|
|
16
|
+
})(CardanoAddressType || (CardanoAddressType = {}));
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.signTransaction = exports.transformToTrezorOutputs = exports.transformToTrezorInputs = exports.transformToTokenBundle = void 0;
|
|
4
|
+
const transformations_1 = require("./transformations");
|
|
5
|
+
Object.defineProperty(exports, "transformToTokenBundle", { enumerable: true, get: function () { return transformations_1.transformToTokenBundle; } });
|
|
6
|
+
Object.defineProperty(exports, "transformToTrezorInputs", { enumerable: true, get: function () { return transformations_1.transformToTrezorInputs; } });
|
|
7
|
+
Object.defineProperty(exports, "transformToTrezorOutputs", { enumerable: true, get: function () { return transformations_1.transformToTrezorOutputs; } });
|
|
8
|
+
const sign_1 = require("./sign");
|
|
9
|
+
Object.defineProperty(exports, "signTransaction", { enumerable: true, get: function () { return sign_1.signTransaction; } });
|