@audius/sdk 0.0.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/.eslintrc +38 -0
- package/.prettierrc.js +1 -0
- package/.python-version +1 -0
- package/Dockerfile +15 -0
- package/README.md +3 -0
- package/babel.config.js +3 -0
- package/data-contracts/ABIs/AdminUpgradeabilityProxy.json +132 -0
- package/data-contracts/ABIs/BaseAdminUpgradeabilityProxy.json +113 -0
- package/data-contracts/ABIs/BaseUpgradeabilityProxy.json +22 -0
- package/data-contracts/ABIs/DiscoveryProviderFactory.json +189 -0
- package/data-contracts/ABIs/DiscoveryProviderFactoryInterface.json +61 -0
- package/data-contracts/ABIs/DiscoveryProviderStorage.json +205 -0
- package/data-contracts/ABIs/DiscoveryProviderStorageInterface.json +65 -0
- package/data-contracts/ABIs/ECDSA.json +4 -0
- package/data-contracts/ABIs/IPLDBlacklistFactory.json +168 -0
- package/data-contracts/ABIs/Initializable.json +4 -0
- package/data-contracts/ABIs/Migrations.json +67 -0
- package/data-contracts/ABIs/OpenZeppelinUpgradesAddress.json +4 -0
- package/data-contracts/ABIs/Ownable.json +79 -0
- package/data-contracts/ABIs/PlaylistFactory.json +669 -0
- package/data-contracts/ABIs/PlaylistFactoryInterface.json +42 -0
- package/data-contracts/ABIs/PlaylistStorage.json +250 -0
- package/data-contracts/ABIs/PlaylistStorageInterface.json +129 -0
- package/data-contracts/ABIs/Proxy.json +10 -0
- package/data-contracts/ABIs/Registry.json +240 -0
- package/data-contracts/ABIs/RegistryContract.json +102 -0
- package/data-contracts/ABIs/RegistryContractInterface.json +28 -0
- package/data-contracts/ABIs/RegistryInterface.json +66 -0
- package/data-contracts/ABIs/SigningLogic.json +43 -0
- package/data-contracts/ABIs/SigningLogicInitializable.json +46 -0
- package/data-contracts/ABIs/SocialFeatureFactory.json +460 -0
- package/data-contracts/ABIs/SocialFeatureStorage.json +225 -0
- package/data-contracts/ABIs/SocialFeatureStorageInterface.json +123 -0
- package/data-contracts/ABIs/TestContract.json +135 -0
- package/data-contracts/ABIs/TestContractInterface.json +19 -0
- package/data-contracts/ABIs/TestContractWithStorage.json +165 -0
- package/data-contracts/ABIs/TestContractWithStorageInterface.json +24 -0
- package/data-contracts/ABIs/TestStorage.json +144 -0
- package/data-contracts/ABIs/TestStorageInterface.json +42 -0
- package/data-contracts/ABIs/TestUserReplicaSetManager.json +432 -0
- package/data-contracts/ABIs/TrackFactory.json +391 -0
- package/data-contracts/ABIs/TrackFactoryInterface.json +73 -0
- package/data-contracts/ABIs/TrackStorage.json +223 -0
- package/data-contracts/ABIs/TrackStorageInterface.json +121 -0
- package/data-contracts/ABIs/UpgradeabilityProxy.json +37 -0
- package/data-contracts/ABIs/UserFactory.json +657 -0
- package/data-contracts/ABIs/UserFactoryInterface.json +65 -0
- package/data-contracts/ABIs/UserLibraryFactory.json +334 -0
- package/data-contracts/ABIs/UserReplicaSetManager.json +418 -0
- package/data-contracts/ABIs/UserStorage.json +233 -0
- package/data-contracts/ABIs/UserStorageInterface.json +93 -0
- package/data-contracts/signatureSchemas.ts +1236 -0
- package/dist/core.d.ts +446 -0
- package/dist/core.js +769 -0
- package/dist/core.js.map +1 -0
- package/dist/index.d.ts +689 -0
- package/dist/index.js +72850 -0
- package/dist/index.js.map +1 -0
- package/eth-contracts/ABIs/Address.json +4 -0
- package/eth-contracts/ABIs/AudiusAdminUpgradeabilityProxy.json +105 -0
- package/eth-contracts/ABIs/AudiusClaimDistributor.json +4968 -0
- package/eth-contracts/ABIs/AudiusToken.json +724 -0
- package/eth-contracts/ABIs/BaseUpgradeabilityProxy.json +23 -0
- package/eth-contracts/ABIs/Checkpointing.json +4 -0
- package/eth-contracts/ABIs/ClaimsManager.json +539 -0
- package/eth-contracts/ABIs/Context.json +11 -0
- package/eth-contracts/ABIs/DelegateManager.json +989 -0
- package/eth-contracts/ABIs/DelegateManagerV2.json +1049 -0
- package/eth-contracts/ABIs/DelegateManagerV2Bad.json +1049 -0
- package/eth-contracts/ABIs/ERC20.json +252 -0
- package/eth-contracts/ABIs/ERC20Burnable.json +287 -0
- package/eth-contracts/ABIs/ERC20Detailed.json +270 -0
- package/eth-contracts/ABIs/ERC20Mintable.json +364 -0
- package/eth-contracts/ABIs/ERC20Pausable.json +397 -0
- package/eth-contracts/ABIs/EthRewardsManager.json +174 -0
- package/eth-contracts/ABIs/Governance.json +938 -0
- package/eth-contracts/ABIs/GovernanceUpgraded.json +953 -0
- package/eth-contracts/ABIs/GovernanceV2.json +938 -0
- package/eth-contracts/ABIs/IERC20.json +200 -0
- package/eth-contracts/ABIs/Initializable.json +4 -0
- package/eth-contracts/ABIs/InitializableV2.json +14 -0
- package/eth-contracts/ABIs/Migrations.json +71 -0
- package/eth-contracts/ABIs/MinterRole.json +91 -0
- package/eth-contracts/ABIs/MockAccount.json +62 -0
- package/eth-contracts/ABIs/MockDelegateManager.json +55 -0
- package/eth-contracts/ABIs/MockStakingCaller.json +259 -0
- package/eth-contracts/ABIs/MockWormhole.json +106 -0
- package/eth-contracts/ABIs/OpenZeppelinUpgradesAddress.json +4 -0
- package/eth-contracts/ABIs/Ownable.json +93 -0
- package/eth-contracts/ABIs/Pausable.json +150 -0
- package/eth-contracts/ABIs/PauserRole.json +91 -0
- package/eth-contracts/ABIs/Proxy.json +10 -0
- package/eth-contracts/ABIs/Registry.json +288 -0
- package/eth-contracts/ABIs/Roles.json +4 -0
- package/eth-contracts/ABIs/SafeERC20.json +4 -0
- package/eth-contracts/ABIs/SafeMath.json +4 -0
- package/eth-contracts/ABIs/ServiceProviderFactory.json +1153 -0
- package/eth-contracts/ABIs/ServiceTypeManager.json +337 -0
- package/eth-contracts/ABIs/Staking.json +555 -0
- package/eth-contracts/ABIs/StakingUpgraded.json +570 -0
- package/eth-contracts/ABIs/TestContract.json +44 -0
- package/eth-contracts/ABIs/TrustedNotifierManager.json +265 -0
- package/eth-contracts/ABIs/Uint256Helpers.json +4 -0
- package/eth-contracts/ABIs/UpgradeabilityProxy.json +40 -0
- package/eth-contracts/ABIs/Wormhole.json +45 -0
- package/eth-contracts/ABIs/WormholeClient.json +155 -0
- package/examples/file.mp3 +0 -0
- package/examples/initAudiusLibs.js +86 -0
- package/examples/initializeVersions.js +95 -0
- package/examples/pic.jpg +0 -0
- package/initScripts/configureLocalDiscProv.js +167 -0
- package/initScripts/helpers/claim.js +43 -0
- package/initScripts/helpers/distributeTokens.js +24 -0
- package/initScripts/helpers/spRegistration.js +138 -0
- package/initScripts/helpers/utils.js +34 -0
- package/initScripts/helpers/version.js +93 -0
- package/initScripts/local.js +617 -0
- package/initScripts/mainnet.js +131 -0
- package/initScripts/manageProdRelayerWallets.js +191 -0
- package/package.json +125 -0
- package/rollup.config.js +164 -0
- package/scripts/AudiusClaimDistributor.json +4968 -0
- package/scripts/Wormhole.json +155 -0
- package/scripts/addCIDToIpldBlacklist.js +124 -0
- package/scripts/circleci-test.sh +53 -0
- package/scripts/communityRewards/transferCommunityRewardsToSolana.js +222 -0
- package/scripts/ipfs.sh +58 -0
- package/scripts/migrate_contracts.sh +25 -0
- package/scripts/reset.sh +65 -0
- package/scripts/test.sh +77 -0
- package/src/api/account.js +670 -0
- package/src/api/base.js +122 -0
- package/src/api/file.js +168 -0
- package/src/api/playlist.js +328 -0
- package/src/api/rewards.d.ts +4 -0
- package/src/api/rewards.js +682 -0
- package/src/api/serviceProvider.js +154 -0
- package/src/api/track.js +604 -0
- package/src/api/user.js +888 -0
- package/src/api/user.test.js +172 -0
- package/src/constants.ts +7 -0
- package/src/core.ts +3 -0
- package/src/index.js +6 -0
- package/src/libs.d.ts +3 -0
- package/src/libs.js +619 -0
- package/src/sanityChecks/addSecondaries.js +40 -0
- package/src/sanityChecks/assignReplicaSetIfNecessary.js +10 -0
- package/src/sanityChecks/index.d.ts +9 -0
- package/src/sanityChecks/index.js +31 -0
- package/src/sanityChecks/isCreator.js +73 -0
- package/src/sanityChecks/needsRecoveryEmail.js +20 -0
- package/src/sanityChecks/rolloverNodes.js +74 -0
- package/src/sanityChecks/sanitizeNodes.js +24 -0
- package/src/sanityChecks/syncNodes.js +28 -0
- package/src/sdk/constants.ts +10 -0
- package/src/sdk/index.ts +1 -0
- package/src/sdk/oauth/Oauth.ts +265 -0
- package/src/sdk/oauth/index.ts +1 -0
- package/src/sdk/sdk.ts +102 -0
- package/src/service-selection/ServiceSelection.test.ts +320 -0
- package/src/service-selection/ServiceSelection.ts +460 -0
- package/src/service-selection/constants.ts +14 -0
- package/src/service-selection/index.ts +1 -0
- package/src/services/ABIDecoder/AudiusABIDecoder.ts +71 -0
- package/src/services/ABIDecoder/index.ts +1 -0
- package/src/services/comstock/Comstock.ts +39 -0
- package/src/services/comstock/index.ts +1 -0
- package/src/services/contracts/ContractClient.ts +227 -0
- package/src/services/contracts/GovernedContractClient.ts +53 -0
- package/src/services/contracts/ProviderSelection.ts +42 -0
- package/src/services/creatorNode/CreatorNode.ts +1065 -0
- package/src/services/creatorNode/CreatorNodeSelection.test.ts +997 -0
- package/src/services/creatorNode/CreatorNodeSelection.ts +488 -0
- package/src/services/creatorNode/constants.ts +10 -0
- package/src/services/creatorNode/index.ts +2 -0
- package/src/services/dataContracts/AudiusContracts.ts +234 -0
- package/src/services/dataContracts/IPLDBlacklistFactoryClient.ts +73 -0
- package/src/services/dataContracts/PlaylistFactoryClient.ts +370 -0
- package/src/services/dataContracts/RegistryClient.ts +95 -0
- package/src/services/dataContracts/SocialFeatureFactoryClient.ts +196 -0
- package/src/services/dataContracts/TrackFactoryClient.ts +131 -0
- package/src/services/dataContracts/UserFactoryClient.ts +351 -0
- package/src/services/dataContracts/UserLibraryFactoryClient.ts +115 -0
- package/src/services/dataContracts/UserReplicaSetManagerClient.ts +206 -0
- package/src/services/dataContracts/index.ts +1 -0
- package/src/services/discoveryProvider/DiscoveryProvider.ts +1168 -0
- package/src/services/discoveryProvider/DiscoveryProviderSelection.test.ts +536 -0
- package/src/services/discoveryProvider/DiscoveryProviderSelection.ts +383 -0
- package/src/services/discoveryProvider/constants.ts +13 -0
- package/src/services/discoveryProvider/index.ts +1 -0
- package/src/services/discoveryProvider/requests.ts +629 -0
- package/src/services/ethContracts/AudiusTokenClient.ts +163 -0
- package/src/services/ethContracts/ClaimDistributionClient.ts +45 -0
- package/src/services/ethContracts/ClaimsManagerClient.ts +102 -0
- package/src/services/ethContracts/DelegateManagerClient.ts +480 -0
- package/src/services/ethContracts/EthContracts.ts +359 -0
- package/src/services/ethContracts/EthRewardsManagerClient.ts +33 -0
- package/src/services/ethContracts/GovernanceClient.ts +451 -0
- package/src/services/ethContracts/RegistryClient.ts +33 -0
- package/src/services/ethContracts/ServiceProviderFactoryClient.ts +691 -0
- package/src/services/ethContracts/ServiceTypeManagerClient.ts +112 -0
- package/src/services/ethContracts/StakingProxyClient.ts +97 -0
- package/src/services/ethContracts/TrustedNotifierManagerClient.ts +101 -0
- package/src/services/ethContracts/WormholeClient.ts +97 -0
- package/src/services/ethContracts/index.ts +1 -0
- package/src/services/ethWeb3Manager/EthWeb3Manager.ts +239 -0
- package/src/services/ethWeb3Manager/index.ts +1 -0
- package/src/services/hedgehog/Hedgehog.ts +96 -0
- package/src/services/hedgehog/index.ts +1 -0
- package/src/services/identity/IdentityService.ts +551 -0
- package/src/services/identity/index.ts +1 -0
- package/src/services/identity/requests.ts +65 -0
- package/src/services/schemaValidator/SchemaValidator.ts +105 -0
- package/src/services/schemaValidator/index.ts +1 -0
- package/src/services/schemaValidator/schemas/trackSchema.json +267 -0
- package/src/services/schemaValidator/schemas/userSchema.json +230 -0
- package/src/services/solanaAudiusData/errors.ts +20 -0
- package/src/services/solanaAudiusData/index.ts +1189 -0
- package/src/services/solanaWeb3Manager/errors.js +101 -0
- package/src/services/solanaWeb3Manager/index.d.ts +46 -0
- package/src/services/solanaWeb3Manager/index.js +655 -0
- package/src/services/solanaWeb3Manager/padBNToUint8Array.ts +7 -0
- package/src/services/solanaWeb3Manager/rewards.js +941 -0
- package/src/services/solanaWeb3Manager/rewardsAttester.ts +1093 -0
- package/src/services/solanaWeb3Manager/tokenAccount.js +149 -0
- package/src/services/solanaWeb3Manager/transactionHandler.js +345 -0
- package/src/services/solanaWeb3Manager/transfer.js +272 -0
- package/src/services/solanaWeb3Manager/userBank.js +160 -0
- package/src/services/solanaWeb3Manager/utils.d.ts +31 -0
- package/src/services/solanaWeb3Manager/utils.js +163 -0
- package/src/services/solanaWeb3Manager/wAudio.js +28 -0
- package/src/services/solanaWeb3Manager/wAudio.test.js +30 -0
- package/src/services/web3Manager/Web3Config.ts +14 -0
- package/src/services/web3Manager/Web3Manager.ts +360 -0
- package/src/services/web3Manager/XMLHttpRequest.ts +11 -0
- package/src/services/web3Manager/index.ts +2 -0
- package/src/services/wormhole/index.js +424 -0
- package/src/types.ts +8 -0
- package/src/userStateManager.ts +53 -0
- package/src/utils/apiSigning.ts +51 -0
- package/src/utils/captcha.ts +97 -0
- package/src/utils/estimateGas.ts +64 -0
- package/src/utils/fileHasher.ts +278 -0
- package/src/utils/importContractABI.d.ts +9 -0
- package/src/utils/importContractABI.js +19 -0
- package/src/utils/index.ts +11 -0
- package/src/utils/multiProvider.ts +72 -0
- package/src/utils/network.test.ts +127 -0
- package/src/utils/network.ts +308 -0
- package/src/utils/promiseFight.test.ts +87 -0
- package/src/utils/promiseFight.ts +36 -0
- package/src/utils/signatures.ts +139 -0
- package/src/utils/types.ts +34 -0
- package/src/utils/utils.test.ts +36 -0
- package/src/utils/utils.ts +235 -0
- package/src/utils/uuid.ts +14 -0
- package/src/web3.d.ts +9 -0
- package/src/web3.js +8 -0
- package/tests/assets/static_image.png +0 -0
- package/tests/assets/static_text.txt +1 -0
- package/tests/audiusTokenClientTest.js +37 -0
- package/tests/creatorNodeTest.js +19 -0
- package/tests/fileHasherTest.js +125 -0
- package/tests/governanceTest.js +382 -0
- package/tests/helpers.js +105 -0
- package/tests/index.js +14 -0
- package/tests/playlistClientTest.js +157 -0
- package/tests/providerSelectionTest.js +241 -0
- package/tests/registryClientTest.js +19 -0
- package/tests/rewardsAttesterTest.js +373 -0
- package/tests/serviceTypeManagerClientTest.js +33 -0
- package/tests/socialFeatureClientTest.js +79 -0
- package/tests/stakingTest.js +302 -0
- package/tests/trackClientTest.js +86 -0
- package/tests/userClientTest.js +121 -0
- package/tsconfig.json +10 -0
- package/types/@audius-hedgehog/index.d.ts +39 -0
- package/types/abi-decoder/index.d.ts +41 -0
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
import Web3 from '../../web3'
|
|
2
|
+
import sigUtil from 'eth-sig-util'
|
|
3
|
+
import retry from 'async-retry'
|
|
4
|
+
import { ContractMethod, estimateGas } from '../../utils'
|
|
5
|
+
import { AudiusABIDecoder } from '../ABIDecoder'
|
|
6
|
+
import EthereumWallet from 'ethereumjs-wallet'
|
|
7
|
+
import { XMLHttpRequest } from './XMLHttpRequest'
|
|
8
|
+
import type { Web3Config } from './Web3Config'
|
|
9
|
+
import type { IdentityService } from '../identity'
|
|
10
|
+
import type { Hedgehog } from '@audius/hedgehog'
|
|
11
|
+
import type Web3Type from 'web3'
|
|
12
|
+
import type { HttpProvider, TransactionReceipt, EventLog } from 'web3-core'
|
|
13
|
+
import type { EIP712TypedData } from 'eth-sig-util'
|
|
14
|
+
import type { DecodedLog } from 'abi-decoder'
|
|
15
|
+
|
|
16
|
+
const DEFAULT_GAS_LIMIT = 2000000
|
|
17
|
+
|
|
18
|
+
export type Web3ManagerConfig = {
|
|
19
|
+
web3Config: Web3Config
|
|
20
|
+
identityService: IdentityService
|
|
21
|
+
hedgehog: Hedgehog
|
|
22
|
+
isServer?: boolean
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** singleton class to be instantiated and persisted with every AudiusLibs */
|
|
26
|
+
export class Web3Manager {
|
|
27
|
+
web3Config: Web3Config
|
|
28
|
+
isServer: boolean
|
|
29
|
+
identityService: IdentityService
|
|
30
|
+
hedgehog: Hedgehog
|
|
31
|
+
AudiusABIDecoder: typeof AudiusABIDecoder
|
|
32
|
+
web3: Web3Type | undefined
|
|
33
|
+
useExternalWeb3: boolean | undefined
|
|
34
|
+
// @ts-expect-error an error is thrown if it's not provided
|
|
35
|
+
ownerWallet: EthereumWallet
|
|
36
|
+
|
|
37
|
+
constructor({
|
|
38
|
+
web3Config,
|
|
39
|
+
identityService,
|
|
40
|
+
hedgehog,
|
|
41
|
+
isServer = false
|
|
42
|
+
}: Web3ManagerConfig) {
|
|
43
|
+
this.web3Config = web3Config
|
|
44
|
+
this.isServer = isServer
|
|
45
|
+
|
|
46
|
+
// Unset if externalWeb3 = true
|
|
47
|
+
this.identityService = identityService
|
|
48
|
+
this.hedgehog = hedgehog
|
|
49
|
+
this.AudiusABIDecoder = AudiusABIDecoder
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async init() {
|
|
53
|
+
const web3Config = this.web3Config
|
|
54
|
+
if (!web3Config) throw new Error('Failed to initialize Web3Manager')
|
|
55
|
+
|
|
56
|
+
if (
|
|
57
|
+
// External Web3
|
|
58
|
+
web3Config?.useExternalWeb3 &&
|
|
59
|
+
web3Config.externalWeb3Config?.web3 &&
|
|
60
|
+
web3Config.externalWeb3Config.ownerWallet
|
|
61
|
+
) {
|
|
62
|
+
this.web3 = web3Config.externalWeb3Config.web3
|
|
63
|
+
this.useExternalWeb3 = true
|
|
64
|
+
this.ownerWallet = web3Config.externalWeb3Config.ownerWallet
|
|
65
|
+
} else if (
|
|
66
|
+
// Internal Web3
|
|
67
|
+
web3Config &&
|
|
68
|
+
!web3Config.useExternalWeb3 &&
|
|
69
|
+
web3Config.internalWeb3Config?.web3ProviderEndpoints
|
|
70
|
+
) {
|
|
71
|
+
// either user has external web3 but it's not configured, or doesn't have web3
|
|
72
|
+
this.web3 = new Web3(
|
|
73
|
+
this.provider(
|
|
74
|
+
web3Config.internalWeb3Config.web3ProviderEndpoints[0] as string,
|
|
75
|
+
10000
|
|
76
|
+
)
|
|
77
|
+
)
|
|
78
|
+
this.useExternalWeb3 = false
|
|
79
|
+
|
|
80
|
+
if (web3Config.internalWeb3Config.privateKey) {
|
|
81
|
+
const pkeyBuffer = Buffer.from(
|
|
82
|
+
web3Config.internalWeb3Config.privateKey,
|
|
83
|
+
'hex'
|
|
84
|
+
)
|
|
85
|
+
this.ownerWallet = EthereumWallet.fromPrivateKey(pkeyBuffer)
|
|
86
|
+
return
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// create private key pair here if it doesn't already exist
|
|
90
|
+
const storedWallet = this.hedgehog.getWallet()
|
|
91
|
+
if (storedWallet) {
|
|
92
|
+
this.ownerWallet = storedWallet
|
|
93
|
+
} else {
|
|
94
|
+
const passwordEntropy = `audius-dummy-pkey-${Math.floor(
|
|
95
|
+
Math.random() * 1000000
|
|
96
|
+
)}`
|
|
97
|
+
this.ownerWallet = await this.hedgehog.createWalletObj(passwordEntropy)
|
|
98
|
+
}
|
|
99
|
+
} else {
|
|
100
|
+
throw new Error("web3ProviderEndpoint isn't passed into constructor")
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
getWeb3() {
|
|
105
|
+
return this.web3 as Web3Type
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
setWeb3(web3: Web3Type) {
|
|
109
|
+
this.web3 = web3
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
getWalletAddress() {
|
|
113
|
+
if (this.useExternalWeb3) {
|
|
114
|
+
// Lowercase the owner wallet. Consider using the checksum address.
|
|
115
|
+
// See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md.
|
|
116
|
+
// @ts-expect-error Wallet type doesn't have `toLowerCase` method?
|
|
117
|
+
return this.ownerWallet.toLowerCase()
|
|
118
|
+
} else {
|
|
119
|
+
return this.ownerWallet.getAddressString()
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
setOwnerWallet(ownerWallet: EthereumWallet) {
|
|
124
|
+
this.ownerWallet = ownerWallet
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
web3IsExternal() {
|
|
128
|
+
return this.useExternalWeb3
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
getOwnerWalletPrivateKey() {
|
|
132
|
+
if (this.useExternalWeb3) {
|
|
133
|
+
throw new Error("Can't get owner wallet private key for external web3")
|
|
134
|
+
} else {
|
|
135
|
+
return this.ownerWallet.getPrivateKey()
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Signs provided string data (should be timestamped).
|
|
141
|
+
* @param data
|
|
142
|
+
*/
|
|
143
|
+
async sign(data: string) {
|
|
144
|
+
if (this.useExternalWeb3) {
|
|
145
|
+
const account = this.getWalletAddress()
|
|
146
|
+
if (this.isServer) {
|
|
147
|
+
return await this.web3?.eth.sign(
|
|
148
|
+
this.web3.utils.fromUtf8(data),
|
|
149
|
+
account
|
|
150
|
+
)
|
|
151
|
+
} else {
|
|
152
|
+
return await this.web3?.eth.personal.sign(
|
|
153
|
+
this.web3.utils.fromUtf8(data),
|
|
154
|
+
account,
|
|
155
|
+
''
|
|
156
|
+
)
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return sigUtil.personalSign(this.getOwnerWalletPrivateKey(), { data })
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Given a data payload and signature, verifies that signature is valid, and returns
|
|
165
|
+
* Ethereum wallet address used to sign data.
|
|
166
|
+
* @param data information that was signed
|
|
167
|
+
* @param signature hex-formatted signature of data generated by web3 personalSign method
|
|
168
|
+
*/
|
|
169
|
+
async verifySignature(data: string, signature: string) {
|
|
170
|
+
return sigUtil.recoverPersonalSignature({ data: data, sig: signature })
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
async signTypedData(signatureData: EIP712TypedData) {
|
|
174
|
+
if (this.useExternalWeb3) {
|
|
175
|
+
return await ethSignTypedData(
|
|
176
|
+
this.getWeb3(),
|
|
177
|
+
this.getWalletAddress(),
|
|
178
|
+
signatureData
|
|
179
|
+
)
|
|
180
|
+
} else {
|
|
181
|
+
// Due to changes in ethereumjs-util's toBuffer method as of v6.2.0
|
|
182
|
+
// non hex-prefixed string values are not permitted and need to be
|
|
183
|
+
// provided directly as a buffer.
|
|
184
|
+
// https://github.com/ethereumjs/ethereumjs-util/releases/tag/v6.2.0
|
|
185
|
+
Object.keys(signatureData.message).forEach((key) => {
|
|
186
|
+
const message = signatureData.message[key]
|
|
187
|
+
if (typeof message === 'string' && !message.startsWith('0x')) {
|
|
188
|
+
signatureData.message[key] = Buffer.from(message)
|
|
189
|
+
}
|
|
190
|
+
})
|
|
191
|
+
return sigUtil.signTypedData(this.ownerWallet.getPrivateKey(), {
|
|
192
|
+
data: signatureData
|
|
193
|
+
})
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
async sendTransaction(
|
|
198
|
+
contractMethod: ContractMethod,
|
|
199
|
+
contractRegistryKey?: string | null,
|
|
200
|
+
contractAddress?: string | null,
|
|
201
|
+
txRetries = 5,
|
|
202
|
+
txGasLimit?: number
|
|
203
|
+
): Promise<TransactionReceipt> {
|
|
204
|
+
const gasLimit =
|
|
205
|
+
txGasLimit ??
|
|
206
|
+
(await estimateGas({
|
|
207
|
+
method: contractMethod,
|
|
208
|
+
gasLimitMaximum: DEFAULT_GAS_LIMIT
|
|
209
|
+
}))
|
|
210
|
+
if (this.useExternalWeb3) {
|
|
211
|
+
return await contractMethod.send({
|
|
212
|
+
from: this.ownerWallet,
|
|
213
|
+
gas: gasLimit
|
|
214
|
+
})
|
|
215
|
+
} else {
|
|
216
|
+
const encodedABI = contractMethod.encodeABI()
|
|
217
|
+
|
|
218
|
+
const response = await retry(
|
|
219
|
+
async () => {
|
|
220
|
+
return await this.identityService.relay(
|
|
221
|
+
contractRegistryKey,
|
|
222
|
+
contractAddress,
|
|
223
|
+
this.ownerWallet.getAddressString(),
|
|
224
|
+
encodedABI,
|
|
225
|
+
gasLimit
|
|
226
|
+
)
|
|
227
|
+
},
|
|
228
|
+
{
|
|
229
|
+
// Retry function 5x by default
|
|
230
|
+
// 1st retry delay = 500ms, 2nd = 1500ms, 3rd...nth retry = 4000 ms (capped)
|
|
231
|
+
minTimeout: 500,
|
|
232
|
+
maxTimeout: 4000,
|
|
233
|
+
factor: 3,
|
|
234
|
+
retries: txRetries,
|
|
235
|
+
onRetry: (err) => {
|
|
236
|
+
if (err) {
|
|
237
|
+
console.log(
|
|
238
|
+
// eslint-disable-next-line @typescript-eslint/no-base-to-string
|
|
239
|
+
`libs web3Manager transaction send retry error : ${err}`
|
|
240
|
+
)
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
)
|
|
245
|
+
|
|
246
|
+
const receipt = response.receipt
|
|
247
|
+
|
|
248
|
+
// interestingly, using contractMethod.send from Metamask's web3 (eg. like in the if
|
|
249
|
+
// above) parses the event log into an 'events' key on the transaction receipt and
|
|
250
|
+
// blows away the 'logs' key. However, using sendRawTransaction as our
|
|
251
|
+
// relayer does, returns only the logs. Here, we replicate the part of the 'events'
|
|
252
|
+
// key that our code consumes, but we may want to change our functions to consume
|
|
253
|
+
// this data in a different way in future (this parsing is messy).
|
|
254
|
+
// More on Metamask's / Web3.js' behavior here:
|
|
255
|
+
// https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#methods-mymethod-send
|
|
256
|
+
if (receipt.logs) {
|
|
257
|
+
const events: TransactionReceipt['events'] = {}
|
|
258
|
+
// TODO: decodeLogs appears to return DecodedLog, not DecodedLog[] so maybe a type/version issue
|
|
259
|
+
const decoded = this.AudiusABIDecoder.decodeLogs(
|
|
260
|
+
contractRegistryKey as string,
|
|
261
|
+
receipt.logs
|
|
262
|
+
) as unknown as DecodedLog[]
|
|
263
|
+
decoded.forEach((evt) => {
|
|
264
|
+
const returnValues: Record<string, string> = {}
|
|
265
|
+
evt.events.forEach((arg) => {
|
|
266
|
+
returnValues[arg.name] = arg.value
|
|
267
|
+
})
|
|
268
|
+
const eventLog = { returnValues }
|
|
269
|
+
events[evt.name] = eventLog as EventLog
|
|
270
|
+
})
|
|
271
|
+
receipt.events = events
|
|
272
|
+
}
|
|
273
|
+
return response.receipt
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// TODO - Remove this. Adapted from https://github.com/raiden-network/webui/pull/51/files
|
|
278
|
+
// Vendored code below
|
|
279
|
+
provider(url: string, timeout: number) {
|
|
280
|
+
return this.monkeyPatchProvider(
|
|
281
|
+
new Web3.providers.HttpProvider(url, { timeout })
|
|
282
|
+
)
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// TODO: Workaround for https://github.com/ethereum/web3.js/issues/1803 it should be immediately removed
|
|
286
|
+
// as soon as the issue is fixed upstream.
|
|
287
|
+
// Issue is also documented here https://github.com/ethereum/web3.js/issues/1802
|
|
288
|
+
monkeyPatchProvider(httpProvider: HttpProvider) {
|
|
289
|
+
// @ts-expect-error overriding a private method not appearing in types
|
|
290
|
+
override(httpProvider, '_prepareRequest', function () {
|
|
291
|
+
return function (
|
|
292
|
+
this: HttpProvider & {
|
|
293
|
+
timeout: number
|
|
294
|
+
headers: Array<{ name: string; value: string }>
|
|
295
|
+
}
|
|
296
|
+
) {
|
|
297
|
+
const request = new XMLHttpRequest()
|
|
298
|
+
|
|
299
|
+
request.open('POST', this.host, true)
|
|
300
|
+
request.setRequestHeader('Content-Type', 'application/json')
|
|
301
|
+
request.timeout = this.timeout && this.timeout !== 1 ? this.timeout : 0
|
|
302
|
+
|
|
303
|
+
if (this.headers) {
|
|
304
|
+
this.headers.forEach(function (header) {
|
|
305
|
+
request.setRequestHeader(header.name, header.value)
|
|
306
|
+
})
|
|
307
|
+
}
|
|
308
|
+
return request
|
|
309
|
+
}
|
|
310
|
+
})
|
|
311
|
+
return httpProvider
|
|
312
|
+
}
|
|
313
|
+
// End vendored code
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/** Browser and testing-compatible signTypedData */
|
|
317
|
+
const ethSignTypedData = async (
|
|
318
|
+
web3: Web3Type,
|
|
319
|
+
wallet: EthereumWallet,
|
|
320
|
+
signatureData: EIP712TypedData
|
|
321
|
+
) => {
|
|
322
|
+
return await new Promise((resolve, reject) => {
|
|
323
|
+
let processedSignatureData: EIP712TypedData | string = signatureData
|
|
324
|
+
let method
|
|
325
|
+
// @ts-expect-error isMetaMask not captured by web3Provider
|
|
326
|
+
if (web3.currentProvider.isMetaMask === true) {
|
|
327
|
+
method = 'eth_signTypedData_v3'
|
|
328
|
+
processedSignatureData = JSON.stringify(signatureData)
|
|
329
|
+
} else {
|
|
330
|
+
method = 'eth_signTypedData'
|
|
331
|
+
// fix per https://github.com/ethereum/web3.js/issues/1119
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
;(web3.currentProvider as HttpProvider).send(
|
|
335
|
+
{
|
|
336
|
+
method: method,
|
|
337
|
+
params: [wallet, processedSignatureData],
|
|
338
|
+
// @ts-expect-error from not in JsonRpcPayload
|
|
339
|
+
from: wallet
|
|
340
|
+
},
|
|
341
|
+
(err, result) => {
|
|
342
|
+
if (err) {
|
|
343
|
+
reject(err)
|
|
344
|
+
} else if (result?.error) {
|
|
345
|
+
reject(result?.error)
|
|
346
|
+
} else {
|
|
347
|
+
resolve(result?.result)
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
)
|
|
351
|
+
})
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
function override<Class, K extends keyof Class, T extends Class[K] & Function>(
|
|
355
|
+
object: Class,
|
|
356
|
+
methodName: K,
|
|
357
|
+
callback: T
|
|
358
|
+
) {
|
|
359
|
+
object[methodName] = callback(object[methodName])
|
|
360
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
2
|
+
|
|
3
|
+
let XMLHttpRequestRef: typeof window.XMLHttpRequest
|
|
4
|
+
|
|
5
|
+
if (typeof window === 'undefined' || window === null) {
|
|
6
|
+
XMLHttpRequestRef = require('xmlhttprequest').XMLHttpRequest
|
|
7
|
+
} else {
|
|
8
|
+
XMLHttpRequestRef = window.XMLHttpRequest
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export { XMLHttpRequestRef as XMLHttpRequest }
|