-
Notifications
You must be signed in to change notification settings - Fork 1
/
ContractPublication.js
111 lines (94 loc) · 3.04 KB
/
ContractPublication.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const crypto = require('crypto');
const {Entry, Chain} = require('factom');
const util = require('./util');
/*
*
* ABI Model
*
* -list function names
* -list function arguments & argument types
* -list function return types
*
* Possible values argument & return types:
* -'number' - An integer or floating point number
* -'boolean' - Either true or false
* -'string' - A string (character array)
*
* */
const types = [
'number',
'boolean',
'string'
];
const publicationModel = {
abi: {
_add: {
args: ['number', 'number'],
returns: 'number'
},
_echo: {
args: ['string'],
returns: 'string'
}
}
};
class ContractPublicationBuilder {
constructor(wasm) {
//validate wasm binary
if (!Buffer.isBuffer(wasm)) throw new Error('Argument wasm must be a WASM binary buffer');
this._wasm = wasm;
}
func(name, args, returns) {
//initialize ABI object if not already initialized
if (!this._abi) this._abi = {};
//check all argument and return types are valid
if (!Array.isArray(args)) throw new Error('Argument args must be an array');
args.forEach((arg) => {
if (typeof arg !== 'string') throw new Error('Unknown argument type declared on function' + name);
if (!types.includes(arg)) throw new Error('Unknown argument type declared on function' + name);
});
if (!types.includes(returns)) throw new Error('Unknown return type ' + returns + ' declared on func ' + name);
this._abi[name] = {
args,
returns
};
return this;
}
build() {
//Check the WASM binary is valid
try {
//check instantiation works
const module = new WebAssembly.Module(this._wasm);
const instance = new WebAssembly.Instance(module, util.getDefaultImports());
//for each function declared in the ABI check it exists in the binary
Object.keys(this._abi).forEach(function (func) {
if (typeof instance.exports[func] !== 'function') throw new Error('Function \'' + func + '\' was not found in the WASM binary');
});
} catch (e) {
throw new Error('Error loading contract WASM binary: ' + e.message);
}
return new ContractPublication(this);
}
}
class ContractPublication {
constructor(builder) {
this._wasm = builder._wasm;
this._abi = builder._abi;
}
getObject() {
return {
abi: this._abi
}
}
getChain() {
return new Chain(Entry.builder()
.extId(this._wasm, 'binary') //salt the contract to make the deployment random
.extId(crypto.randomBytes(16).toString('utf8'), 'utf8') //salt the contract to make the deployment random
.content(JSON.stringify(this.getObject()), 'utf8')
.build());
}
static builder(wasm) {
return new ContractPublicationBuilder(wasm);
}
}
module.exports = ContractPublication;