@pioneer-platform/pioneer-sdk 0.0.82 → 4.14.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.
@@ -0,0 +1,151 @@
1
+ import { Chain, NetworkIdToChain } from '@coinmasters/types';
2
+ import {
3
+ addressNListToBIP32,
4
+ //@ts-ignore
5
+ COIN_MAP_KEEPKEY_LONG,
6
+ xpubConvert,
7
+ } from '@pioneer-platform/pioneer-coins';
8
+
9
+ export const getPubkey = async (networkId: string, path: any, sdk: any, context: string) => {
10
+ try {
11
+ if (!path || !path.addressNList) {
12
+ throw new Error('Invalid or missing path provided for pubkey retrieval');
13
+ }
14
+ if (networkId.indexOf('eip155') > -1) networkId = 'eip155:*';
15
+ let chain: Chain = NetworkIdToChain[networkId];
16
+
17
+ let pubkey: any = { type: path.type };
18
+ let addressInfo = {
19
+ address_n: path.addressNListMaster,
20
+ //@ts-ignore
21
+ coin: COIN_MAP_KEEPKEY_LONG[chain],
22
+ script_type: path.script_type,
23
+ };
24
+ const networkIdToType: any = {
25
+ 'bip122:000000000019d6689c085ae165831e93': 'UTXO',
26
+ 'bip122:000000000000000000651ef99cb9fcbe': 'UTXO',
27
+ 'bip122:000007d91d1254d60e2dd1ae58038307': 'UTXO',
28
+ 'bip122:00000000001a91e3dace36e2be3bf030': 'UTXO',
29
+ 'bip122:12a765e31ffd4059bada1e25190f6e98': 'UTXO',
30
+ 'cosmos:mayachain-mainnet-v1': 'MAYACHAIN',
31
+ 'cosmos:osmosis-1': 'OSMOSIS',
32
+ 'cosmos:cosmoshub-4': 'COSMOS',
33
+ 'cosmos:kaiyo-1': 'COSMOS',
34
+ 'cosmos:thorchain-mainnet-v1': 'THORCHAIN',
35
+ 'eip155:1': 'EVM',
36
+ 'eip155:137': 'EVM',
37
+ 'eip155:*': 'EVM',
38
+ 'ripple:4109c6f2045fc7eff4cde8f9905d19c2': 'XRP',
39
+ 'zcash:main': 'UTXO',
40
+ };
41
+
42
+ const networkType = networkIdToType[networkId];
43
+ let address;
44
+
45
+ const addressTimeout = setTimeout(() => {
46
+ console.error('⏰ [PUBKEY] Address retrieval timeout for', networkId);
47
+ }, 30000);
48
+
49
+ try {
50
+ let result;
51
+ switch (networkType) {
52
+ case 'UTXO':
53
+ result = await sdk.address.utxoGetAddress(addressInfo);
54
+ break;
55
+ case 'EVM':
56
+ result = await sdk.address.ethereumGetAddress(addressInfo);
57
+ break;
58
+ case 'OSMOSIS':
59
+ result = await sdk.address.osmosisGetAddress(addressInfo);
60
+ break;
61
+ case 'COSMOS':
62
+ result = await sdk.address.cosmosGetAddress(addressInfo);
63
+ break;
64
+ case 'MAYACHAIN':
65
+ result = await sdk.address.mayachainGetAddress(addressInfo);
66
+ break;
67
+ case 'THORCHAIN':
68
+ result = await sdk.address.thorchainGetAddress(addressInfo);
69
+ break;
70
+ case 'XRP':
71
+ result = await sdk.address.xrpGetAddress(addressInfo);
72
+ break;
73
+ default:
74
+ throw new Error(`Unsupported network type for networkId: ${networkId}`);
75
+ }
76
+
77
+ if (!result) {
78
+ throw new Error(`Address call returned null/undefined for ${networkType} (${networkId})`);
79
+ }
80
+
81
+ if (typeof result === 'object' && result.address) {
82
+ address = result.address;
83
+ } else if (typeof result === 'string') {
84
+ address = result;
85
+ } else {
86
+ throw new Error(`Invalid address response format for ${networkType} (${networkId}): ${JSON.stringify(result)}`);
87
+ }
88
+
89
+ clearTimeout(addressTimeout);
90
+ } catch (addressError) {
91
+ clearTimeout(addressTimeout);
92
+ console.error('❌ [PUBKEY] Address retrieval failed:', addressError.message);
93
+ throw addressError;
94
+ }
95
+ if (!address) throw new Error(`Failed to get address for ${chain}`);
96
+ if (address.includes('bitcoincash:')) {
97
+ address = address.replace('bitcoincash:', '');
98
+ }
99
+ pubkey.master = address;
100
+ pubkey.address = address;
101
+ if (['xpub', 'ypub', 'zpub'].includes(path.type)) {
102
+ const pathQuery = {
103
+ symbol: 'BTC',
104
+ coin: 'Bitcoin',
105
+ script_type: 'p2pkh',
106
+ address_n: path.addressNList,
107
+ showDisplay: false,
108
+ };
109
+
110
+ const xpubTimeout = setTimeout(() => {
111
+ console.error('getPublicKey timeout after 20 seconds');
112
+ }, 20000);
113
+
114
+ try {
115
+ const responsePubkey = await sdk.system.info.getPublicKey(pathQuery);
116
+ clearTimeout(xpubTimeout);
117
+
118
+ if (path.script_type === 'p2wpkh') {
119
+ responsePubkey.xpub = xpubConvert(responsePubkey.xpub, 'zpub');
120
+ } else if (path.script_type === 'p2sh-p2wpkh') {
121
+ responsePubkey.xpub = xpubConvert(responsePubkey.xpub, 'ypub');
122
+ }
123
+
124
+ pubkey.pubkey = responsePubkey.xpub;
125
+ pubkey.path = addressNListToBIP32(path.addressNList);
126
+ pubkey.pathMaster = addressNListToBIP32(path.addressNListMaster);
127
+ } catch (xpubError) {
128
+ clearTimeout(xpubTimeout);
129
+ console.error('getPublicKey failed:', xpubError);
130
+ throw xpubError;
131
+ }
132
+ } else {
133
+ pubkey.pubkey = address;
134
+ pubkey.path = addressNListToBIP32(path.addressNList);
135
+ pubkey.pathMaster = addressNListToBIP32(path.addressNListMaster);
136
+ }
137
+
138
+ pubkey.scriptType = path.script_type;
139
+ pubkey.note = path.note;
140
+ pubkey.available_scripts_types = path.available_scripts_types;
141
+ pubkey.context = context;
142
+ pubkey.networks = path.networks;
143
+ pubkey.addressNList = path.addressNList;
144
+ pubkey.addressNListMaster = path.addressNListMaster;
145
+
146
+ return pubkey;
147
+ } catch (error) {
148
+ console.error('Error processing path:', error);
149
+ throw error;
150
+ }
151
+ };