@haneullabs/move-bytecode-template 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +175 -0
- package/index.js +7 -0
- package/index.mjs +5 -0
- package/nodejs/move_bytecode_template.d.ts +76 -0
- package/nodejs/move_bytecode_template.js +629 -0
- package/nodejs/move_bytecode_template_bg.wasm +0 -0
- package/nodejs/move_bytecode_template_bg.wasm.d.ts +18 -0
- package/package.json +47 -0
- package/web/move_bytecode_template.d.ts +118 -0
- package/web/move_bytecode_template.js +676 -0
- package/web/move_bytecode_template_bg.wasm +0 -0
- package/web/move_bytecode_template_bg.wasm.d.ts +18 -0
package/README.md
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
# Move Bytecode Template
|
|
2
|
+
|
|
3
|
+
Move Bytecode Template allows updating a pre-compiled bytecode, so that a standard template could be
|
|
4
|
+
customized and used to publish new modules on Haneul directly in the browser. Hence, removing the need
|
|
5
|
+
for a backend to compile new modules.
|
|
6
|
+
|
|
7
|
+
This crate builds a WASM binary for the `move-language/move-binary-format` allowing bytecode
|
|
8
|
+
serialization and deserialization in various environments. The main target for this package is
|
|
9
|
+
"web".
|
|
10
|
+
|
|
11
|
+
## Applications
|
|
12
|
+
|
|
13
|
+
This package is a perfect fit for the following applications:
|
|
14
|
+
|
|
15
|
+
- Publishing new Coins
|
|
16
|
+
- Publishing TransferPolicies
|
|
17
|
+
- Initializing any base type with a custom sub-type
|
|
18
|
+
|
|
19
|
+
## Example of a Template Module
|
|
20
|
+
|
|
21
|
+
The following code is a close-copy of the `Coin` example from the
|
|
22
|
+
[Coins and Tokens](https://docs.sui.io/standards/currency).
|
|
23
|
+
|
|
24
|
+
```move
|
|
25
|
+
module 0x0::template {
|
|
26
|
+
use std::option;
|
|
27
|
+
use sui::coin;
|
|
28
|
+
use sui::transfer;
|
|
29
|
+
use sui::tx_context::{Self, TxContext};
|
|
30
|
+
|
|
31
|
+
/// The OTW for the Coin
|
|
32
|
+
struct TEMPLATE has drop {}
|
|
33
|
+
|
|
34
|
+
const DECIMALS: u8 = 6;
|
|
35
|
+
const SYMBOL: vector<u8> = b"TMPL";
|
|
36
|
+
const NAME: vector<u8> = b"Template Coin";
|
|
37
|
+
const DESCRIPTION: vector<u8> = b"Template Coin Description";
|
|
38
|
+
|
|
39
|
+
/// Init the Coin
|
|
40
|
+
fun init(witness: TEMPLATE, ctx: &mut TxContext) {
|
|
41
|
+
let (treasury, metadata) = coin::create_currency(
|
|
42
|
+
witness, DECIMALS, SYMBOL, NAME, DESCRIPTION, option::none(), ctx
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
transfer::public_transfer(treasury, tx_context::sender(ctx));
|
|
46
|
+
transfer::public_transfer(metadata, tx_context::sender(ctx));
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
To update the identifiers, you can use the `update_identifiers` function.
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
import { fromHex, update_identifiers } from '@haneullabs/move-bytecode-template';
|
|
55
|
+
|
|
56
|
+
let bytecode = /* ... */;
|
|
57
|
+
let updated = update_identifiers(bytecode, {
|
|
58
|
+
"TEMPLATE": "MY_MODULE",
|
|
59
|
+
"template": "my_module"
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
console.assert(updated != bytecode, 'identifiers were not updated!');
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
To update constants in the bytecode, you can use the `update_constants` function. For each constant
|
|
66
|
+
you need to supply new value as BCS bytes, existing value as BCS, and the type of the constant (as a
|
|
67
|
+
string: `U8`, `U16` ... `U256`, `Address`, `Vector(U8)` and so on).
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
import { bcs } from '@haneullabs/bcs';
|
|
71
|
+
import * as template from '@haneullabs/move-bytecode-template';
|
|
72
|
+
|
|
73
|
+
// please, manually scan the existing values, this operation is very sensitive
|
|
74
|
+
console.log(template.get_constants(bytecode));
|
|
75
|
+
|
|
76
|
+
let updated;
|
|
77
|
+
|
|
78
|
+
// Update DECIMALS
|
|
79
|
+
updated = update_constants(
|
|
80
|
+
bytecode,
|
|
81
|
+
bcs.u8().serialize(3).toBytes(), // new value
|
|
82
|
+
bcs.u8().serialize(6).toBytes(), // current value
|
|
83
|
+
'U8', // type of the constant
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
// Update SYMBOL
|
|
87
|
+
updated = update_constants(
|
|
88
|
+
updated,
|
|
89
|
+
bcs.string().serialize('MYC').toBytes(), // new value
|
|
90
|
+
bcs.string().serialize('TMPL').toBytes(), // current value
|
|
91
|
+
'Vector(U8)', // type of the constant
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
// Update NAME
|
|
95
|
+
updated = update_constants(
|
|
96
|
+
updated,
|
|
97
|
+
bcs.string().serialize('My Coin').toBytes(), // new value
|
|
98
|
+
bcs.string().serialize('Template Coin').toBytes(), // current value
|
|
99
|
+
'Vector(U8)', // type of the constant
|
|
100
|
+
);
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
After updating the bytecode, refer to the
|
|
104
|
+
[Asset Tokenization](https://docs.sui.io/guides/developer/nft/asset-tokenization#closer-view-of-the-template-module)
|
|
105
|
+
guide to deploy the contract.
|
|
106
|
+
|
|
107
|
+
## Usage in Web applications
|
|
108
|
+
|
|
109
|
+
In some bundler and web applications you will need to set up and initialize the wasm bindings. The
|
|
110
|
+
exact requirements depend on how your application is being built.
|
|
111
|
+
|
|
112
|
+
The `@haneullabs/move-bytecode-template` has 2 relevant exports you might need to get things working
|
|
113
|
+
correctly.
|
|
114
|
+
|
|
115
|
+
- `init` is an exported function that initializes the wasm module in browser environments (a no-op
|
|
116
|
+
function is exported when running in node so you can use this anywhere)
|
|
117
|
+
- `/web/walrus_wasm_bg.wasm` is an export of the wasm module itself, which may be needed in some
|
|
118
|
+
bundlers to know where to import the wasm module from
|
|
119
|
+
|
|
120
|
+
### Initializing in webpack and nodejs
|
|
121
|
+
|
|
122
|
+
In webpack and nodejs you should be able to initialize the wasm module without specifying the
|
|
123
|
+
location of the wasm module:
|
|
124
|
+
|
|
125
|
+
```ts
|
|
126
|
+
import init, * as template from '@haneullabs/move-bytecode-template';
|
|
127
|
+
|
|
128
|
+
await init();
|
|
129
|
+
|
|
130
|
+
const json = template.deserialize(fromHex('a11ceb0b06....'));
|
|
131
|
+
const bytes = template.serialize(json);
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
## Vite
|
|
135
|
+
|
|
136
|
+
In vite (and some other bundlers) you may need to disable certain optimizations so the wasm module
|
|
137
|
+
can be loaded correctly. You can add something like this to your vite config:
|
|
138
|
+
|
|
139
|
+
```ts
|
|
140
|
+
export default defineConfig({
|
|
141
|
+
optimizeDeps: {
|
|
142
|
+
exclude: ["@haneullabs/move-bytecode-template"],
|
|
143
|
+
},
|
|
144
|
+
});
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## Initializing wasm with a url
|
|
148
|
+
|
|
149
|
+
If initialization fails without the url, you may need to specify a url where the wasm module can be
|
|
150
|
+
loaded. Some bundlers support a `?url` suffix for imports that will return a url where the resource
|
|
151
|
+
can be loaded from:
|
|
152
|
+
|
|
153
|
+
```ts
|
|
154
|
+
import init, * as template from '@haneullabs/move-bytecode-template';
|
|
155
|
+
import url from '@haneullabs/move-bytecode-template/web/move_bytecode_template_bg.wasm?url';
|
|
156
|
+
|
|
157
|
+
await init({ module_or_path: url });
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## Build locally
|
|
161
|
+
|
|
162
|
+
To build the binary, you need to have Rust installed and then the `wasm-pack`. The installation
|
|
163
|
+
script [can be found here](https://github.com/rustwasm/wasm-pack).
|
|
164
|
+
|
|
165
|
+
```
|
|
166
|
+
pnpm build:wasm
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
## Running tests
|
|
170
|
+
|
|
171
|
+
After building tests can be run with
|
|
172
|
+
|
|
173
|
+
```
|
|
174
|
+
pnpm test
|
|
175
|
+
```
|
package/index.js
ADDED
package/index.mjs
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
/**
|
|
4
|
+
* Get the version of the crate (useful for testing the package).
|
|
5
|
+
*/
|
|
6
|
+
export function version(): string;
|
|
7
|
+
/**
|
|
8
|
+
* Deserialize the `Uint8Array`` bytecode into a JSON object.
|
|
9
|
+
* The JSON object contains the ABI (Application Binary Interface) of the module.
|
|
10
|
+
*
|
|
11
|
+
* ```javascript
|
|
12
|
+
* import * as template from '@haneullabs/move-binary-template';
|
|
13
|
+
*
|
|
14
|
+
* const json = template.deserialize( binary );
|
|
15
|
+
* console.log( json, json.identifiers );
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export function deserialize(binary: Uint8Array): any;
|
|
19
|
+
/**
|
|
20
|
+
* Update the identifiers in the module bytecode, given a map of old -> new identifiers.
|
|
21
|
+
* Returns the updated bytecode.
|
|
22
|
+
*
|
|
23
|
+
* ```javascript
|
|
24
|
+
* import * as template from '@haneullabs/move-binary-template';
|
|
25
|
+
*
|
|
26
|
+
* const updated = template.update_identifiers( binary, {
|
|
27
|
+
* 'TEMPLATE': 'NEW_VALUE',
|
|
28
|
+
* 'template': 'new_value',
|
|
29
|
+
* 'Name': 'NewName'
|
|
30
|
+
* });
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export function update_identifiers(binary: Uint8Array, map: any): Uint8Array;
|
|
34
|
+
/**
|
|
35
|
+
* Updates a constant in the constant pool. Because constants don't have names,
|
|
36
|
+
* the only way to identify them is by their type and value.
|
|
37
|
+
*
|
|
38
|
+
* The value of a constant is BCS-encoded and the type is a string representation
|
|
39
|
+
* of the `SignatureToken` enum. String identifier for `SignatureToken` is a
|
|
40
|
+
* capitalized version of the type: U8, Address, Vector(Bool), Vector(U8), etc.
|
|
41
|
+
*
|
|
42
|
+
* ```javascript
|
|
43
|
+
* import * as template from '@haneullabs/move-binary-template';
|
|
44
|
+
* import { bcs } from '@haneullabs/bcs';
|
|
45
|
+
*
|
|
46
|
+
* let binary = template.update_constants(
|
|
47
|
+
* binary, // Uint8Array
|
|
48
|
+
* bcs.u64().serialize(0).toBytes(), // new value
|
|
49
|
+
* bcs.u64().serialize(100000).toBytes(), // old value
|
|
50
|
+
* 'U64' // type
|
|
51
|
+
* );
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export function update_constants(binary: Uint8Array, new_value: Uint8Array, expected_value: Uint8Array, expected_type: string): Uint8Array;
|
|
55
|
+
/**
|
|
56
|
+
* Convenience method to analyze the constant pool; returns all constants in order
|
|
57
|
+
* with their type and BCS value.
|
|
58
|
+
*
|
|
59
|
+
* ```javascript
|
|
60
|
+
* import * as template from '@haneullabs/move-binary-template';
|
|
61
|
+
*
|
|
62
|
+
* let consts = template.get_constants(binary);
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
export function get_constants(binary: Uint8Array): any;
|
|
66
|
+
/**
|
|
67
|
+
* Serialize the JSON module into a `Uint8Array` (bytecode).
|
|
68
|
+
*/
|
|
69
|
+
export function serialize(json_module: any): Uint8Array;
|
|
70
|
+
/**
|
|
71
|
+
* A transformed constant from the constant pool.
|
|
72
|
+
*/
|
|
73
|
+
export class Constant {
|
|
74
|
+
private constructor();
|
|
75
|
+
free(): void;
|
|
76
|
+
}
|