@alephium/web3 0.2.0-rc.8 → 0.2.0-test.1

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 (75) hide show
  1. package/.eslintignore +2 -2
  2. package/.eslintrc.json +21 -0
  3. package/LICENSE +165 -0
  4. package/README.md +135 -2
  5. package/contracts/add/add.ral +13 -0
  6. package/contracts/greeter_main.ral +1 -1
  7. package/contracts/main.ral +4 -0
  8. package/contracts/sub/sub.ral +10 -0
  9. package/contracts/test/metadata.ral +18 -0
  10. package/contracts/test/warnings.ral +8 -0
  11. package/dist/alephium-web3.min.js +1 -1
  12. package/dist/alephium-web3.min.js.LICENSE.txt +17 -0
  13. package/dist/alephium-web3.min.js.map +1 -1
  14. package/dist/scripts/create-project.js +1 -1
  15. package/dist/src/api/api-alephium.d.ts +6 -19
  16. package/dist/src/api/api-explorer.d.ts +16 -16
  17. package/dist/src/api/index.js +1 -5
  18. package/dist/src/contract/contract.d.ts +16 -31
  19. package/dist/src/contract/contract.js +96 -123
  20. package/dist/src/contract/index.js +1 -5
  21. package/dist/src/index.d.ts +0 -1
  22. package/dist/src/index.js +1 -19
  23. package/dist/src/signer/index.d.ts +1 -0
  24. package/dist/src/signer/index.js +2 -5
  25. package/dist/src/signer/node-wallet.d.ts +11 -0
  26. package/dist/src/signer/node-wallet.js +57 -0
  27. package/dist/src/signer/signer.js +1 -5
  28. package/dist/src/test/index.d.ts +6 -0
  29. package/dist/src/test/index.js +41 -0
  30. package/dist/src/test/privatekey-wallet.d.ts +11 -0
  31. package/dist/src/test/privatekey-wallet.js +68 -0
  32. package/dist/src/transaction/index.d.ts +1 -0
  33. package/dist/src/transaction/index.js +2 -5
  34. package/dist/src/transaction/sign-verify.d.ts +2 -0
  35. package/dist/src/transaction/sign-verify.js +58 -0
  36. package/dist/src/utils/index.d.ts +1 -0
  37. package/dist/src/utils/index.js +2 -5
  38. package/dist/src/utils/password-crypto.d.ts +2 -0
  39. package/dist/src/utils/password-crypto.js +69 -0
  40. package/dist/src/utils/utils.d.ts +2 -3
  41. package/dist/src/utils/utils.js +15 -16
  42. package/gitignore +9 -0
  43. package/package.json +32 -6
  44. package/scripts/create-project.ts +1 -1
  45. package/src/api/api-alephium.ts +0 -14
  46. package/src/contract/contract.ts +102 -176
  47. package/src/contract/ralph.test.ts +178 -0
  48. package/src/fixtures/address.json +36 -0
  49. package/src/fixtures/balance.json +9 -0
  50. package/src/fixtures/self-clique.json +19 -0
  51. package/src/fixtures/transaction.json +13 -0
  52. package/src/fixtures/transactions.json +179 -0
  53. package/src/index.ts +0 -2
  54. package/src/signer/fixtures/genesis.json +26 -0
  55. package/src/signer/fixtures/wallets.json +26 -0
  56. package/src/signer/index.ts +1 -0
  57. package/src/signer/node-wallet.ts +65 -0
  58. package/src/test/index.ts +31 -0
  59. package/src/test/privatekey-wallet.ts +57 -0
  60. package/src/transaction/index.ts +1 -0
  61. package/src/transaction/sign-verify.test.ts +50 -0
  62. package/src/transaction/sign-verify.ts +39 -0
  63. package/src/utils/address.test.ts +47 -0
  64. package/src/utils/djb2.test.ts +35 -0
  65. package/src/utils/index.ts +1 -0
  66. package/src/utils/password-crypto.test.ts +27 -0
  67. package/src/utils/password-crypto.ts +77 -0
  68. package/src/utils/utils.test.ts +161 -0
  69. package/src/utils/utils.ts +7 -7
  70. package/templates/base/package.json +1 -1
  71. package/templates/react/package.json +1 -1
  72. package/test/contract.test.ts +213 -0
  73. package/test/events.test.ts +141 -0
  74. package/test/transaction.test.ts +73 -0
  75. package/jest-config.json +0 -11
@@ -18,11 +18,7 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
18
  */
19
19
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
20
20
  if (k2 === undefined) k2 = k;
21
- var desc = Object.getOwnPropertyDescriptor(m, k);
22
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
23
- desc = { enumerable: true, get: function() { return m[k]; } };
24
- }
25
- Object.defineProperty(o, k2, desc);
21
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
26
22
  }) : (function(o, m, k, k2) {
27
23
  if (k2 === undefined) k2 = k;
28
24
  o[k2] = m[k];
@@ -43,9 +39,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
43
39
  return (mod && mod.__esModule) ? mod : { "default": mod };
44
40
  };
45
41
  Object.defineProperty(exports, "__esModule", { value: true });
46
- exports.toApiVal = exports.extractArray = exports.Script = exports.Contract = exports.Artifact = exports.Project = exports.DEFAULT_COMPILER_OPTIONS = exports.DEFAULT_NODE_COMPILER_OPTIONS = void 0;
42
+ exports.toApiVal = exports.extractArray = exports.Script = exports.Contract = exports.Artifact = exports.Project = exports.DEFAULT_COMPILER_OPTIONS = void 0;
47
43
  const buffer_1 = require("buffer/");
48
- const crypto_1 = require("crypto");
44
+ const cryptojs = __importStar(require("crypto-js"));
45
+ const crypto = __importStar(require("crypto"));
49
46
  const fs_1 = __importDefault(require("fs"));
50
47
  const fs_2 = require("fs");
51
48
  const ralph = __importStar(require("./ralph"));
@@ -58,39 +55,35 @@ var SourceType;
58
55
  SourceType[SourceType["AbstractContract"] = 2] = "AbstractContract";
59
56
  SourceType[SourceType["Interface"] = 3] = "Interface";
60
57
  })(SourceType || (SourceType = {}));
61
- exports.DEFAULT_NODE_COMPILER_OPTIONS = {
62
- ignoreUnusedConstantsWarnings: false,
63
- ignoreUnusedVariablesWarnings: false,
64
- ignoreUnusedFieldsWarnings: false,
65
- ignoreUnusedPrivateFunctionsWarnings: false,
66
- ignoreReadonlyCheckWarnings: false,
67
- ignoreExternalCallCheckWarnings: false
58
+ exports.DEFAULT_COMPILER_OPTIONS = {
59
+ errorOnWarnings: true,
60
+ ignoreUnusedConstantsWarnings: true
68
61
  };
69
- exports.DEFAULT_COMPILER_OPTIONS = { errorOnWarnings: true, ...exports.DEFAULT_NODE_COMPILER_OPTIONS };
70
62
  class TypedMatcher {
71
63
  constructor(pattern, type) {
72
64
  this.matcher = new RegExp(pattern, 'mg');
73
65
  this.type = type;
66
+ this.name = undefined;
74
67
  }
75
68
  match(str) {
76
- const results = str.match(this.matcher);
77
- return results === null ? 0 : results.length;
69
+ const results = [...str.matchAll(this.matcher)];
70
+ if (results.length > 0) {
71
+ this.name = results[0][1];
72
+ }
73
+ return results.length;
78
74
  }
79
75
  }
80
76
  class SourceFile {
81
- constructor(type, sourceCode, sourceCodeHash, contractPath) {
77
+ constructor(type, typeId, sourceCode, contractPath) {
82
78
  this.type = type;
79
+ this.typeId = typeId;
83
80
  this.sourceCode = sourceCode;
84
- this.sourceCodeHash = sourceCodeHash;
81
+ this.sourceCodeHash = cryptojs.SHA256(sourceCode).toString();
85
82
  this.contractPath = contractPath;
86
83
  }
87
84
  getArtifactPath(artifactsRootPath) {
88
85
  return artifactsRootPath + this.contractPath.slice(this.contractPath.indexOf('/')) + '.json';
89
86
  }
90
- static async from(type, sourceCode, contractPath) {
91
- const sourceCodeHash = await crypto_1.webcrypto.subtle.digest('SHA-256', buffer_1.Buffer.from(sourceCode));
92
- return new SourceFile(type, sourceCode, buffer_1.Buffer.from(sourceCodeHash).toString('hex'), contractPath);
93
- }
94
87
  }
95
88
  class Compiled {
96
89
  constructor(sourceFile, artifact, warnings) {
@@ -100,35 +93,15 @@ class Compiled {
100
93
  }
101
94
  }
102
95
  class ProjectArtifact {
103
- constructor(compilerOptionsUsed, infos) {
104
- ProjectArtifact.checkCompilerOptionsParameter(compilerOptionsUsed);
105
- this.compilerOptionsUsed = compilerOptionsUsed;
96
+ constructor(infos) {
106
97
  this.infos = infos;
107
98
  }
108
- static checkCompilerOptionsParameter(compilerOptions) {
109
- if (Object.keys(compilerOptions).length != Object.keys(exports.DEFAULT_NODE_COMPILER_OPTIONS).length) {
110
- throw Error(`Not all compiler options are set: ${compilerOptions}`);
111
- }
112
- const combined = { ...compilerOptions, ...exports.DEFAULT_NODE_COMPILER_OPTIONS };
113
- if (Object.keys(combined).length !== Object.keys(exports.DEFAULT_NODE_COMPILER_OPTIONS).length) {
114
- throw Error(`There are unknown compiler options: ${compilerOptions}`);
115
- }
116
- }
117
99
  async saveToFile(rootPath) {
118
100
  const filepath = rootPath + '/' + ProjectArtifact.artifactFileName;
119
- const artifact = { compilerOptionsUsed: this.compilerOptionsUsed, infos: Object.fromEntries(this.infos) };
120
- const content = JSON.stringify(artifact, null, 2);
101
+ const content = JSON.stringify(Object.fromEntries(this.infos), null, 2);
121
102
  return fs_2.promises.writeFile(filepath, content);
122
103
  }
123
- needToReCompile(compilerOptions, files) {
124
- ProjectArtifact.checkCompilerOptionsParameter(compilerOptions);
125
- const optionsMatched = Object.entries(compilerOptions).every(([key, inputOption]) => {
126
- const usedOption = this.compilerOptionsUsed[`${key}`];
127
- return usedOption === inputOption;
128
- });
129
- if (!optionsMatched) {
130
- return true;
131
- }
104
+ sourceHasChanged(files) {
132
105
  if (files.length !== this.infos.size) {
133
106
  return true;
134
107
  }
@@ -146,48 +119,19 @@ class ProjectArtifact {
146
119
  return undefined;
147
120
  }
148
121
  const content = await fs_2.promises.readFile(filepath);
149
- const json = JSON.parse(content.toString());
150
- const compilerOptionsUsed = json.compilerOptionsUsed;
151
- const files = new Map(Object.entries(json.infos));
152
- return new ProjectArtifact(compilerOptionsUsed, files);
122
+ const files = new Map(Object.entries(JSON.parse(content.toString())));
123
+ return new ProjectArtifact(files);
153
124
  }
154
125
  }
155
126
  ProjectArtifact.artifactFileName = '.project.json';
156
127
  class Project {
157
- constructor(provider, contractsRootPath, artifactsRootPath, sourceFiles, contracts, scripts, errorOnWarnings, projectArtifact) {
128
+ constructor(provider, contractsRootPath, artifactsRootPath, sourceFiles, contracts, scripts) {
158
129
  this.nodeProvider = provider;
159
130
  this.contractsRootPath = contractsRootPath;
160
131
  this.artifactsRootPath = artifactsRootPath;
161
132
  this.sourceFiles = sourceFiles;
162
133
  this.contracts = contracts;
163
134
  this.scripts = scripts;
164
- this.projectArtifact = projectArtifact;
165
- if (errorOnWarnings) {
166
- Project.checkCompilerWarnings([...contracts.map((c) => c.warnings).flat(), ...scripts.map((s) => s.warnings).flat()], errorOnWarnings);
167
- }
168
- }
169
- static buildProjectArtifact(sourceFiles, contracts, scripts, compilerOptions) {
170
- const files = new Map();
171
- contracts.forEach((c) => {
172
- files.set(c.sourceFile.contractPath, {
173
- sourceCodeHash: c.sourceFile.sourceCodeHash,
174
- warnings: c.warnings
175
- });
176
- });
177
- scripts.forEach((s) => {
178
- files.set(s.sourceFile.contractPath, {
179
- sourceCodeHash: s.sourceFile.sourceCodeHash,
180
- warnings: s.warnings
181
- });
182
- });
183
- const compiledSize = contracts.length + scripts.length;
184
- sourceFiles.slice(compiledSize).forEach((c) => {
185
- files.set(c.contractPath, {
186
- sourceCodeHash: c.sourceCodeHash,
187
- warnings: []
188
- });
189
- });
190
- return new ProjectArtifact(compilerOptions, files);
191
135
  }
192
136
  getContractPath(path) {
193
137
  return path.startsWith(`./${this.contractsRootPath}`)
@@ -196,12 +140,15 @@ class Project {
196
140
  ? path
197
141
  : this.contractsRootPath + '/' + path;
198
142
  }
199
- static checkCompilerWarnings(warnings, errorOnWarnings) {
200
- if (warnings.length !== 0) {
143
+ static checkCompilerWarnings(warnings, compilerOptions) {
144
+ const remains = compilerOptions.ignoreUnusedConstantsWarnings
145
+ ? warnings.filter((s) => !s.includes('unused constants'))
146
+ : warnings;
147
+ if (remains.length !== 0) {
201
148
  const prefixPerWarning = ' - ';
202
- const warningString = prefixPerWarning + warnings.join('\n' + prefixPerWarning);
203
- const output = `Compilation warnings:\n` + warningString + '\n';
204
- if (errorOnWarnings) {
149
+ const warningString = prefixPerWarning + remains.join('\n' + prefixPerWarning);
150
+ const output = 'Compilation warnings:\n' + warningString + '\n';
151
+ if (compilerOptions.errorOnWarnings) {
205
152
  throw new Error(output);
206
153
  }
207
154
  else {
@@ -209,20 +156,22 @@ class Project {
209
156
  }
210
157
  }
211
158
  }
212
- static contract(path) {
159
+ static contract(path, compilerOptions) {
213
160
  const contractPath = Project.currentProject.getContractPath(path);
214
161
  const contract = Project.currentProject.contracts.find((c) => c.sourceFile.contractPath === contractPath);
215
162
  if (typeof contract === 'undefined') {
216
163
  throw new Error(`Contract ${contractPath} does not exist`);
217
164
  }
165
+ Project.checkCompilerWarnings(contract.warnings, { ...exports.DEFAULT_COMPILER_OPTIONS, ...compilerOptions });
218
166
  return contract.artifact;
219
167
  }
220
- static script(path) {
168
+ static script(path, compilerOptions) {
221
169
  const contractPath = Project.currentProject.getContractPath(path);
222
170
  const script = Project.currentProject.scripts.find((c) => c.sourceFile.contractPath === contractPath);
223
171
  if (typeof script === 'undefined') {
224
172
  throw new Error(`Script ${contractPath} does not exist`);
225
173
  }
174
+ Project.checkCompilerWarnings(script.warnings, { ...exports.DEFAULT_COMPILER_OPTIONS, ...compilerOptions });
226
175
  return script.artifact;
227
176
  }
228
177
  async saveArtifactsToFile() {
@@ -241,7 +190,6 @@ class Project {
241
190
  for (const script of this.scripts) {
242
191
  await saveToFile(script);
243
192
  }
244
- await this.projectArtifact.saveToFile(this.artifactsRootPath);
245
193
  }
246
194
  contractByCodeHash(codeHash) {
247
195
  const contract = this.contracts.find((c) => c.artifact.codeHash === codeHash);
@@ -250,30 +198,53 @@ class Project {
250
198
  }
251
199
  return contract.artifact;
252
200
  }
253
- static async compile(provider, files, contractsRootPath, artifactsRootPath, errorOnWarnings, compilerOptions) {
201
+ async saveProjectArtifactToFile() {
202
+ const files = new Map();
203
+ this.contracts.forEach((c) => {
204
+ files.set(c.sourceFile.contractPath, {
205
+ sourceCodeHash: c.sourceFile.sourceCodeHash,
206
+ warnings: c.warnings
207
+ });
208
+ });
209
+ this.scripts.forEach((s) => {
210
+ files.set(s.sourceFile.contractPath, {
211
+ sourceCodeHash: s.sourceFile.sourceCodeHash,
212
+ warnings: s.warnings
213
+ });
214
+ });
215
+ const compiledSize = this.contracts.length + this.scripts.length;
216
+ this.sourceFiles.slice(compiledSize).forEach((c) => {
217
+ files.set(c.contractPath, {
218
+ sourceCodeHash: c.sourceCodeHash,
219
+ warnings: []
220
+ });
221
+ });
222
+ const projectArtifact = new ProjectArtifact(files);
223
+ await projectArtifact.saveToFile(this.artifactsRootPath);
224
+ }
225
+ static async compile(provider, files, contractsRootPath, artifactsRootPath) {
254
226
  const sourceStr = files.map((f) => f.sourceCode).join('\n');
255
227
  const result = await provider.contracts.postContractsCompileProject({
256
- code: sourceStr,
257
- compilerOptions: compilerOptions
228
+ code: sourceStr
258
229
  });
259
230
  const contracts = [];
260
231
  const scripts = [];
261
232
  result.contracts.forEach((contractResult, index) => {
262
233
  const sourceFile = files[`${index}`];
263
- const contract = Contract.fromCompileResult(contractResult);
234
+ const contract = Contract.fromCompileResult(sourceFile.typeId, contractResult);
264
235
  contracts.push(new Compiled(sourceFile, contract, contractResult.warnings));
265
236
  });
266
237
  result.scripts.forEach((scriptResult, index) => {
267
238
  const sourceFile = files[index + contracts.length];
268
- const script = Script.fromCompileResult(scriptResult);
239
+ const script = Script.fromCompileResult(sourceFile.typeId, scriptResult);
269
240
  scripts.push(new Compiled(sourceFile, script, scriptResult.warnings));
270
241
  });
271
- const projectArtifact = Project.buildProjectArtifact(files, contracts, scripts, compilerOptions);
272
- const project = new Project(provider, contractsRootPath, artifactsRootPath, files, contracts, scripts, errorOnWarnings, projectArtifact);
242
+ const project = new Project(provider, contractsRootPath, artifactsRootPath, files, contracts, scripts);
273
243
  await project.saveArtifactsToFile();
244
+ await project.saveProjectArtifactToFile();
274
245
  return project;
275
246
  }
276
- static async loadArtifacts(provider, files, projectArtifact, contractsRootPath, artifactsRootPath, errorOnWarnings, compilerOptions) {
247
+ static async loadArtifacts(provider, files, projectArtifact, contractsRootPath, artifactsRootPath) {
277
248
  try {
278
249
  const contracts = [];
279
250
  const scripts = [];
@@ -293,11 +264,11 @@ class Project {
293
264
  scripts.push(new Compiled(file, artifact, warnings));
294
265
  }
295
266
  }
296
- return new Project(provider, contractsRootPath, artifactsRootPath, files, contracts, scripts, errorOnWarnings, projectArtifact);
267
+ return new Project(provider, contractsRootPath, artifactsRootPath, files, contracts, scripts);
297
268
  }
298
269
  catch (error) {
299
270
  console.log(`Failed to load artifacts, error: ${error}, try to re-compile contracts...`);
300
- return Project.compile(provider, files, contractsRootPath, artifactsRootPath, errorOnWarnings, compilerOptions);
271
+ return Project.compile(provider, files, contractsRootPath, artifactsRootPath);
301
272
  }
302
273
  }
303
274
  static async loadSourceFile(dirPath, filename) {
@@ -316,8 +287,12 @@ class Project {
316
287
  throw new Error(`Multiple definitions in file: ${contractPath}`);
317
288
  }
318
289
  const matcherIndex = results.indexOf(1);
319
- const type = this.matchers[`${matcherIndex}`].type;
320
- return SourceFile.from(type, sourceStr, contractPath);
290
+ const matcher = this.matchers[`${matcherIndex}`];
291
+ const type = matcher.type;
292
+ if (matcher.name === undefined) {
293
+ throw new Error(`Invalid definition in file: ${contractPath}`);
294
+ }
295
+ return new SourceFile(type, matcher.name, sourceStr, contractPath);
321
296
  }
322
297
  static async loadSourceFiles(contractsRootPath) {
323
298
  const loadDir = async function (dirPath, results) {
@@ -341,26 +316,23 @@ class Project {
341
316
  }
342
317
  return sourceFiles.sort((a, b) => a.type - b.type);
343
318
  }
344
- static async build(compilerOptionsPartial = {}, contractsRootPath = 'contracts', artifactsRootPath = 'artifacts') {
319
+ static async build(contractsRootPath = 'contracts', artifactsRootPath = 'artifacts') {
345
320
  const provider = (0, global_1.getCurrentNodeProvider)();
346
321
  const sourceFiles = await Project.loadSourceFiles(contractsRootPath);
347
- const { errorOnWarnings, ...nodeCompilerOptions } = { ...exports.DEFAULT_COMPILER_OPTIONS, ...compilerOptionsPartial };
348
322
  const projectArtifact = await ProjectArtifact.from(artifactsRootPath);
349
- if (typeof projectArtifact === 'undefined' || projectArtifact.needToReCompile(nodeCompilerOptions, sourceFiles)) {
350
- console.log(`Compile contracts in folder "${contractsRootPath}"`);
351
- Project.currentProject = await Project.compile(provider, sourceFiles, contractsRootPath, artifactsRootPath, errorOnWarnings, nodeCompilerOptions);
323
+ if (typeof projectArtifact === 'undefined' || projectArtifact.sourceHasChanged(sourceFiles)) {
324
+ Project.currentProject = await Project.compile(provider, sourceFiles, contractsRootPath, artifactsRootPath);
352
325
  }
353
326
  else {
354
- console.log(`Load compiled contracts from folder "${artifactsRootPath}"`);
355
- Project.currentProject = await Project.loadArtifacts(provider, sourceFiles, projectArtifact, contractsRootPath, artifactsRootPath, errorOnWarnings, nodeCompilerOptions);
327
+ Project.currentProject = await Project.loadArtifacts(provider, sourceFiles, projectArtifact, contractsRootPath, artifactsRootPath);
356
328
  }
357
329
  }
358
330
  }
359
331
  exports.Project = Project;
360
- Project.abstractContractMatcher = new TypedMatcher('^Abstract Contract [A-Z][a-zA-Z0-9]*', SourceType.AbstractContract);
361
- Project.contractMatcher = new TypedMatcher('^Contract [A-Z][a-zA-Z0-9]*', SourceType.Contract);
362
- Project.interfaceMatcher = new TypedMatcher('^Interface [A-Z][a-zA-Z0-9]* \\{', SourceType.Interface);
363
- Project.scriptMatcher = new TypedMatcher('^TxScript [A-Z][a-zA-Z0-9]*', SourceType.Script);
332
+ Project.abstractContractMatcher = new TypedMatcher('^Abstract Contract ([A-Z][a-zA-Z0-9]*)\\(*', SourceType.AbstractContract);
333
+ Project.contractMatcher = new TypedMatcher('^Contract ([A-Z][a-zA-Z0-9]*)\\(*', SourceType.Contract);
334
+ Project.interfaceMatcher = new TypedMatcher('^Interface ([A-Z][a-zA-Z0-9]*) \\{', SourceType.Interface);
335
+ Project.scriptMatcher = new TypedMatcher('^TxScript ([A-Z][a-zA-Z0-9]*)( \\{*|\\(*)', SourceType.Script);
364
336
  Project.matchers = [
365
337
  Project.abstractContractMatcher,
366
338
  Project.contractMatcher,
@@ -368,7 +340,8 @@ Project.matchers = [
368
340
  Project.scriptMatcher
369
341
  ];
370
342
  class Artifact {
371
- constructor(functions) {
343
+ constructor(typeId, functions) {
344
+ this.typeId = typeId;
372
345
  this.functions = functions;
373
346
  }
374
347
  publicFunctions() {
@@ -383,8 +356,8 @@ class Artifact {
383
356
  }
384
357
  exports.Artifact = Artifact;
385
358
  class Contract extends Artifact {
386
- constructor(bytecode, codeHash, fieldsSig, eventsSig, functions) {
387
- super(functions);
359
+ constructor(typeId, bytecode, codeHash, fieldsSig, eventsSig, functions) {
360
+ super(typeId, functions);
388
361
  this.bytecode = bytecode;
389
362
  this.codeHash = codeHash;
390
363
  this.fieldsSig = fieldsSig;
@@ -399,11 +372,11 @@ class Contract extends Artifact {
399
372
  artifact.functions == null) {
400
373
  throw Error('The artifact JSON for contract is incomplete');
401
374
  }
402
- const contract = new Contract(artifact.bytecode, artifact.codeHash, artifact.fieldsSig, artifact.eventsSig, artifact.functions);
375
+ const contract = new Contract(artifact.typeId, artifact.bytecode, artifact.codeHash, artifact.fieldsSig, artifact.eventsSig, artifact.functions);
403
376
  return contract;
404
377
  }
405
- static fromCompileResult(result) {
406
- return new Contract(result.bytecode, result.codeHash, result.fields, result.events, result.functions);
378
+ static fromCompileResult(typeId, result) {
379
+ return new Contract(typeId, result.bytecode, result.codeHash, result.fields, result.events, result.functions);
407
380
  }
408
381
  // support both 'code.ral' and 'code.ral.json'
409
382
  static async fromArtifactFile(path) {
@@ -419,6 +392,7 @@ class Contract extends Artifact {
419
392
  }
420
393
  toString() {
421
394
  const object = {
395
+ typeId: this.typeId,
422
396
  bytecode: this.bytecode,
423
397
  codeHash: this.codeHash,
424
398
  fieldsSig: this.fieldsSig,
@@ -439,10 +413,8 @@ class Contract extends Artifact {
439
413
  asset: asset
440
414
  };
441
415
  }
442
- // no need to be cryptographically strong random
443
416
  static randomAddress() {
444
- const bytes = new Uint8Array(33);
445
- crypto_1.webcrypto.getRandomValues(bytes);
417
+ const bytes = crypto.randomBytes(33);
446
418
  bytes[0] = 3;
447
419
  return utils_1.bs58.encode(bytes);
448
420
  }
@@ -596,20 +568,20 @@ Contract.ContractDestroyedEvent = {
596
568
  fieldTypes: ['Address']
597
569
  };
598
570
  class Script extends Artifact {
599
- constructor(bytecodeTemplate, fieldsSig, functions) {
600
- super(functions);
571
+ constructor(typeId, bytecodeTemplate, fieldsSig, functions) {
572
+ super(typeId, functions);
601
573
  this.bytecodeTemplate = bytecodeTemplate;
602
574
  this.fieldsSig = fieldsSig;
603
575
  }
604
- static fromCompileResult(result) {
605
- return new Script(result.bytecodeTemplate, result.fields, result.functions);
576
+ static fromCompileResult(typeId, result) {
577
+ return new Script(typeId, result.bytecodeTemplate, result.fields, result.functions);
606
578
  }
607
579
  // TODO: safely parse json
608
580
  static fromJson(artifact) {
609
581
  if (artifact.bytecodeTemplate == null || artifact.fieldsSig == null || artifact.functions == null) {
610
582
  throw Error('The artifact JSON for script is incomplete');
611
583
  }
612
- return new Script(artifact.bytecodeTemplate, artifact.fieldsSig, artifact.functions);
584
+ return new Script(artifact.typeId, artifact.bytecodeTemplate, artifact.fieldsSig, artifact.functions);
613
585
  }
614
586
  static async fromArtifactFile(path) {
615
587
  const content = await fs_2.promises.readFile(path);
@@ -618,6 +590,7 @@ class Script extends Artifact {
618
590
  }
619
591
  toString() {
620
592
  const object = {
593
+ typeId: this.typeId,
621
594
  bytecodeTemplate: this.bytecodeTemplate,
622
595
  fieldsSig: this.fieldsSig,
623
596
  functions: this.functions
@@ -18,11 +18,7 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
18
  */
19
19
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
20
20
  if (k2 === undefined) k2 = k;
21
- var desc = Object.getOwnPropertyDescriptor(m, k);
22
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
23
- desc = { enumerable: true, get: function() { return m[k]; } };
24
- }
25
- Object.defineProperty(o, k2, desc);
21
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
26
22
  }) : (function(o, m, k, k2) {
27
23
  if (k2 === undefined) k2 = k;
28
24
  o[k2] = m[k];
@@ -5,4 +5,3 @@ export * from './utils';
5
5
  export * from './transaction';
6
6
  export * from './constants';
7
7
  export * from './global';
8
- export * as utils from './utils';
package/dist/src/index.js CHANGED
@@ -18,32 +18,15 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
18
  */
19
19
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
20
20
  if (k2 === undefined) k2 = k;
21
- var desc = Object.getOwnPropertyDescriptor(m, k);
22
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
23
- desc = { enumerable: true, get: function() { return m[k]; } };
24
- }
25
- Object.defineProperty(o, k2, desc);
21
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
26
22
  }) : (function(o, m, k, k2) {
27
23
  if (k2 === undefined) k2 = k;
28
24
  o[k2] = m[k];
29
25
  }));
30
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
31
- Object.defineProperty(o, "default", { enumerable: true, value: v });
32
- }) : function(o, v) {
33
- o["default"] = v;
34
- });
35
26
  var __exportStar = (this && this.__exportStar) || function(m, exports) {
36
27
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
37
28
  };
38
- var __importStar = (this && this.__importStar) || function (mod) {
39
- if (mod && mod.__esModule) return mod;
40
- var result = {};
41
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
42
- __setModuleDefault(result, mod);
43
- return result;
44
- };
45
29
  Object.defineProperty(exports, "__esModule", { value: true });
46
- exports.utils = void 0;
47
30
  __exportStar(require("./api"), exports);
48
31
  __exportStar(require("./contract"), exports);
49
32
  __exportStar(require("./signer"), exports);
@@ -51,4 +34,3 @@ __exportStar(require("./utils"), exports);
51
34
  __exportStar(require("./transaction"), exports);
52
35
  __exportStar(require("./constants"), exports);
53
36
  __exportStar(require("./global"), exports);
54
- exports.utils = __importStar(require("./utils"));
@@ -1 +1,2 @@
1
1
  export * from './signer';
2
+ export * from './node-wallet';
@@ -18,11 +18,7 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
18
  */
19
19
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
20
20
  if (k2 === undefined) k2 = k;
21
- var desc = Object.getOwnPropertyDescriptor(m, k);
22
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
23
- desc = { enumerable: true, get: function() { return m[k]; } };
24
- }
25
- Object.defineProperty(o, k2, desc);
21
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
26
22
  }) : (function(o, m, k, k2) {
27
23
  if (k2 === undefined) k2 = k;
28
24
  o[k2] = m[k];
@@ -32,3 +28,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
32
28
  };
33
29
  Object.defineProperty(exports, "__esModule", { value: true });
34
30
  __exportStar(require("./signer"), exports);
31
+ __exportStar(require("./node-wallet"), exports);
@@ -0,0 +1,11 @@
1
+ import { Account, SignerWithNodeProvider } from '../signer';
2
+ export declare class NodeWallet extends SignerWithNodeProvider {
3
+ walletName: string;
4
+ accounts: Account[] | undefined;
5
+ constructor(walletName: string, alwaysSubmitTx?: boolean);
6
+ getAccounts(): Promise<Account[]>;
7
+ private getAllAccounts;
8
+ signRaw(signerAddress: string, hexString: string): Promise<string>;
9
+ unlock(password: string): Promise<void>;
10
+ lock(): Promise<void>;
11
+ }
@@ -0,0 +1,57 @@
1
+ "use strict";
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
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ exports.NodeWallet = void 0;
21
+ const signer_1 = require("../signer");
22
+ class NodeWallet extends signer_1.SignerWithNodeProvider {
23
+ constructor(walletName, alwaysSubmitTx = true) {
24
+ super(alwaysSubmitTx);
25
+ this.walletName = walletName;
26
+ }
27
+ async getAccounts() {
28
+ if (typeof this.accounts === 'undefined') {
29
+ this.accounts = await this.getAllAccounts();
30
+ }
31
+ return this.accounts;
32
+ }
33
+ async getAllAccounts() {
34
+ const walletAddresses = await this.provider.wallets.getWalletsWalletNameAddresses(this.walletName);
35
+ const accounts = walletAddresses.addresses.map((acc) => ({
36
+ publicKey: acc.publicKey,
37
+ address: acc.address,
38
+ group: acc.group
39
+ }));
40
+ return accounts;
41
+ }
42
+ async signRaw(signerAddress, hexString) {
43
+ const currentActiveAddressResponse = await this.provider.wallets.getWalletsWalletNameAddresses(this.walletName);
44
+ const { activeAddress } = currentActiveAddressResponse;
45
+ await this.provider.wallets.postWalletsWalletNameChangeActiveAddress(this.walletName, { address: signerAddress });
46
+ const { signature } = await this.provider.wallets.postWalletsWalletNameSign(this.walletName, { data: hexString });
47
+ await this.provider.wallets.postWalletsWalletNameChangeActiveAddress(this.walletName, { address: activeAddress }); // set the address that's active back to previous state
48
+ return signature;
49
+ }
50
+ async unlock(password) {
51
+ return await this.provider.wallets.postWalletsWalletNameUnlock(this.walletName, { password });
52
+ }
53
+ async lock() {
54
+ return await this.provider.wallets.postWalletsWalletNameLock(this.walletName);
55
+ }
56
+ }
57
+ exports.NodeWallet = NodeWallet;
@@ -18,11 +18,7 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
18
18
  */
19
19
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
20
20
  if (k2 === undefined) k2 = k;
21
- var desc = Object.getOwnPropertyDescriptor(m, k);
22
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
23
- desc = { enumerable: true, get: function() { return m[k]; } };
24
- }
25
- Object.defineProperty(o, k2, desc);
21
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
26
22
  }) : (function(o, m, k, k2) {
27
23
  if (k2 === undefined) k2 = k;
28
24
  o[k2] = m[k];
@@ -0,0 +1,6 @@
1
+ import { NodeWallet } from '../signer';
2
+ export declare const testWalletName = "alephium-web3-test-only-wallet";
3
+ export declare const testAddress = "1DrDyTr9RpRsQnDnXo2YRiPzPW4ooHX5LLoqXrqfMrpQH";
4
+ export declare const testPassword = "alph";
5
+ export declare function testNodeWallet(): Promise<NodeWallet>;
6
+ export * from './privatekey-wallet';
@@ -0,0 +1,41 @@
1
+ "use strict";
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
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
20
+ if (k2 === undefined) k2 = k;
21
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
22
+ }) : (function(o, m, k, k2) {
23
+ if (k2 === undefined) k2 = k;
24
+ o[k2] = m[k];
25
+ }));
26
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
27
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
28
+ };
29
+ Object.defineProperty(exports, "__esModule", { value: true });
30
+ exports.testNodeWallet = exports.testPassword = exports.testAddress = exports.testWalletName = void 0;
31
+ const signer_1 = require("../signer");
32
+ exports.testWalletName = 'alephium-web3-test-only-wallet';
33
+ exports.testAddress = '1DrDyTr9RpRsQnDnXo2YRiPzPW4ooHX5LLoqXrqfMrpQH';
34
+ exports.testPassword = 'alph';
35
+ async function testNodeWallet() {
36
+ const wallet = new signer_1.NodeWallet(exports.testWalletName);
37
+ await wallet.unlock(exports.testPassword);
38
+ return wallet;
39
+ }
40
+ exports.testNodeWallet = testNodeWallet;
41
+ __exportStar(require("./privatekey-wallet"), exports);