@alephium/web3 0.2.0-rc.1 → 0.2.0-rc.12
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/.eslintignore +2 -2
- package/README.md +2 -135
- package/contracts/greeter/greeter.ral +2 -2
- package/contracts/greeter/greeter_interface.ral +1 -0
- package/contracts/greeter_main.ral +1 -3
- package/dist/alephium-web3.min.js +1 -1
- package/dist/alephium-web3.min.js.LICENSE.txt +0 -17
- package/dist/alephium-web3.min.js.map +1 -1
- package/dist/scripts/create-project.js +3 -2
- package/dist/src/api/api-alephium.d.ts +44 -11
- package/dist/src/api/api-alephium.js +81 -74
- package/dist/src/api/api-explorer.d.ts +16 -16
- package/dist/src/api/api-explorer.js +24 -33
- package/dist/src/api/index.d.ts +3 -2
- package/dist/src/api/index.js +22 -3
- package/dist/src/api/types.d.ts +23 -0
- package/dist/src/api/types.js +240 -0
- package/dist/src/api/utils.d.ts +6 -0
- package/{src/utils/djb2.test.ts → dist/src/api/utils.js} +11 -17
- package/dist/src/contract/contract.d.ts +104 -64
- package/dist/src/contract/contract.js +370 -426
- package/dist/src/contract/events.d.ts +4 -4
- package/dist/src/contract/index.js +5 -1
- package/dist/src/contract/ralph.d.ts +4 -4
- package/dist/src/global.d.ts +4 -0
- package/dist/src/global.js +38 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +20 -1
- package/dist/src/signer/index.d.ts +0 -1
- package/dist/src/signer/index.js +5 -2
- package/dist/src/signer/signer.d.ts +21 -11
- package/dist/src/signer/signer.js +46 -9
- package/dist/src/transaction/index.d.ts +0 -1
- package/dist/src/transaction/index.js +5 -2
- package/dist/src/transaction/status.d.ts +2 -1
- package/dist/src/utils/index.d.ts +0 -1
- package/dist/src/utils/index.js +5 -2
- package/dist/src/utils/subscription.d.ts +0 -1
- package/dist/src/utils/subscription.js +2 -1
- package/dist/src/utils/utils.d.ts +5 -11
- package/dist/src/utils/utils.js +17 -25
- package/jest-config.json +11 -0
- package/package.json +8 -36
- package/scripts/create-project.ts +3 -2
- package/src/api/api-alephium.ts +54 -16
- package/src/api/api-explorer.ts +1 -11
- package/src/api/index.ts +14 -3
- package/src/api/types.ts +233 -0
- package/src/{utils/password-crypto.test.ts → api/utils.ts} +7 -9
- package/src/contract/contract.ts +537 -526
- package/src/contract/events.ts +6 -6
- package/src/contract/ralph.ts +4 -4
- package/src/{transaction/sign-verify.ts → global.ts} +14 -15
- package/src/index.ts +3 -0
- package/src/signer/index.ts +0 -1
- package/src/signer/signer.ts +70 -19
- package/src/transaction/index.ts +0 -1
- package/src/transaction/status.ts +4 -2
- package/src/utils/index.ts +0 -1
- package/src/utils/subscription.ts +3 -3
- package/src/utils/utils.ts +10 -20
- package/templates/base/package.json +2 -2
- package/templates/base/src/greeter.ts +10 -9
- package/templates/react/package.json +2 -2
- package/templates/react/src/App.tsx +2 -2
- package/.eslintrc.json +0 -21
- package/LICENSE +0 -165
- package/contracts/add/add.ral +0 -16
- package/contracts/main.ral +0 -6
- package/contracts/sub/sub.ral +0 -9
- package/dist/src/signer/node-wallet.d.ts +0 -13
- package/dist/src/signer/node-wallet.js +0 -60
- package/dist/src/test/index.d.ts +0 -7
- package/dist/src/test/index.js +0 -41
- package/dist/src/test/privatekey-wallet.d.ts +0 -12
- package/dist/src/test/privatekey-wallet.js +0 -68
- package/dist/src/transaction/sign-verify.d.ts +0 -2
- package/dist/src/transaction/sign-verify.js +0 -58
- package/dist/src/utils/password-crypto.d.ts +0 -2
- package/dist/src/utils/password-crypto.js +0 -69
- package/gitignore +0 -10
- package/src/contract/ralph.test.ts +0 -178
- package/src/fixtures/address.json +0 -36
- package/src/fixtures/balance.json +0 -9
- package/src/fixtures/self-clique.json +0 -19
- package/src/fixtures/transaction.json +0 -13
- package/src/fixtures/transactions.json +0 -179
- package/src/signer/fixtures/genesis.json +0 -26
- package/src/signer/fixtures/wallets.json +0 -26
- package/src/signer/node-wallet.ts +0 -74
- package/src/test/index.ts +0 -32
- package/src/test/privatekey-wallet.ts +0 -58
- package/src/transaction/sign-verify.test.ts +0 -50
- package/src/utils/address.test.ts +0 -47
- package/src/utils/password-crypto.ts +0 -77
- package/src/utils/utils.test.ts +0 -161
- package/test/contract.test.ts +0 -178
- package/test/events.test.ts +0 -138
- package/test/transaction.test.ts +0 -72
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alephium/web3",
|
|
3
|
-
"version": "0.2.0-rc.
|
|
3
|
+
"version": "0.2.0-rc.12",
|
|
4
4
|
"description": "A JS/TS library to interact with the Alephium platform",
|
|
5
5
|
"license": "GPL",
|
|
6
6
|
"main": "dist/src/index.js",
|
|
@@ -27,29 +27,16 @@
|
|
|
27
27
|
},
|
|
28
28
|
"author": "Alephium dev <dev@alephium.org>",
|
|
29
29
|
"config": {
|
|
30
|
-
"alephium_version": "1.5.0-
|
|
30
|
+
"alephium_version": "1.5.0-rc7",
|
|
31
31
|
"explorer_backend_version": "1.7.1"
|
|
32
32
|
},
|
|
33
33
|
"scripts": {
|
|
34
34
|
"build": "rm -rf dist/* && npx tsc --build . && webpack",
|
|
35
|
-
"
|
|
35
|
+
"test": "jest -i --config ./jest-config.json",
|
|
36
36
|
"update-schemas": "npm run update-schema:alephium && npm run update-schema:explorer",
|
|
37
|
-
"update-schema:alephium": "npx swagger-typescript-api -t ./configs -o ./src/api -n api-alephium.ts -p https://raw.githubusercontent.com/alephium/alephium/v${npm_package_config_alephium_version}/api/src/main/resources/openapi.json",
|
|
38
|
-
"update-schema:explorer": "npx swagger-typescript-api -t ./configs -o ./src/api -n api-explorer.ts -p https://raw.githubusercontent.com/alephium/explorer-backend/v${npm_package_config_explorer_backend_version}/app/src/main/resources/explorer-backend-openapi.json",
|
|
37
|
+
"update-schema:alephium": "npx swagger-typescript-api --disable-throw-on-error -t ./configs -o ./src/api -n api-alephium.ts -p https://raw.githubusercontent.com/alephium/alephium/v${npm_package_config_alephium_version}/api/src/main/resources/openapi.json",
|
|
38
|
+
"update-schema:explorer": "npx swagger-typescript-api --disable-throw-on-error -t ./configs -o ./src/api -n api-explorer.ts -p https://raw.githubusercontent.com/alephium/explorer-backend/v${npm_package_config_explorer_backend_version}/app/src/main/resources/explorer-backend-openapi.json",
|
|
39
39
|
"check-versions": "node scripts/check-versions.js ${npm_package_config_alephium_version} ${npm_package_config_explorer_backend_version}",
|
|
40
|
-
"dev": "tsnd --respawn lib/index.ts",
|
|
41
|
-
"lint": "eslint . --ext ts",
|
|
42
|
-
"lint:fix": "eslint . --fix --ext ts",
|
|
43
|
-
"test": "jest -i --config ./configs/jest.config.ts",
|
|
44
|
-
"test:client": "jest -i --config ./configs/jest-client.config.ts",
|
|
45
|
-
"test:unit": "jest -i --config ./configs/jest-unit.config.ts",
|
|
46
|
-
"test:watch": "npm run test -- --watch --coverage=false",
|
|
47
|
-
"test:watch:unit": "npm run test:unit -- --watch --coverage=false",
|
|
48
|
-
"test:watch:client": "npm run test:client -- --watch --coverage=false",
|
|
49
|
-
"prepublishOnly": "npm run build",
|
|
50
|
-
"prepack": "node scripts/rename-gitignore.js .gitignore gitignore",
|
|
51
|
-
"postpack": "node scripts/rename-gitignore.js gitignore .gitignore",
|
|
52
|
-
"prettier": "prettier --write \"**/*.{js,jsx,ts,tsx,json,md}\"",
|
|
53
40
|
"start-devnet": "node scripts/start-devnet.js ${npm_package_config_alephium_version}",
|
|
54
41
|
"restart-devnet": "npm run start-devnet",
|
|
55
42
|
"stop-devnet": "node scripts/stop-devnet.js"
|
|
@@ -64,15 +51,12 @@
|
|
|
64
51
|
"buffer": "^6.0.3",
|
|
65
52
|
"commander": "^9.1.0",
|
|
66
53
|
"cross-fetch": "^3.1.5",
|
|
67
|
-
"crypto-js": "4.1.1",
|
|
68
|
-
"elliptic": "6.5.4",
|
|
69
54
|
"eventemitter3": "^4.0.7",
|
|
70
55
|
"find-up": "^2.1.0",
|
|
71
56
|
"fs-extra": "^10.0.1"
|
|
72
57
|
},
|
|
73
58
|
"devDependencies": {
|
|
74
|
-
"@
|
|
75
|
-
"@types/elliptic": "^6.4.13",
|
|
59
|
+
"@babel/eslint-parser": "^7.18.9",
|
|
76
60
|
"@types/find-up": "^2.1.0",
|
|
77
61
|
"@types/fs-extra": "^9.0.13",
|
|
78
62
|
"@types/jest": "^27.5.1",
|
|
@@ -81,14 +65,12 @@
|
|
|
81
65
|
"@types/rewire": "^2.5.28",
|
|
82
66
|
"@typescript-eslint/eslint-plugin": "^4.30.0",
|
|
83
67
|
"@typescript-eslint/parser": "^4.30.0",
|
|
84
|
-
"babel-eslint": "^10.1.0",
|
|
85
68
|
"clean-webpack-plugin": "4.0.0",
|
|
86
69
|
"crypto-browserify": "^3.12.0",
|
|
87
70
|
"eslint": "^7.32.0",
|
|
88
71
|
"eslint-config-prettier": "^8.5.0",
|
|
89
72
|
"eslint-plugin-header": "^3.1.1",
|
|
90
73
|
"eslint-plugin-prettier": "^4.0.0",
|
|
91
|
-
"eslint-plugin-react": "^7.29.4",
|
|
92
74
|
"eslint-plugin-security": "1.4.0",
|
|
93
75
|
"html-webpack-plugin": "5.5.0",
|
|
94
76
|
"jest": "^28.1.0",
|
|
@@ -98,7 +80,6 @@
|
|
|
98
80
|
"mock-socket": "^9.0.8",
|
|
99
81
|
"prettier": "^2.3.2",
|
|
100
82
|
"process": "^0.11.10",
|
|
101
|
-
"react-app-rewired": "^2.2.1",
|
|
102
83
|
"rewire": "^6.0.0",
|
|
103
84
|
"shelljs": "^0.8.5",
|
|
104
85
|
"stream-browserify": "^3.0.0",
|
|
@@ -107,21 +88,12 @@
|
|
|
107
88
|
"ts-jest": "^28.0.2",
|
|
108
89
|
"ts-node": "^10.7.0",
|
|
109
90
|
"tslib": "^2.3.1",
|
|
110
|
-
"typescript": "
|
|
91
|
+
"typescript": "4.7.4",
|
|
111
92
|
"webpack": "^5.72.0",
|
|
112
93
|
"webpack-cli": "^4.9.2"
|
|
113
94
|
},
|
|
114
95
|
"engines": {
|
|
115
|
-
"node": ">=
|
|
96
|
+
"node": ">=16.0.0",
|
|
116
97
|
"npm": ">=7.0.0"
|
|
117
|
-
},
|
|
118
|
-
"prettier": {
|
|
119
|
-
"printWidth": 120,
|
|
120
|
-
"tabWidth": 2,
|
|
121
|
-
"useTabs": false,
|
|
122
|
-
"semi": false,
|
|
123
|
-
"singleQuote": true,
|
|
124
|
-
"bracketSameLine": false,
|
|
125
|
-
"trailingComma": "none"
|
|
126
98
|
}
|
|
127
99
|
}
|
|
@@ -73,7 +73,7 @@ function prepareShared(packageRoot: string, projectRoot: string) {
|
|
|
73
73
|
console.log('...')
|
|
74
74
|
|
|
75
75
|
fsExtra.copySync(path.join(packageRoot, 'templates/shared'), projectRoot)
|
|
76
|
-
copy('', ['.editorconfig', '.eslintignore', '.gitattributes'
|
|
76
|
+
copy('', ['.editorconfig', '.eslintignore', '.gitattributes'])
|
|
77
77
|
copy('dev', ['user.conf'])
|
|
78
78
|
copy('scripts', ['start-devnet.js', 'stop-devnet.js'])
|
|
79
79
|
if (fsExtra.existsSync(path.join(packageRoot, 'gitignore'))) {
|
|
@@ -87,7 +87,8 @@ function prepareShared(packageRoot: string, projectRoot: string) {
|
|
|
87
87
|
|
|
88
88
|
function prepareBase(packageRoot: string, projectRoot: string) {
|
|
89
89
|
prepareShared(packageRoot, projectRoot)
|
|
90
|
-
copy('contracts', ['
|
|
90
|
+
copy('contracts', ['greeter_main.ral'])
|
|
91
|
+
copy('contracts/greeter', ['greeter.ral', 'greeter_interface.ral'])
|
|
91
92
|
fsExtra.copySync(path.join(packageRoot, 'templates/base'), projectRoot)
|
|
92
93
|
}
|
|
93
94
|
|
package/src/api/api-alephium.ts
CHANGED
|
@@ -422,6 +422,7 @@ export interface ChangeActiveAddress {
|
|
|
422
422
|
}
|
|
423
423
|
|
|
424
424
|
export interface CompileContractResult {
|
|
425
|
+
name: string
|
|
425
426
|
bytecode: string
|
|
426
427
|
|
|
427
428
|
/** @format 32-byte-hash */
|
|
@@ -429,12 +430,29 @@ export interface CompileContractResult {
|
|
|
429
430
|
fields: FieldsSig
|
|
430
431
|
functions: FunctionSig[]
|
|
431
432
|
events: EventSig[]
|
|
433
|
+
warnings: string[]
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
export interface CompileProjectResult {
|
|
437
|
+
contracts: CompileContractResult[]
|
|
438
|
+
scripts: CompileScriptResult[]
|
|
432
439
|
}
|
|
433
440
|
|
|
434
441
|
export interface CompileScriptResult {
|
|
442
|
+
name: string
|
|
435
443
|
bytecodeTemplate: string
|
|
436
444
|
fields: FieldsSig
|
|
437
445
|
functions: FunctionSig[]
|
|
446
|
+
warnings: string[]
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
export interface CompilerOptions {
|
|
450
|
+
ignoreUnusedConstantsWarnings?: boolean
|
|
451
|
+
ignoreUnusedVariablesWarnings?: boolean
|
|
452
|
+
ignoreUnusedFieldsWarnings?: boolean
|
|
453
|
+
ignoreUnusedPrivateFunctionsWarnings?: boolean
|
|
454
|
+
ignoreReadonlyCheckWarnings?: boolean
|
|
455
|
+
ignoreExternalCallCheckWarnings?: boolean
|
|
438
456
|
}
|
|
439
457
|
|
|
440
458
|
export interface Confirmed {
|
|
@@ -457,6 +475,7 @@ export interface Confirmed {
|
|
|
457
475
|
|
|
458
476
|
export interface Contract {
|
|
459
477
|
code: string
|
|
478
|
+
compilerOptions?: CompilerOptions
|
|
460
479
|
}
|
|
461
480
|
|
|
462
481
|
export interface ContractEvent {
|
|
@@ -561,7 +580,6 @@ export type DiscoveryAction = Reachable | Unreachable
|
|
|
561
580
|
|
|
562
581
|
export interface EventSig {
|
|
563
582
|
name: string
|
|
564
|
-
signature: string
|
|
565
583
|
fieldNames: string[]
|
|
566
584
|
fieldTypes: string[]
|
|
567
585
|
}
|
|
@@ -571,9 +589,9 @@ export interface FetchResponse {
|
|
|
571
589
|
}
|
|
572
590
|
|
|
573
591
|
export interface FieldsSig {
|
|
574
|
-
signature: string
|
|
575
592
|
names: string[]
|
|
576
593
|
types: string[]
|
|
594
|
+
isMutable: boolean[]
|
|
577
595
|
}
|
|
578
596
|
|
|
579
597
|
export interface FixedAssetOutput {
|
|
@@ -599,9 +617,12 @@ export interface FixedAssetOutput {
|
|
|
599
617
|
|
|
600
618
|
export interface FunctionSig {
|
|
601
619
|
name: string
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
620
|
+
usePreapprovedAssets: boolean
|
|
621
|
+
useAssetsInContract: boolean
|
|
622
|
+
isPublic: boolean
|
|
623
|
+
paramNames: string[]
|
|
624
|
+
paramTypes: string[]
|
|
625
|
+
paramIsMutable: boolean[]
|
|
605
626
|
returnTypes: string[]
|
|
606
627
|
}
|
|
607
628
|
|
|
@@ -703,6 +724,11 @@ export interface Penalty {
|
|
|
703
724
|
type: string
|
|
704
725
|
}
|
|
705
726
|
|
|
727
|
+
export interface Project {
|
|
728
|
+
code: string
|
|
729
|
+
compilerOptions?: CompilerOptions
|
|
730
|
+
}
|
|
731
|
+
|
|
706
732
|
export interface Reachable {
|
|
707
733
|
peers: string[]
|
|
708
734
|
type: string
|
|
@@ -729,6 +755,7 @@ export interface RevealMnemonicResult {
|
|
|
729
755
|
|
|
730
756
|
export interface Script {
|
|
731
757
|
code: string
|
|
758
|
+
compilerOptions?: CompilerOptions
|
|
732
759
|
}
|
|
733
760
|
|
|
734
761
|
export interface SelfClique {
|
|
@@ -1058,16 +1085,7 @@ export interface WalletUnlock {
|
|
|
1058
1085
|
}
|
|
1059
1086
|
|
|
1060
1087
|
import 'cross-fetch/polyfill'
|
|
1061
|
-
|
|
1062
|
-
function convertHttpResponse<T>(
|
|
1063
|
-
response: HttpResponse<T, { detail: string }> | HttpResponse<T, { detail: string }>
|
|
1064
|
-
): T {
|
|
1065
|
-
if (response.error) {
|
|
1066
|
-
throw new Error(response.error.detail)
|
|
1067
|
-
} else {
|
|
1068
|
-
return response.data
|
|
1069
|
-
}
|
|
1070
|
-
}
|
|
1088
|
+
import { convertHttpResponse } from './utils'
|
|
1071
1089
|
|
|
1072
1090
|
export type QueryParamsType = Record<string | number, any>
|
|
1073
1091
|
export type ResponseFormat = keyof Omit<Body, 'body' | 'bodyUsed'>
|
|
@@ -1271,7 +1289,6 @@ export class HttpClient<SecurityDataType = unknown> {
|
|
|
1271
1289
|
this.abortControllers.delete(cancelToken)
|
|
1272
1290
|
}
|
|
1273
1291
|
|
|
1274
|
-
if (!response.ok) throw data
|
|
1275
1292
|
return data
|
|
1276
1293
|
})
|
|
1277
1294
|
}
|
|
@@ -2160,6 +2177,27 @@ export class Api<SecurityDataType extends unknown> extends HttpClient<SecurityDa
|
|
|
2160
2177
|
...params
|
|
2161
2178
|
}).then(convertHttpResponse),
|
|
2162
2179
|
|
|
2180
|
+
/**
|
|
2181
|
+
* No description
|
|
2182
|
+
*
|
|
2183
|
+
* @tags Contracts
|
|
2184
|
+
* @name PostContractsCompileProject
|
|
2185
|
+
* @summary Compile a project
|
|
2186
|
+
* @request POST:/contracts/compile-project
|
|
2187
|
+
*/
|
|
2188
|
+
postContractsCompileProject: (data: Project, params: RequestParams = {}) =>
|
|
2189
|
+
this.request<
|
|
2190
|
+
CompileProjectResult,
|
|
2191
|
+
BadRequest | Unauthorized | NotFound | InternalServerError | ServiceUnavailable
|
|
2192
|
+
>({
|
|
2193
|
+
path: `/contracts/compile-project`,
|
|
2194
|
+
method: 'POST',
|
|
2195
|
+
body: data,
|
|
2196
|
+
type: ContentType.Json,
|
|
2197
|
+
format: 'json',
|
|
2198
|
+
...params
|
|
2199
|
+
}).then(convertHttpResponse),
|
|
2200
|
+
|
|
2163
2201
|
/**
|
|
2164
2202
|
* No description
|
|
2165
2203
|
*
|
package/src/api/api-explorer.ts
CHANGED
|
@@ -278,16 +278,7 @@ export interface UnconfirmedTransaction {
|
|
|
278
278
|
}
|
|
279
279
|
|
|
280
280
|
import 'cross-fetch/polyfill'
|
|
281
|
-
|
|
282
|
-
function convertHttpResponse<T>(
|
|
283
|
-
response: HttpResponse<T, { detail: string }> | HttpResponse<T, { detail: string }>
|
|
284
|
-
): T {
|
|
285
|
-
if (response.error) {
|
|
286
|
-
throw new Error(response.error.detail)
|
|
287
|
-
} else {
|
|
288
|
-
return response.data
|
|
289
|
-
}
|
|
290
|
-
}
|
|
281
|
+
import { convertHttpResponse } from './utils'
|
|
291
282
|
|
|
292
283
|
export type QueryParamsType = Record<string | number, any>
|
|
293
284
|
export type ResponseFormat = keyof Omit<Body, 'body' | 'bodyUsed'>
|
|
@@ -491,7 +482,6 @@ export class HttpClient<SecurityDataType = unknown> {
|
|
|
491
482
|
this.abortControllers.delete(cancelToken)
|
|
492
483
|
}
|
|
493
484
|
|
|
494
|
-
if (!response.ok) throw data
|
|
495
485
|
return data
|
|
496
486
|
})
|
|
497
487
|
}
|
package/src/api/index.ts
CHANGED
|
@@ -19,9 +19,19 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
19
19
|
import { Api as NodeApi } from './api-alephium'
|
|
20
20
|
import { Api as ExplorerApi } from './api-explorer'
|
|
21
21
|
|
|
22
|
-
export class NodeProvider extends NodeApi<
|
|
23
|
-
constructor(baseUrl: string) {
|
|
24
|
-
|
|
22
|
+
export class NodeProvider extends NodeApi<string> {
|
|
23
|
+
constructor(baseUrl: string, apiKey?: string) {
|
|
24
|
+
// eslint-disable-next-line security/detect-possible-timing-attacks
|
|
25
|
+
if (apiKey === undefined) {
|
|
26
|
+
super({ baseUrl: baseUrl })
|
|
27
|
+
} else {
|
|
28
|
+
super({
|
|
29
|
+
baseUrl: baseUrl,
|
|
30
|
+
baseApiParams: { secure: true },
|
|
31
|
+
securityWorker: (accessToken) => (accessToken !== null ? { headers: { 'X-API-KEY': `${accessToken}` } } : {})
|
|
32
|
+
})
|
|
33
|
+
this.setSecurityData(apiKey)
|
|
34
|
+
}
|
|
25
35
|
}
|
|
26
36
|
}
|
|
27
37
|
|
|
@@ -33,3 +43,4 @@ export class ExplorerProvider extends ExplorerApi<null> {
|
|
|
33
43
|
|
|
34
44
|
export * as node from './api-alephium'
|
|
35
45
|
export * as explorer from './api-explorer'
|
|
46
|
+
export * from './types'
|
package/src/api/types.ts
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2018 - 2022 The Alephium Authors
|
|
3
|
+
This file is part of the alephium project.
|
|
4
|
+
|
|
5
|
+
The library is free software: you can redistribute it and/or modify
|
|
6
|
+
it under the terms of the GNU Lesser General Public License as published by
|
|
7
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
8
|
+
(at your option) any later version.
|
|
9
|
+
|
|
10
|
+
The library is distributed in the hope that it will be useful,
|
|
11
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13
|
+
GNU Lesser General Public License for more details.
|
|
14
|
+
|
|
15
|
+
You should have received a copy of the GNU Lesser General Public License
|
|
16
|
+
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { assertType, bs58, Eq } from '../utils'
|
|
20
|
+
import * as node from './api-alephium'
|
|
21
|
+
|
|
22
|
+
export type Number256 = number | bigint | string
|
|
23
|
+
export type Val = Number256 | boolean | string | Val[]
|
|
24
|
+
export type NamedVals = Record<string, Val>
|
|
25
|
+
|
|
26
|
+
export interface Token {
|
|
27
|
+
id: string
|
|
28
|
+
amount: Number256
|
|
29
|
+
}
|
|
30
|
+
assertType<Eq<keyof Token, keyof node.Token>>
|
|
31
|
+
|
|
32
|
+
export function toApiToken(token: Token): node.Token {
|
|
33
|
+
return { id: token.id, amount: toApiNumber256(token.amount) }
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function toApiTokens(tokens?: Token[]): node.Token[] | undefined {
|
|
37
|
+
return tokens?.map(toApiToken)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function fromApiToken(token: node.Token): Token {
|
|
41
|
+
return { id: token.id, amount: fromApiNumber256(token.amount) }
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function fromApiTokens(tokens?: node.Token[]): Token[] | undefined {
|
|
45
|
+
return tokens?.map(fromApiToken)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function toApiBoolean(v: Val): boolean {
|
|
49
|
+
if (typeof v === 'boolean') {
|
|
50
|
+
return v
|
|
51
|
+
} else {
|
|
52
|
+
throw new Error(`Invalid boolean value: ${v}`)
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// TODO: check integer bounds
|
|
57
|
+
export function toApiNumber256(v: Val): string {
|
|
58
|
+
if ((typeof v === 'number' && Number.isInteger(v)) || typeof v === 'bigint') {
|
|
59
|
+
return v.toString()
|
|
60
|
+
} else if (typeof v === 'string') {
|
|
61
|
+
return v
|
|
62
|
+
} else {
|
|
63
|
+
throw new Error(`Invalid 256 bit number: ${v}`)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function toApiNumber256Optional(v?: Val): string | undefined {
|
|
68
|
+
return v === undefined ? undefined : toApiNumber256(v)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function fromApiNumber256(n: string): Number256 {
|
|
72
|
+
if (Number.isSafeInteger(Number.parseInt(n))) {
|
|
73
|
+
return Number(n)
|
|
74
|
+
} else {
|
|
75
|
+
return BigInt(n)
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// TODO: check hex string
|
|
80
|
+
export function toApiByteVec(v: Val): string {
|
|
81
|
+
if (typeof v === 'string') {
|
|
82
|
+
// try to convert from address to contract id
|
|
83
|
+
try {
|
|
84
|
+
const address = bs58.decode(v)
|
|
85
|
+
if (address.length == 33 && address[0] == 3) {
|
|
86
|
+
return Buffer.from(address.slice(1)).toString('hex')
|
|
87
|
+
}
|
|
88
|
+
} catch (_) {
|
|
89
|
+
return v as string
|
|
90
|
+
}
|
|
91
|
+
return v as string
|
|
92
|
+
} else {
|
|
93
|
+
throw new Error(`Invalid string: ${v}`)
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function toApiAddress(v: Val): string {
|
|
98
|
+
if (typeof v === 'string') {
|
|
99
|
+
try {
|
|
100
|
+
bs58.decode(v)
|
|
101
|
+
return v as string
|
|
102
|
+
} catch (error) {
|
|
103
|
+
throw new Error(`Invalid base58 string: ${v}`)
|
|
104
|
+
}
|
|
105
|
+
} else {
|
|
106
|
+
throw new Error(`Invalid string: ${v}`)
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function toApiArray(tpe: string, v: Val): node.Val {
|
|
111
|
+
if (!Array.isArray(v)) {
|
|
112
|
+
throw new Error(`Expected array, got ${v}`)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const semiColonIndex = tpe.lastIndexOf(';')
|
|
116
|
+
if (semiColonIndex == -1) {
|
|
117
|
+
throw new Error(`Invalid Val type: ${tpe}`)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const subType = tpe.slice(1, semiColonIndex)
|
|
121
|
+
const dim = parseInt(tpe.slice(semiColonIndex + 1, -1))
|
|
122
|
+
if ((v as Val[]).length != dim) {
|
|
123
|
+
throw new Error(`Invalid val dimension: ${v}`)
|
|
124
|
+
} else {
|
|
125
|
+
return { value: (v as Val[]).map((v) => toApiVal(v, subType)), type: 'Array' }
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export function toApiVal(v: Val, tpe: string): node.Val {
|
|
130
|
+
if (tpe === 'Bool') {
|
|
131
|
+
return { value: toApiBoolean(v), type: tpe }
|
|
132
|
+
} else if (tpe === 'U256' || tpe === 'I256') {
|
|
133
|
+
return { value: toApiNumber256(v), type: tpe }
|
|
134
|
+
} else if (tpe === 'ByteVec') {
|
|
135
|
+
return { value: toApiByteVec(v), type: tpe }
|
|
136
|
+
} else if (tpe === 'Address') {
|
|
137
|
+
return { value: toApiAddress(v), type: tpe }
|
|
138
|
+
} else {
|
|
139
|
+
return toApiArray(tpe, v)
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function _fromApiVal(vals: node.Val[], valIndex: number, tpe: string): [result: Val, nextIndex: number] {
|
|
144
|
+
if (vals.length === 0) {
|
|
145
|
+
throw new Error('Not enough Vals')
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const firstVal = vals[`${valIndex}`]
|
|
149
|
+
if (tpe === 'Bool' && firstVal.type === tpe) {
|
|
150
|
+
return [firstVal.value as boolean, valIndex + 1]
|
|
151
|
+
} else if ((tpe === 'U256' || tpe === 'I256') && firstVal.type === tpe) {
|
|
152
|
+
return [fromApiNumber256(firstVal.value as string), valIndex + 1]
|
|
153
|
+
} else if ((tpe === 'ByteVec' || tpe === 'Address') && firstVal.type === tpe) {
|
|
154
|
+
return [firstVal.value as string, valIndex + 1]
|
|
155
|
+
} else {
|
|
156
|
+
const [baseType, dims] = decodeArrayType(tpe)
|
|
157
|
+
const arraySize = dims.reduce((a, b) => a * b)
|
|
158
|
+
const nextIndex = valIndex + arraySize
|
|
159
|
+
const valsToUse = vals.slice(valIndex, nextIndex)
|
|
160
|
+
if (valsToUse.length == arraySize && valsToUse.every((val) => val.type === baseType)) {
|
|
161
|
+
const localVals = valsToUse.map((val) => fromApiVal(val, baseType))
|
|
162
|
+
return [foldVals(localVals, dims), nextIndex]
|
|
163
|
+
} else {
|
|
164
|
+
throw new Error(`Invalid array Val type: ${valsToUse}, ${tpe}`)
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export function fromApiVals(vals: node.Val[], names: string[], types: string[]): NamedVals {
|
|
170
|
+
let valIndex = 0
|
|
171
|
+
const result: NamedVals = {}
|
|
172
|
+
types.forEach((currentType, index) => {
|
|
173
|
+
const currentName = names[`${index}`]
|
|
174
|
+
const [val, nextIndex] = _fromApiVal(vals, valIndex, currentType)
|
|
175
|
+
valIndex = nextIndex
|
|
176
|
+
result[`${currentName}`] = val
|
|
177
|
+
})
|
|
178
|
+
return result
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export function fromApiArray(vals: node.Val[], types: string[]): Val[] {
|
|
182
|
+
let valIndex = 0
|
|
183
|
+
const result: Val[] = []
|
|
184
|
+
for (const currentType of types) {
|
|
185
|
+
const [val, nextIndex] = _fromApiVal(vals, valIndex, currentType)
|
|
186
|
+
result.push(val)
|
|
187
|
+
valIndex = nextIndex
|
|
188
|
+
}
|
|
189
|
+
return result
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export function fromApiVal(v: node.Val, tpe: string): Val {
|
|
193
|
+
if (v.type === 'Bool' && v.type === tpe) {
|
|
194
|
+
return v.value as boolean
|
|
195
|
+
} else if ((v.type === 'U256' || v.type === 'I256') && v.type === tpe) {
|
|
196
|
+
return fromApiNumber256(v.value as string)
|
|
197
|
+
} else if ((v.type === 'ByteVec' || v.type === 'Address') && v.type === tpe) {
|
|
198
|
+
return v.value as string
|
|
199
|
+
} else {
|
|
200
|
+
throw new Error(`Invalid node.Val type: ${v}`)
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function decodeArrayType(tpe: string): [baseType: string, dims: number[]] {
|
|
205
|
+
const semiColonIndex = tpe.lastIndexOf(';')
|
|
206
|
+
if (semiColonIndex === -1) {
|
|
207
|
+
throw new Error(`Invalid Val type: ${tpe}`)
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const subType = tpe.slice(1, semiColonIndex)
|
|
211
|
+
const dim = parseInt(tpe.slice(semiColonIndex + 1, -1))
|
|
212
|
+
if (subType[0] == '[') {
|
|
213
|
+
const [baseType, subDim] = decodeArrayType(subType)
|
|
214
|
+
return [baseType, (subDim.unshift(dim), subDim)]
|
|
215
|
+
} else {
|
|
216
|
+
return [subType, [dim]]
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function foldVals(vals: Val[], dims: number[]): Val {
|
|
221
|
+
if (dims.length == 1) {
|
|
222
|
+
return vals
|
|
223
|
+
} else {
|
|
224
|
+
const result: Val[] = []
|
|
225
|
+
const chunkSize = vals.length / dims[0]
|
|
226
|
+
const chunkDims = dims.slice(1)
|
|
227
|
+
for (let i = 0; i < vals.length; i += chunkSize) {
|
|
228
|
+
const chunk = vals.slice(i, i + chunkSize)
|
|
229
|
+
result.push(foldVals(chunk, chunkDims))
|
|
230
|
+
}
|
|
231
|
+
return result
|
|
232
|
+
}
|
|
233
|
+
}
|
|
@@ -16,12 +16,10 @@ You should have received a copy of the GNU Lesser General Public License
|
|
|
16
16
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
})
|
|
27
|
-
})
|
|
19
|
+
export function convertHttpResponse<T>(response: { data: T; error?: { detail: string } }): T {
|
|
20
|
+
if (response.error) {
|
|
21
|
+
throw new Error(`[Node API Error] - ${response.error.detail}`)
|
|
22
|
+
} else {
|
|
23
|
+
return response.data
|
|
24
|
+
}
|
|
25
|
+
}
|