@alephium/web3 0.0.3 → 0.1.0-rc.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.
Files changed (144) hide show
  1. package/.editorconfig +13 -0
  2. package/.eslintignore +2 -0
  3. package/.eslintrc.json +21 -0
  4. package/README.md +2 -2
  5. package/contracts/add.ral +7 -3
  6. package/contracts/greeter.ral +3 -1
  7. package/contracts/greeter_interface.ral +3 -0
  8. package/contracts/greeter_main.ral +9 -0
  9. package/contracts/main.ral +3 -5
  10. package/dev/user.conf +8 -4
  11. package/dist/alephium-web3.min.js +1 -1
  12. package/dist/alephium-web3.min.js.map +1 -1
  13. package/dist/scripts/check-versions.d.ts +1 -0
  14. package/dist/scripts/check-versions.js +39 -0
  15. package/dist/{cli → scripts}/create-project.d.ts +0 -0
  16. package/dist/scripts/create-project.js +124 -0
  17. package/dist/scripts/header.d.ts +0 -0
  18. package/dist/scripts/header.js +18 -0
  19. package/dist/scripts/rename-gitignore.d.ts +1 -0
  20. package/dist/scripts/rename-gitignore.js +24 -0
  21. package/dist/scripts/start-devnet.d.ts +1 -0
  22. package/dist/scripts/start-devnet.js +131 -0
  23. package/dist/scripts/stop-devnet.d.ts +1 -0
  24. package/dist/scripts/stop-devnet.js +32 -0
  25. package/dist/{api → src/api}/api-alephium.d.ts +287 -168
  26. package/dist/{api → src/api}/api-alephium.js +497 -115
  27. package/dist/{api → src/api}/api-explorer.d.ts +117 -19
  28. package/dist/{api → src/api}/api-explorer.js +206 -46
  29. package/dist/src/api/index.d.ts +10 -0
  30. package/dist/src/api/index.js +55 -0
  31. package/dist/{lib → src}/constants.d.ts +0 -0
  32. package/dist/{lib → src}/constants.js +0 -0
  33. package/dist/src/contract/contract.d.ts +182 -0
  34. package/dist/src/contract/contract.js +760 -0
  35. package/dist/src/contract/events.d.ts +29 -0
  36. package/dist/src/contract/events.js +77 -0
  37. package/dist/src/contract/index.d.ts +3 -0
  38. package/dist/src/contract/index.js +32 -0
  39. package/dist/src/contract/ralph.d.ts +12 -0
  40. package/dist/src/contract/ralph.js +362 -0
  41. package/dist/src/index.d.ts +5 -0
  42. package/dist/src/index.js +34 -0
  43. package/dist/src/signer/index.d.ts +2 -0
  44. package/dist/src/signer/index.js +31 -0
  45. package/dist/src/signer/node-wallet.d.ts +13 -0
  46. package/dist/src/signer/node-wallet.js +60 -0
  47. package/dist/src/signer/signer.d.ts +143 -0
  48. package/dist/src/signer/signer.js +184 -0
  49. package/dist/src/test/index.d.ts +7 -0
  50. package/dist/src/test/index.js +41 -0
  51. package/dist/src/test/privatekey-wallet.d.ts +12 -0
  52. package/dist/src/test/privatekey-wallet.js +68 -0
  53. package/dist/{lib → src/utils}/address.d.ts +0 -0
  54. package/dist/{lib → src/utils}/address.js +1 -1
  55. package/dist/{lib → src/utils}/bs58.d.ts +2 -2
  56. package/dist/{lib → src/utils}/bs58.js +3 -1
  57. package/dist/{lib → src/utils}/djb2.d.ts +0 -0
  58. package/dist/{lib → src/utils}/djb2.js +1 -1
  59. package/dist/src/utils/index.d.ts +6 -0
  60. package/dist/src/utils/index.js +35 -0
  61. package/dist/{lib → src/utils}/password-crypto.d.ts +0 -0
  62. package/dist/{lib → src/utils}/password-crypto.js +8 -7
  63. package/dist/src/utils/transaction.d.ts +2 -0
  64. package/dist/src/utils/transaction.js +58 -0
  65. package/dist/src/utils/utils.d.ts +30 -0
  66. package/dist/{lib → src/utils}/utils.js +83 -19
  67. package/gitignore +5 -4
  68. package/package.json +56 -40
  69. package/scripts/create-project.ts +136 -0
  70. package/scripts/header.js +17 -0
  71. package/scripts/start-devnet.js +3 -3
  72. package/src/api/api-alephium.ts +2323 -0
  73. package/src/api/api-explorer.ts +808 -0
  74. package/src/api/index.ts +35 -0
  75. package/src/constants.ts +20 -0
  76. package/src/contract/contract.ts +1014 -0
  77. package/src/contract/events.ts +102 -0
  78. package/src/contract/index.ts +21 -0
  79. package/src/contract/ralph.test.ts +178 -0
  80. package/src/contract/ralph.ts +362 -0
  81. package/src/fixtures/address.json +36 -0
  82. package/src/fixtures/balance.json +9 -0
  83. package/src/fixtures/self-clique.json +19 -0
  84. package/src/fixtures/transaction.json +13 -0
  85. package/src/fixtures/transactions.json +179 -0
  86. package/src/index.ts +24 -0
  87. package/src/signer/fixtures/genesis.json +26 -0
  88. package/src/signer/fixtures/wallets.json +26 -0
  89. package/src/signer/index.ts +20 -0
  90. package/src/signer/node-wallet.ts +74 -0
  91. package/src/signer/signer.ts +313 -0
  92. package/src/test/index.ts +32 -0
  93. package/src/test/privatekey-wallet.ts +58 -0
  94. package/src/utils/address.test.ts +47 -0
  95. package/src/utils/address.ts +39 -0
  96. package/src/utils/bs58.ts +26 -0
  97. package/src/utils/djb2.test.ts +35 -0
  98. package/src/utils/djb2.ts +25 -0
  99. package/src/utils/index.ts +24 -0
  100. package/src/utils/password-crypto.test.ts +27 -0
  101. package/src/utils/password-crypto.ts +77 -0
  102. package/src/utils/transaction.test.ts +50 -0
  103. package/src/utils/transaction.ts +39 -0
  104. package/src/utils/utils.test.ts +160 -0
  105. package/src/utils/utils.ts +209 -0
  106. package/templates/{README.md → base/README.md} +4 -4
  107. package/templates/base/package.json +35 -0
  108. package/templates/base/src/greeter.ts +41 -0
  109. package/templates/base/tsconfig.json +19 -0
  110. package/templates/react/README.md +34 -0
  111. package/templates/react/config-overrides.js +18 -0
  112. package/templates/react/package.json +66 -0
  113. package/templates/react/src/App.tsx +42 -0
  114. package/templates/react/src/artifacts/greeter.ral.json +26 -0
  115. package/templates/react/src/artifacts/greeter_main.ral.json +22 -0
  116. package/templates/shared/.eslintrc.json +12 -0
  117. package/templates/shared/scripts/header.js +0 -0
  118. package/test/contract.test.ts +161 -0
  119. package/test/events.test.ts +139 -0
  120. package/webpack.config.js +1 -1
  121. package/contracts/greeter-main.ral +0 -8
  122. package/dist/cli/create-project.js +0 -87
  123. package/dist/lib/clique.d.ts +0 -23
  124. package/dist/lib/clique.js +0 -149
  125. package/dist/lib/contract.d.ts +0 -152
  126. package/dist/lib/contract.js +0 -711
  127. package/dist/lib/explorer.d.ts +0 -8
  128. package/dist/lib/explorer.js +0 -46
  129. package/dist/lib/index.d.ts +0 -12
  130. package/dist/lib/index.js +0 -60
  131. package/dist/lib/node.d.ts +0 -10
  132. package/dist/lib/node.js +0 -64
  133. package/dist/lib/numbers.d.ts +0 -7
  134. package/dist/lib/numbers.js +0 -128
  135. package/dist/lib/signer.d.ts +0 -17
  136. package/dist/lib/signer.js +0 -70
  137. package/dist/lib/storage-browser.d.ts +0 -9
  138. package/dist/lib/storage-browser.js +0 -52
  139. package/dist/lib/storage-node.d.ts +0 -9
  140. package/dist/lib/storage-node.js +0 -65
  141. package/dist/lib/utils.d.ts +0 -11
  142. package/dist/lib/wallet.d.ts +0 -30
  143. package/dist/lib/wallet.js +0 -142
  144. package/templates/package.json +0 -29
@@ -0,0 +1,30 @@
1
+ import { ec as EC, SignatureInput } from 'elliptic';
2
+ import * as node from '../api/api-alephium';
3
+ import * as explorer from '../api/api-explorer';
4
+ export declare function convertHttpResponse<T>(response: node.HttpResponse<T, {
5
+ detail: string;
6
+ }> | explorer.HttpResponse<T, {
7
+ detail: string;
8
+ }>): T;
9
+ export declare function signatureEncode(signature: EC.Signature): string;
10
+ export declare const signatureDecode: (ec: EC, signature: string) => SignatureInput;
11
+ export declare function isHexString(input: string): boolean;
12
+ export declare const groupOfAddress: (address: string) => number;
13
+ export declare function contractIdFromAddress(address: string): Uint8Array;
14
+ export declare function tokenIdFromAddress(address: string): Uint8Array;
15
+ export declare function hexToBinUnsafe(hex: string): Uint8Array;
16
+ export declare function binToHex(bin: Uint8Array): string;
17
+ export declare function publicKeyFromPrivateKey(privateKey: string): string;
18
+ export declare function addressFromPublicKey(publicKey: string): string;
19
+ export declare function addressFromContractId(contractId: string): string;
20
+ export declare function contractIdFromTx(txId: string, outputIndex: number): string;
21
+ export declare function subContractId(parentContractId: string, pathInHex: string): string;
22
+ export declare function stringToHex(str: string): string;
23
+ declare type _Eq<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;
24
+ export declare type Eq<X, Y> = _Eq<{
25
+ [P in keyof X]: X[P];
26
+ }, {
27
+ [P in keyof Y]: Y[P];
28
+ }>;
29
+ export declare function assertType<T extends true>(): void;
30
+ export {};
@@ -20,25 +20,33 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
20
20
  return (mod && mod.__esModule) ? mod : { "default": mod };
21
21
  };
22
22
  Object.defineProperty(exports, "__esModule", { value: true });
23
- exports.tokenIdFromAddress = exports.groupOfAddress = exports.getStorage = exports.signatureDecode = exports.signatureEncode = void 0;
23
+ exports.assertType = exports.stringToHex = exports.subContractId = exports.contractIdFromTx = exports.addressFromContractId = exports.addressFromPublicKey = exports.publicKeyFromPrivateKey = exports.binToHex = exports.hexToBinUnsafe = exports.tokenIdFromAddress = exports.contractIdFromAddress = exports.groupOfAddress = exports.isHexString = exports.signatureDecode = exports.signatureEncode = exports.convertHttpResponse = void 0;
24
+ const elliptic_1 = require("elliptic");
24
25
  const bn_js_1 = __importDefault(require("bn.js"));
26
+ const blakejs_1 = __importDefault(require("blakejs"));
25
27
  const bs58_1 = __importDefault(require("./bs58"));
26
- const storage_node_1 = __importDefault(require("./storage-node"));
27
- const storage_browser_1 = __importDefault(require("./storage-browser"));
28
- const constants_1 = require("./constants");
28
+ const buffer_1 = require("buffer/");
29
+ const constants_1 = require("../constants");
29
30
  const djb2_1 = __importDefault(require("./djb2"));
30
- const signatureEncode = (ec, signature) => {
31
+ const ec = new elliptic_1.ec('secp256k1');
32
+ function convertHttpResponse(response) {
33
+ if (response.error) {
34
+ throw new Error(response.error.detail);
35
+ }
36
+ else {
37
+ return response.data;
38
+ }
39
+ }
40
+ exports.convertHttpResponse = convertHttpResponse;
41
+ function signatureEncode(signature) {
31
42
  let sNormalized = signature.s;
32
43
  if (ec.n && signature.s.cmp(ec.nh) === 1) {
33
44
  sNormalized = ec.n.sub(signature.s);
34
45
  }
35
- const r = signature.r.toArrayLike(Buffer, 'be', 33).slice(1);
36
- const s = sNormalized.toArrayLike(Buffer, 'be', 33).slice(1);
37
- const xs = new Uint8Array(r.byteLength + s.byteLength);
38
- xs.set(new Uint8Array(r), 0);
39
- xs.set(new Uint8Array(s), r.byteLength);
40
- return Buffer.from(xs).toString('hex');
41
- };
46
+ const r = signature.r.toString('hex', 66).slice(2);
47
+ const s = sNormalized.toString('hex', 66).slice(2);
48
+ return r + s;
49
+ }
42
50
  exports.signatureEncode = signatureEncode;
43
51
  // the signature should be in hex string format for 64 bytes
44
52
  const signatureDecode = (ec, signature) => {
@@ -56,11 +64,6 @@ const signatureDecode = (ec, signature) => {
56
64
  }
57
65
  };
58
66
  exports.signatureDecode = signatureDecode;
59
- const getStorage = () => {
60
- const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
61
- return isBrowser ? new storage_browser_1.default() : new storage_node_1.default();
62
- };
63
- exports.getStorage = getStorage;
64
67
  const xorByte = (intValue) => {
65
68
  const byte0 = (intValue >> 24) & 0xff;
66
69
  const byte1 = (intValue >> 16) & 0xff;
@@ -68,6 +71,10 @@ const xorByte = (intValue) => {
68
71
  const byte3 = intValue & 0xff;
69
72
  return (byte0 ^ byte1 ^ byte2 ^ byte3) & 0xff;
70
73
  };
74
+ function isHexString(input) {
75
+ return input.length % 2 === 0 && /[0-9a-f]*$/.test(input);
76
+ }
77
+ exports.isHexString = isHexString;
71
78
  var AddressType;
72
79
  (function (AddressType) {
73
80
  AddressType[AddressType["P2PKH"] = 0] = "P2PKH";
@@ -119,17 +126,74 @@ const groupOfP2mpkhAddress = (address) => {
119
126
  const groupOfP2shAddress = (address) => {
120
127
  return groupOfAddressBytes(address);
121
128
  };
129
+ function contractIdFromAddress(address) {
130
+ return idFromAddress(address);
131
+ }
132
+ exports.contractIdFromAddress = contractIdFromAddress;
122
133
  function tokenIdFromAddress(address) {
134
+ return idFromAddress(address);
135
+ }
136
+ exports.tokenIdFromAddress = tokenIdFromAddress;
137
+ function idFromAddress(address) {
123
138
  const decoded = bs58_1.default.decode(address);
124
139
  if (decoded.length == 0)
125
140
  throw new Error('Address string is empty');
126
141
  const addressType = decoded[0];
127
142
  const addressBody = decoded.slice(1);
128
143
  if (addressType == AddressType.P2C) {
129
- return Buffer.from(addressBody).toString('hex');
144
+ return addressBody;
130
145
  }
131
146
  else {
132
147
  throw new Error(`Invalid contract address type: ${addressType}`);
133
148
  }
134
149
  }
135
- exports.tokenIdFromAddress = tokenIdFromAddress;
150
+ function hexToBinUnsafe(hex) {
151
+ return buffer_1.Buffer.from(hex, 'hex');
152
+ }
153
+ exports.hexToBinUnsafe = hexToBinUnsafe;
154
+ function binToHex(bin) {
155
+ return buffer_1.Buffer.from(bin).toString('hex');
156
+ }
157
+ exports.binToHex = binToHex;
158
+ function publicKeyFromPrivateKey(privateKey) {
159
+ const key = ec.keyFromPrivate(privateKey);
160
+ return key.getPublic(true, 'hex');
161
+ }
162
+ exports.publicKeyFromPrivateKey = publicKeyFromPrivateKey;
163
+ function addressFromPublicKey(publicKey) {
164
+ const addressType = buffer_1.Buffer.from([AddressType.P2PKH]);
165
+ const hash = buffer_1.Buffer.from(blakejs_1.default.blake2b(buffer_1.Buffer.from(publicKey, 'hex'), undefined, 32));
166
+ const bytes = buffer_1.Buffer.concat([addressType, hash]);
167
+ return bs58_1.default.encode(bytes);
168
+ }
169
+ exports.addressFromPublicKey = addressFromPublicKey;
170
+ function addressFromContractId(contractId) {
171
+ const addressType = buffer_1.Buffer.from([AddressType.P2C]);
172
+ const hash = buffer_1.Buffer.from(hexToBinUnsafe(contractId));
173
+ const bytes = buffer_1.Buffer.concat([addressType, hash]);
174
+ return bs58_1.default.encode(bytes);
175
+ }
176
+ exports.addressFromContractId = addressFromContractId;
177
+ function contractIdFromTx(txId, outputIndex) {
178
+ const txIdBin = hexToBinUnsafe(txId);
179
+ const data = buffer_1.Buffer.concat([txIdBin, buffer_1.Buffer.from([outputIndex])]);
180
+ const hash = blakejs_1.default.blake2b(data, undefined, 32);
181
+ return binToHex(hash);
182
+ }
183
+ exports.contractIdFromTx = contractIdFromTx;
184
+ function subContractId(parentContractId, pathInHex) {
185
+ const data = buffer_1.Buffer.concat([hexToBinUnsafe(pathInHex), hexToBinUnsafe(parentContractId)]);
186
+ return binToHex(blakejs_1.default.blake2b(blakejs_1.default.blake2b(data, undefined, 32), undefined, 32));
187
+ }
188
+ exports.subContractId = subContractId;
189
+ function stringToHex(str) {
190
+ let hex = '';
191
+ for (let i = 0; i < str.length; i++) {
192
+ hex += '' + str.charCodeAt(i).toString(16);
193
+ }
194
+ return hex;
195
+ }
196
+ exports.stringToHex = stringToHex;
197
+ // eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/no-unused-vars
198
+ function assertType() { }
199
+ exports.assertType = assertType;
package/gitignore CHANGED
@@ -1,9 +1,10 @@
1
1
  node_modules/
2
2
  dist/
3
+ build/
4
+ web/
3
5
  .vscode
4
6
  .DS_Store
5
7
  coverage/
6
- dev/logs
7
- dev/network*
8
- dev/alephium*
9
- artifacts/
8
+ /dev/
9
+ !dev/user.conf
10
+ /artifacts/
package/package.json CHANGED
@@ -1,27 +1,22 @@
1
1
  {
2
2
  "name": "@alephium/web3",
3
- "version": "0.0.3",
3
+ "version": "0.1.0-rc.0",
4
4
  "description": "A JS/TS library to interact with the Alephium platform",
5
5
  "license": "GPL",
6
- "main": "dist/lib/index.js",
6
+ "main": "dist/src/index.js",
7
7
  "browser": "dist/alephium-web3.min.js",
8
- "types": "dist/lib/index.d.ts",
8
+ "types": "dist/src/index.d.ts",
9
9
  "exports": {
10
- ".": "./dist/lib/index.js",
11
- "./api/alephium": "./dist/api/api-alephium.js",
12
- "./api/explorer": "./dist/api/api-explorer.js"
10
+ ".": "./dist/src/index.js",
11
+ "./test": "./dist/src/test/index.js"
13
12
  },
14
13
  "typesVersions": {
15
14
  "*": {
16
- "api/alephium": [
17
- "dist/api/api-alephium"
18
- ],
19
- "api/explorer": [
20
- "dist/api/api-explorer"
15
+ "test": [
16
+ "dist/src/test/"
21
17
  ]
22
18
  }
23
19
  },
24
- "type": "commonjs",
25
20
  "repository": {
26
21
  "type": "git",
27
22
  "url": "git@github.com:alephium/alephium-web3.git"
@@ -32,42 +27,47 @@
32
27
  },
33
28
  "author": "Alephium dev <dev@alephium.org>",
34
29
  "config": {
35
- "alephium_version": "1.3.0-rc9",
36
- "explorer_backend_version": "1.6.0-rc8"
30
+ "alephium_version": "1.4.0-rc2",
31
+ "explorer_backend_version": "1.7.0-leman1"
37
32
  },
38
33
  "scripts": {
39
- "build": "rm -rf dist && npx tsc --build . && webpack",
34
+ "build": "rm -rf dist/* && npx tsc --build . && webpack",
35
+ "bundle": "webpack",
40
36
  "update-schemas": "npm run update-schema:alephium && npm run update-schema:explorer",
41
- "update-schema:alephium": "npx swagger-typescript-api -t ./api -o ./api -n api-alephium.ts -p https://raw.githubusercontent.com/alephium/alephium/v${npm_package_config_alephium_version}/api/src/main/resources/openapi.json",
42
- "update-schema:explorer": "npx swagger-typescript-api -t ./api -o ./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 -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",
43
39
  "check-versions": "node scripts/check-versions.js ${npm_package_config_alephium_version} ${npm_package_config_explorer_backend_version}",
44
40
  "dev": "tsnd --respawn lib/index.ts",
45
- "lint": "eslint . --ext .ts",
46
- "lint:fix": "eslint . --fix --ext .ts",
47
- "jest": "jest --useStderr --silent=false --verbose=true --config jestconfig.json",
48
- "test": "npm run build && npm run jest",
49
- "test:watch": "npm run build && npm run jest -- --watch",
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",
50
49
  "prepublishOnly": "npm run build",
51
50
  "prepack": "node scripts/rename-gitignore.js .gitignore gitignore",
52
51
  "postpack": "node scripts/rename-gitignore.js gitignore .gitignore",
53
- "format": "prettier --write \"**/*.{js,jsx,ts,tsx,json,md}\"",
54
- "devnet:start": "node scripts/start-devnet.js ${npm_package_config_alephium_version}",
55
- "devnet:restart": "npm run devnet:start",
56
- "devnet:stop": "node scripts/stop-devnet.js"
52
+ "prettier": "prettier --write \"**/*.{js,jsx,ts,tsx,json,md}\"",
53
+ "start-devnet": "node scripts/start-devnet.js ${npm_package_config_alephium_version}",
54
+ "restart-devnet": "npm run start-devnet",
55
+ "stop-devnet": "node scripts/stop-devnet.js"
57
56
  },
57
+ "type": "commonjs",
58
58
  "bin": {
59
- "alephium": "dist/cli/create-project.js"
59
+ "alephium": "dist/scripts/create-project.js"
60
60
  },
61
61
  "dependencies": {
62
- "base-x": "^4.0.0",
63
- "bcfg": "~0.1.6",
64
- "bip32": "^2.0.6",
65
- "bip39": "^3.0.4",
66
- "blakejs": "^1.1.1",
67
- "chalk": "^4.1.2",
62
+ "@scure/bip32": "1.0.1",
63
+ "base-x": "4.0.0",
64
+ "bip39": "3.0.4",
65
+ "blakejs": "1.2.1",
66
+ "commander": "^9.1.0",
68
67
  "cross-fetch": "^3.1.5",
69
- "crypto-js": "^4.1.1",
70
- "elliptic": "^6.5.4",
68
+ "crypto-js": "4.1.1",
69
+ "elliptic": "6.5.4",
70
+ "eventemitter3": "^4.0.7",
71
71
  "find-up": "^2.1.0",
72
72
  "fs-extra": "^10.0.1"
73
73
  },
@@ -76,7 +76,7 @@
76
76
  "@types/elliptic": "^6.4.13",
77
77
  "@types/find-up": "^2.1.0",
78
78
  "@types/fs-extra": "^9.0.13",
79
- "@types/jest": "^27.0.1",
79
+ "@types/jest": "^27.5.1",
80
80
  "@types/mock-fs": "^4.13.1",
81
81
  "@types/node": "^16.7.8",
82
82
  "@types/rewire": "^2.5.28",
@@ -84,23 +84,30 @@
84
84
  "@typescript-eslint/parser": "^4.30.0",
85
85
  "babel-eslint": "^10.1.0",
86
86
  "buffer": "^6.0.3",
87
+ "clean-webpack-plugin": "4.0.0",
87
88
  "crypto-browserify": "^3.12.0",
88
89
  "eslint": "^7.32.0",
89
- "eslint-config-prettier": "^8.3.0",
90
+ "eslint-config-prettier": "^8.5.0",
90
91
  "eslint-plugin-header": "^3.1.1",
91
92
  "eslint-plugin-prettier": "^4.0.0",
92
- "jest": "^27.1.0",
93
+ "eslint-plugin-react": "^7.29.4",
94
+ "eslint-plugin-security": "1.4.0",
95
+ "html-webpack-plugin": "5.5.0",
96
+ "jest": "^28.1.0",
93
97
  "jest-localstorage-mock": "^2.4.18",
94
98
  "jest-websocket-mock": "^2.2.1",
95
99
  "mock-fs": "^5.1.2",
96
100
  "mock-socket": "^9.0.8",
97
101
  "prettier": "^2.3.2",
102
+ "process": "^0.11.10",
103
+ "react-app-rewired": "^2.2.1",
98
104
  "rewire": "^6.0.0",
99
105
  "shelljs": "^0.8.5",
100
106
  "stream-browserify": "^3.0.0",
101
107
  "swagger-typescript-api": "^9.2.0",
102
- "ts-jest": "^27.0.5",
103
- "ts-node": "^10.2.1",
108
+ "terser-webpack-plugin": "^5.3.1",
109
+ "ts-jest": "^28.0.2",
110
+ "ts-node": "^10.7.0",
104
111
  "tslib": "^2.3.1",
105
112
  "typescript": "^4.4.2",
106
113
  "webpack": "^5.72.0",
@@ -109,5 +116,14 @@
109
116
  "engines": {
110
117
  "node": ">=14.0.0",
111
118
  "npm": ">=7.0.0"
119
+ },
120
+ "prettier": {
121
+ "printWidth": 120,
122
+ "tabWidth": 2,
123
+ "useTabs": false,
124
+ "semi": false,
125
+ "singleQuote": true,
126
+ "bracketSameLine": false,
127
+ "trailingComma": "none"
112
128
  }
113
129
  }
@@ -0,0 +1,136 @@
1
+ #!/usr/bin/env node
2
+ /*
3
+ Copyright 2018 - 2022 The Alephium Authors
4
+ This file is part of the alephium project.
5
+
6
+ The library is free software: you can redistribute it and/or modify
7
+ it under the terms of the GNU Lesser General Public License as published by
8
+ the Free Software Foundation, either version 3 of the License, or
9
+ (at your option) any later version.
10
+
11
+ The library is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ GNU Lesser General Public License for more details.
15
+
16
+ You should have received a copy of the GNU Lesser General Public License
17
+ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
+ */
19
+
20
+ import fsExtra from 'fs-extra'
21
+ import process from 'process'
22
+ import path from 'path'
23
+ import findup from 'find-up'
24
+ import { execSync } from 'child_process'
25
+ import commander from 'commander'
26
+
27
+ function getPackageRoot(): string {
28
+ const packageJsonPath = findup.sync('package.json', { cwd: path.dirname(__filename) })
29
+
30
+ if (packageJsonPath) {
31
+ return path.dirname(packageJsonPath)
32
+ } else {
33
+ throw new Error('Cannot find `package.json`')
34
+ }
35
+ }
36
+
37
+ function extractProjectType(projectType: string): string {
38
+ if (typeof projectType === 'undefined') {
39
+ return 'base'
40
+ } else if (['base', 'react'].includes(projectType)) {
41
+ return projectType
42
+ } else {
43
+ console.log(`Invalid project type: ${projectType}, expect: base or react`)
44
+ process.exit(1)
45
+ }
46
+ }
47
+
48
+ function extractProjectRoot(): string {
49
+ const projectRoot = path.join(projectParent, projectName)
50
+ if (fsExtra.existsSync(projectRoot)) {
51
+ console.log(`Project ${projectName} already exists. Try a different name.`)
52
+ console.log()
53
+ process.exit(1)
54
+ }
55
+ return projectRoot
56
+ }
57
+
58
+ function copy(dir: string, files: string[]) {
59
+ const packageDevDir = path.join(packageRoot, dir)
60
+ const projectDevDir = path.join(projectRoot, dir)
61
+ if (!fsExtra.existsSync(projectDevDir)) {
62
+ fsExtra.mkdirSync(projectDevDir)
63
+ }
64
+ for (const file of files) {
65
+ fsExtra.copyFileSync(path.join(packageDevDir, file), path.join(projectDevDir, file))
66
+ }
67
+ }
68
+
69
+ function prepareShared(packageRoot: string, projectRoot: string) {
70
+ console.log('Copying files')
71
+ console.log(` from ${packageRoot}`)
72
+ console.log(` to ${projectRoot}`)
73
+ console.log('...')
74
+
75
+ fsExtra.copySync(path.join(packageRoot, 'templates/shared'), projectRoot)
76
+ copy('', ['.editorconfig', '.eslintignore', '.gitattributes', 'LICENSE'])
77
+ copy('dev', ['user.conf'])
78
+ copy('scripts', ['start-devnet.js', 'stop-devnet.js'])
79
+ if (fsExtra.existsSync(path.join(packageRoot, 'gitignore'))) {
80
+ fsExtra.copySync(path.join(packageRoot, 'gitignore'), path.join(projectRoot, '.gitignore'))
81
+ } else {
82
+ fsExtra.copySync(path.join(packageRoot, '.gitignore'), path.join(projectRoot, '.gitignore'))
83
+ }
84
+
85
+ console.log()
86
+ }
87
+
88
+ function prepareBase(packageRoot: string, projectRoot: string) {
89
+ prepareShared(packageRoot, projectRoot)
90
+ copy('contracts', ['greeter.ral', 'greeter_interface.ral', 'greeter_main.ral'])
91
+ fsExtra.copySync(path.join(packageRoot, 'templates/base'), projectRoot)
92
+ }
93
+
94
+ function prepareReact(packageRoot: string, projectRoot: string, projectName: string) {
95
+ console.log('Creating the React app')
96
+ execSync(`npx create-react-app ${projectName} --template typescript`)
97
+
98
+ prepareShared(packageRoot, projectRoot)
99
+ fsExtra.copySync(path.join(packageRoot, 'templates/react'), projectRoot)
100
+
101
+ console.log('Initialize the project')
102
+ execSync(
103
+ 'npm install --save-dev react-app-rewired crypto-browserify stream-browserify buffer process eslint-config-prettier eslint-plugin-header eslint-plugin-prettier eslint-plugin-react',
104
+ { cwd: projectRoot }
105
+ )
106
+ execSync('npm install && npm run prettier', { cwd: projectRoot })
107
+ console.log()
108
+ }
109
+
110
+ const program = new commander.Command('Create sample project')
111
+ .arguments('<project-directory>')
112
+ .option('-t, --template <path-to-template>', 'specify a template for the project: either base or react')
113
+ .parse(process.argv)
114
+
115
+ const projectName = program.processedArgs[0]
116
+ const projectType = program.opts()['template']
117
+
118
+ const packageRoot = getPackageRoot()
119
+ const projectParent = process.cwd()
120
+ const projectRoot = extractProjectRoot()
121
+
122
+ switch (extractProjectType(projectType)) {
123
+ case 'base':
124
+ prepareBase(packageRoot, projectRoot)
125
+ break
126
+ case 'react':
127
+ prepareReact(packageRoot, projectRoot, projectName)
128
+ break
129
+ }
130
+
131
+ console.log('✅ Done.')
132
+ console.log()
133
+ console.log('✨ Project is initialized!')
134
+ console.log()
135
+ console.log(`Next step: checkout the readme under <${projectName}>`)
136
+ console.log()
@@ -0,0 +1,17 @@
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
+ */
@@ -71,10 +71,10 @@ function launchDevnet(devDir, jarFile) {
71
71
  fs.writeFileSync(devDir + path.sep + 'alephium.pid', p.pid.toString(), { falg: 'w' })
72
72
  }
73
73
 
74
- const testWallet = 'alephium-js-sdk-test-only-wallet'
74
+ const testWallet = 'alephium-web3-test-only-wallet'
75
75
  const password = 'alph'
76
76
  const mnemonic =
77
- 'vault vault vault vault vault vault vault vault vault vault vault vault vault vault vault vault vault vault vault vault vault vault vault vault'
77
+ 'vault alarm sad mass witness property virus style good flower rice alpha viable evidence run glare pretty scout evil judge enroll refuse another lava'
78
78
 
79
79
  async function prepareWallet() {
80
80
  const wallets = await fetch('http://127.0.0.1:22973/wallets', { method: 'Get' }).then((res) => res.json())
@@ -95,7 +95,7 @@ async function createWallet() {
95
95
 
96
96
  async function unlockWallet() {
97
97
  console.log('Unlock the test wallet')
98
- await fetch('http://127.0.0.1:22973/wallets/alephium-js-sdk-test-only-wallet/unlock', {
98
+ await fetch('http://127.0.0.1:22973/wallets/alephium-web3-test-only-wallet/unlock', {
99
99
  method: 'POST',
100
100
  body: '{ "password": "alph" }'
101
101
  })