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