@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,235 @@
|
|
|
1
|
+
import bs58 from 'bs58'
|
|
2
|
+
import Web3 from '../web3'
|
|
3
|
+
import axios, { AxiosResponse } from 'axios'
|
|
4
|
+
import Hashids from 'hashids/cjs'
|
|
5
|
+
import { MultiProvider } from './multiProvider'
|
|
6
|
+
import { uuid } from './uuid'
|
|
7
|
+
import {
|
|
8
|
+
importDataContractABIs,
|
|
9
|
+
importEthContractABIs
|
|
10
|
+
} from './importContractABI'
|
|
11
|
+
import { fileHasher } from './fileHasher'
|
|
12
|
+
import type { ImageHasher, NonImageHasher, HashedImage } from './fileHasher'
|
|
13
|
+
|
|
14
|
+
// Hashids
|
|
15
|
+
|
|
16
|
+
const HASH_SALT = 'azowernasdfoia'
|
|
17
|
+
const MIN_LENGTH = 5
|
|
18
|
+
const hashids = new Hashids(HASH_SALT, MIN_LENGTH)
|
|
19
|
+
|
|
20
|
+
const ZeroAddress = '0x0000000000000000000000000000000000000000'
|
|
21
|
+
|
|
22
|
+
export type { ImageHasher, NonImageHasher, HashedImage }
|
|
23
|
+
|
|
24
|
+
// eslint-disable-next-line @typescript-eslint/no-extraneous-class -- this should just be esm
|
|
25
|
+
export class Utils {
|
|
26
|
+
static importDataContractABI(pathStr: string) {
|
|
27
|
+
return importDataContractABIs(pathStr)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
static importEthContractABI(pathStr: string) {
|
|
31
|
+
return importEthContractABIs(pathStr)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
static utf8ToHex(utf8Str: string) {
|
|
35
|
+
return Web3.utils.utf8ToHex(utf8Str)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
static padRight(hexStr: string, size: number) {
|
|
39
|
+
return Web3.utils.padRight(hexStr, size)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
static hexToUtf8(hexStr: string) {
|
|
43
|
+
return Web3.utils.hexToUtf8(hexStr)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
static keccak256(utf8Str: string) {
|
|
47
|
+
return Web3.utils.keccak256(utf8Str)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
static isBN(number: number | string) {
|
|
51
|
+
return Web3.utils.isBN(number)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
static toBN(number: number, base?: number) {
|
|
55
|
+
return new Web3.utils.BN(number, base)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
static BN() {
|
|
59
|
+
return Web3.utils.BN
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
static checkStrLen(str: string, maxLen: number, minLen = 1) {
|
|
63
|
+
if (
|
|
64
|
+
str === undefined ||
|
|
65
|
+
str === null ||
|
|
66
|
+
str.length > maxLen ||
|
|
67
|
+
str.length < minLen
|
|
68
|
+
) {
|
|
69
|
+
throw new Error(
|
|
70
|
+
`String '${str}' must be between ${minLen}-${maxLen} characters`
|
|
71
|
+
)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
static async wait(milliseconds: number) {
|
|
76
|
+
return await new Promise<void>((resolve) =>
|
|
77
|
+
setTimeout(resolve, milliseconds)
|
|
78
|
+
)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Regular expression to check if endpoint is a FQDN. https://regex101.com/r/kIowvx/2
|
|
82
|
+
static isFQDN(url: string) {
|
|
83
|
+
const FQDN =
|
|
84
|
+
/(?:^|[ \t])((https?:\/\/)?(?:localhost|[\w-]+(?:\.[\w-]+)+)(:\d+)?(\/\S*)?)/gm
|
|
85
|
+
return FQDN.test(url)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
static isHttps(url: string) {
|
|
89
|
+
const https = /^https:\/\//
|
|
90
|
+
return https.test(url)
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Function to check if the endpont/health_check returns JSON object [ {'healthy':true} ]
|
|
94
|
+
static async isHealthy(url: string) {
|
|
95
|
+
try {
|
|
96
|
+
const { data: body } = await axios.get(url + '/health_check')
|
|
97
|
+
return body.data.healthy
|
|
98
|
+
} catch (error) {
|
|
99
|
+
return false
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
static formatOptionalMultihash(multihash: string) {
|
|
104
|
+
if (multihash) {
|
|
105
|
+
return this.decodeMultihash(multihash).digest
|
|
106
|
+
} else {
|
|
107
|
+
return this.utf8ToHex('')
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
static decodeMultihash(multihash: string) {
|
|
112
|
+
const base16Multihash = bs58.decode(multihash)
|
|
113
|
+
return {
|
|
114
|
+
digest: `0x${base16Multihash.slice(2).toString('hex')}`,
|
|
115
|
+
hashFn: parseInt(base16Multihash[0] as unknown as string),
|
|
116
|
+
size: parseInt(base16Multihash[1] as unknown as string)
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Given a digest value (written on chain, obtained through AudiusABIDecoder.decodeMethod),
|
|
122
|
+
* convert back to a IFPS CIDv0
|
|
123
|
+
* @param multihashDigest digest value from decodeMultihash
|
|
124
|
+
* @returns String CID value
|
|
125
|
+
*/
|
|
126
|
+
static encodeMultihash(multihashDigest: string) {
|
|
127
|
+
// the 1220 is from reconstructing the hashFn and size with digest, the opposite of decodeMultihash
|
|
128
|
+
// since IPFS CIDv0 has a fixed hashFn and size, the first two values are always 12 and 20
|
|
129
|
+
// concat them together with digest and encode back to base58
|
|
130
|
+
const digestStr = `1220${multihashDigest.replace('0x', '')}`
|
|
131
|
+
// convert digestStr from hex to base 58
|
|
132
|
+
return bs58.encode(Buffer.from(digestStr, 'hex'))
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
static parseDataFromResponse(response: AxiosResponse) {
|
|
136
|
+
if (!response || !response.data) return null
|
|
137
|
+
|
|
138
|
+
const obj = response.data
|
|
139
|
+
|
|
140
|
+
// adapted from https://github.com/jashkenas/underscore/blob/master/underscore.js _.isEmpty function
|
|
141
|
+
if (obj == null) return null
|
|
142
|
+
if ((Array.isArray(obj) || typeof obj === 'string') && obj.length === 0)
|
|
143
|
+
return null
|
|
144
|
+
if (Object.keys(obj).length === 0) return null
|
|
145
|
+
|
|
146
|
+
return obj
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
static async configureWeb3(
|
|
150
|
+
web3Provider: string,
|
|
151
|
+
chainNetworkId: string,
|
|
152
|
+
requiresAccount = true
|
|
153
|
+
) {
|
|
154
|
+
// Initializing web3 with a HttpProvider wrapper for multiple providers
|
|
155
|
+
// ref: https://github.com/ChainSafe/web3.js/blob/1.x/packages/web3/types/index.d.ts#L31.
|
|
156
|
+
const web3Instance = new Web3(new MultiProvider(web3Provider))
|
|
157
|
+
|
|
158
|
+
try {
|
|
159
|
+
const networkId = await web3Instance.eth.net.getId()
|
|
160
|
+
if (chainNetworkId && networkId.toString() !== chainNetworkId) {
|
|
161
|
+
return false
|
|
162
|
+
}
|
|
163
|
+
if (requiresAccount) {
|
|
164
|
+
const accounts = await web3Instance.eth.getAccounts()
|
|
165
|
+
if (!accounts || accounts.length < 1) {
|
|
166
|
+
return false
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
} catch (e) {
|
|
170
|
+
return false
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return web3Instance
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
static get zeroAddress() {
|
|
177
|
+
return ZeroAddress
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
static isZeroAddress(address: string) {
|
|
181
|
+
return address === Utils.zeroAddress
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
static makeUuid() {
|
|
185
|
+
return uuid()
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Decodes a string id into an int. Returns null if an invalid ID.
|
|
190
|
+
*/
|
|
191
|
+
static decodeHashId(id: string) {
|
|
192
|
+
try {
|
|
193
|
+
const ids = hashids.decode(id)
|
|
194
|
+
if (!ids.length) return null
|
|
195
|
+
const num = Number(ids[0])
|
|
196
|
+
if (isNaN(num)) return null
|
|
197
|
+
return num
|
|
198
|
+
} catch (e) {
|
|
199
|
+
console.error(`Failed to decode ${id}`, e)
|
|
200
|
+
return null
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Encodes an int to a string based hashid
|
|
206
|
+
*/
|
|
207
|
+
static encodeHashId(id: number | null) {
|
|
208
|
+
try {
|
|
209
|
+
if (id === null) return null
|
|
210
|
+
const encodedId = hashids.encode(id)
|
|
211
|
+
return encodedId
|
|
212
|
+
} catch (e) {
|
|
213
|
+
console.error(`Failed to encode ${id}`, e)
|
|
214
|
+
return null
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* If `promise` responds before `timeoutMs`,
|
|
220
|
+
* this function returns its response; else rejects with `timeoutMessage`
|
|
221
|
+
*/
|
|
222
|
+
static async racePromiseWithTimeout(
|
|
223
|
+
promise: Promise<void>,
|
|
224
|
+
timeoutMs: number,
|
|
225
|
+
timeoutMessage: string
|
|
226
|
+
) {
|
|
227
|
+
// eslint-disable-next-line promise/param-names
|
|
228
|
+
const timeoutPromise = new Promise((_promise, reject) => {
|
|
229
|
+
setTimeout(() => reject(new Error(timeoutMessage)), timeoutMs)
|
|
230
|
+
})
|
|
231
|
+
return await Promise.race([promise, timeoutPromise])
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
static fileHasher = fileHasher
|
|
235
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export const uuid = () => {
|
|
2
|
+
// https://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript/873856#873856
|
|
3
|
+
const s = []
|
|
4
|
+
const hexDigits = '0123456789abcdef'
|
|
5
|
+
for (let i = 0; i < 36; i++) {
|
|
6
|
+
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1)
|
|
7
|
+
}
|
|
8
|
+
s[14] = '4' // bits 12-15 of the time_hi_and_version field to 0010
|
|
9
|
+
s[19] = hexDigits.substr((s[19] as string & 0x3) | 0x8, 1) // bits 6-7 of the clock_seq_hi_and_reserved to 01
|
|
10
|
+
s[8] = s[13] = s[18] = s[23] = '-'
|
|
11
|
+
|
|
12
|
+
const uuid = s.join('')
|
|
13
|
+
return uuid
|
|
14
|
+
}
|
package/src/web3.d.ts
ADDED
package/src/web3.js
ADDED
|
Binary file
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
is mayonnaise an instrument
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
const assert = require('assert')
|
|
2
|
+
const helpers = require('./helpers')
|
|
3
|
+
|
|
4
|
+
const libs = helpers.audiusInstance
|
|
5
|
+
|
|
6
|
+
let token
|
|
7
|
+
let ownerWallet
|
|
8
|
+
let accounts
|
|
9
|
+
let initialSupply
|
|
10
|
+
|
|
11
|
+
before(async function () {
|
|
12
|
+
await libs.init()
|
|
13
|
+
token = libs.ethContracts.AudiusTokenClient
|
|
14
|
+
ownerWallet = libs.ethWeb3Manager.getWalletAddress()
|
|
15
|
+
accounts = await libs.ethWeb3Manager.getWeb3().eth.getAccounts()
|
|
16
|
+
toBN = libs.ethWeb3Manager.getWeb3().utils.toBN
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
it('Should test transfer', async function () {
|
|
20
|
+
const initialOwnerBalance = await token.balanceOf(ownerWallet)
|
|
21
|
+
const initialAccount1Balance = await token.balanceOf(accounts[1])
|
|
22
|
+
const amount = toBN('1000')
|
|
23
|
+
await token.transfer(accounts[1], amount)
|
|
24
|
+
|
|
25
|
+
const finalOwnerBalance = await token.balanceOf(ownerWallet)
|
|
26
|
+
const finalAccount1Balance = await token.balanceOf(accounts[1])
|
|
27
|
+
const expectedOwnerBalance = initialOwnerBalance.sub(amount)
|
|
28
|
+
const expectedAccount1Balance = initialAccount1Balance.add(amount)
|
|
29
|
+
assert(
|
|
30
|
+
finalOwnerBalance.eq(expectedOwnerBalance),
|
|
31
|
+
'Expect owner balance to decrease'
|
|
32
|
+
)
|
|
33
|
+
assert(
|
|
34
|
+
finalAccount1Balance.eq(expectedAccount1Balance),
|
|
35
|
+
'Expect account 1 balance to increase'
|
|
36
|
+
)
|
|
37
|
+
})
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const assert = require('assert')
|
|
2
|
+
let helpers = require('./helpers')
|
|
3
|
+
|
|
4
|
+
let audiusInstance = helpers.audiusInstance
|
|
5
|
+
|
|
6
|
+
before(async function () {
|
|
7
|
+
await audiusInstance.init()
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
it('should verify signature', async function () {
|
|
11
|
+
const address = await audiusInstance.web3Manager.verifySignature(helpers.constants.signatureData, helpers.constants.signature)
|
|
12
|
+
assert.strictEqual(address, helpers.constants.signatureAddress)
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
it('should detect malformed signature', async function () {
|
|
16
|
+
const malformedData = helpers.constants.signatureData.replace('1', '2')
|
|
17
|
+
const address = await audiusInstance.web3Manager.verifySignature(malformedData, helpers.constants.signature)
|
|
18
|
+
assert.notStrictEqual(address, helpers.constants.signatureAddress)
|
|
19
|
+
})
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
const assert = require('assert')
|
|
2
|
+
const fs = require('fs')
|
|
3
|
+
const path = require('path')
|
|
4
|
+
const uuid = require('uuid')
|
|
5
|
+
const sinon = require('sinon')
|
|
6
|
+
|
|
7
|
+
const { Utils } = require('../src/utils')
|
|
8
|
+
|
|
9
|
+
describe('test fileHasher with randomized text content', () => {
|
|
10
|
+
let randomText, randomTextFilePath
|
|
11
|
+
|
|
12
|
+
const createRandomText = () => {
|
|
13
|
+
return uuid.v4()
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const createRandomTextFile = () => {
|
|
17
|
+
const filePath = path.join(__dirname, './assets/random.txt')
|
|
18
|
+
fs.writeFileSync(filePath, createRandomText())
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
before(() => {
|
|
22
|
+
createRandomTextFile()
|
|
23
|
+
randomText = createRandomText()
|
|
24
|
+
randomTextFilePath = path.join(__dirname, './assets/random.txt')
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
afterEach(() => {
|
|
28
|
+
sinon.restore()
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
it('[random - generateNonImageCid] passing in improper content throws', async () => {
|
|
32
|
+
try {
|
|
33
|
+
await Utils.fileHasher.generateNonImageCid(randomText)
|
|
34
|
+
throw new Error('passing in improper data should have failed')
|
|
35
|
+
} catch (e) {
|
|
36
|
+
assert.ok(e.toString().includes('Could not convert content into buffer'))
|
|
37
|
+
}
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it('[random - generateNonImageCids] if `hashNonImages()` errors, `generateNonImageCid()` throws', async () => {
|
|
41
|
+
sinon
|
|
42
|
+
.stub(Utils.fileHasher, 'hashNonImages')
|
|
43
|
+
.throws(new Error('failed to generate only hash'))
|
|
44
|
+
|
|
45
|
+
try {
|
|
46
|
+
await Utils.fileHasher.generateNonImageCid(randomTextFilePath)
|
|
47
|
+
throw new Error(
|
|
48
|
+
'`generateNonImageCid` should throw if `hashNonImages` fails'
|
|
49
|
+
)
|
|
50
|
+
} catch (e) {
|
|
51
|
+
assert.ok(e.toString().includes('failed to generate only hash'))
|
|
52
|
+
}
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
it('[random - generateImageCids] if `hashImages()` errors, `generateImageCids()` throws', async () => {
|
|
56
|
+
sinon
|
|
57
|
+
.stub(Utils.fileHasher, 'hashImages')
|
|
58
|
+
.throws(new Error('failed to generate only hash'))
|
|
59
|
+
|
|
60
|
+
try {
|
|
61
|
+
await Utils.fileHasher.generateImageCids(randomTextFilePath)
|
|
62
|
+
throw new Error(
|
|
63
|
+
'`generateImageCids` should throw if `hashImages` fails'
|
|
64
|
+
)
|
|
65
|
+
} catch (e) {
|
|
66
|
+
assert.ok(e.toString().includes('failed to generate only hash'))
|
|
67
|
+
}
|
|
68
|
+
})
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
describe('test FileHasher with static content', () => {
|
|
72
|
+
const staticTextPath = path.join(__dirname, './assets/static_text.txt')
|
|
73
|
+
const staticImagePath = path.join(__dirname, './assets/static_image.png')
|
|
74
|
+
|
|
75
|
+
const files = [
|
|
76
|
+
// index 0 - static text
|
|
77
|
+
{
|
|
78
|
+
type: 'text',
|
|
79
|
+
path: staticTextPath,
|
|
80
|
+
buffer: fs.readFileSync(staticTextPath),
|
|
81
|
+
cid: 'QmNXk37dMvg8QYhy2S8AtV8stQRDCL91ybCretxFK8R4Yd'
|
|
82
|
+
},
|
|
83
|
+
// index 1 - static image
|
|
84
|
+
{
|
|
85
|
+
type: 'image',
|
|
86
|
+
path: staticImagePath,
|
|
87
|
+
buffer: fs.readFileSync(staticImagePath),
|
|
88
|
+
cid: 'QmQzgdAxHH2jsthbwHg6mGonniu2quqJPYNankGUK2Sn8a'
|
|
89
|
+
}
|
|
90
|
+
]
|
|
91
|
+
|
|
92
|
+
afterEach(() => {
|
|
93
|
+
sinon.restore()
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
for (let i = 0; i < files.length; i++) {
|
|
97
|
+
const filePath = files[i].path
|
|
98
|
+
|
|
99
|
+
it('[static - generateNonImageCid] passing in improper content throws', async () => {
|
|
100
|
+
try {
|
|
101
|
+
await Utils.fileHasher.generateNonImageCid('some static string')
|
|
102
|
+
throw new Error('passing in improper data should have failed')
|
|
103
|
+
} catch (e) {
|
|
104
|
+
assert.ok(
|
|
105
|
+
e.toString().includes('Could not convert content into buffer')
|
|
106
|
+
)
|
|
107
|
+
}
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
it('[static - generateNonImageCid] if `hashNonImages()` errors, `generateNonImageCid()` throws', async () => {
|
|
111
|
+
sinon
|
|
112
|
+
.stub(Utils.fileHasher, 'hashNonImages')
|
|
113
|
+
.throws(new Error('failed to generate only hash'))
|
|
114
|
+
|
|
115
|
+
try {
|
|
116
|
+
await Utils.fileHasher.generateNonImageCid(filePath)
|
|
117
|
+
throw new Error(
|
|
118
|
+
'`generateNonImageCid` should throw if `hashNonImages` fails'
|
|
119
|
+
)
|
|
120
|
+
} catch (e) {
|
|
121
|
+
assert.ok(e.toString().includes('failed to generate only hash'))
|
|
122
|
+
}
|
|
123
|
+
})
|
|
124
|
+
}
|
|
125
|
+
})
|