suidouble 0.0.16 → 0.0.17

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 CHANGED
@@ -305,6 +305,15 @@ suiInBrowser.addEventListener('connected', async()=>{
305
305
 
306
306
  ```
307
307
 
308
+ ### Unit tests
309
+
310
+ ```bash
311
+ npm install
312
+ npm run tests
313
+ ```
314
+
315
+ Take a look at [unit tests](test) code for some inspiration.
316
+
308
317
  ### Todo
309
318
 
310
319
  - subscribe to events
package/lib/SuiMaster.js CHANGED
@@ -15,8 +15,13 @@ class SuiMaster extends SuiCommonMethods {
15
15
  this._signer = null;
16
16
  this._keypair = null;
17
17
 
18
+ this._address = null;
19
+
18
20
  if (params.signer) {
19
21
  this._signer = params.signer;
22
+ if (this._signer && this._signer.connectedAddress) {
23
+ this._address = this._signer.connectedAddress;
24
+ }
20
25
  } else if (params.keypair) {
21
26
  this._keypair = params.keypair;
22
27
  } else if (params.phrase) {
@@ -86,8 +91,6 @@ class SuiMaster extends SuiCommonMethods {
86
91
 
87
92
  this._initialized = false;
88
93
 
89
- this._address = null;
90
-
91
94
  this._packages = {};
92
95
  }
93
96
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "suidouble",
3
- "version": "0.0.16",
3
+ "version": "0.0.17",
4
4
  "description": "Set of provider, package and object classes for javascript representation of Sui Move smart contracts. Use same code for publishing, upgrading, integration testing, interaction with smart contracts and integration in browser web3 dapps",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -0,0 +1,20 @@
1
+ # @generated by Move, please check-in and do not edit manually.
2
+
3
+ [move]
4
+ version = 0
5
+
6
+ dependencies = [
7
+ { name = "Sui" },
8
+ ]
9
+
10
+ [[move.package]]
11
+ name = "MoveStdlib"
12
+ source = { git = "https://github.com/MystenLabs/sui.git", rev = "testnet", subdir = "crates/sui-framework/packages/move-stdlib" }
13
+
14
+ [[move.package]]
15
+ name = "Sui"
16
+ source = { git = "https://github.com/MystenLabs/sui.git", rev = "testnet", subdir = "crates/sui-framework/packages/sui-framework" }
17
+
18
+ dependencies = [
19
+ { name = "MoveStdlib" },
20
+ ]
@@ -0,0 +1,10 @@
1
+ [package]
2
+ name = "suidouble_color"
3
+ version = "0.0.1"
4
+
5
+ [dependencies]
6
+ Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "testnet" }
7
+
8
+ [addresses]
9
+ suidouble_color = "0x0"
10
+ sui = "0000000000000000000000000000000000000000000000000000000000000002"
@@ -0,0 +1,150 @@
1
+
2
+ module suidouble_color::suidouble_color {
3
+ use sui::tx_context::{Self, sender, TxContext};
4
+ use std::string::{Self, utf8, String};
5
+ use sui::transfer;
6
+ use sui::object::{Self, UID, ID};
7
+ use std::vector::{Self, append, insert};
8
+
9
+ use sui::event::emit;
10
+
11
+ // The creator bundle: these two packages often go together.
12
+ use sui::package;
13
+ use sui::display;
14
+
15
+ /// Text size overflow.
16
+ const EInvalidColor: u64 = 0;
17
+
18
+ // ======== Events =========
19
+
20
+ /// Event. When a new color minted
21
+ struct ColorCreated has copy, drop { id: ID, r: u8, g: u8, b: u8 }
22
+
23
+ /// The Hero - an outstanding collection of digital art.
24
+ struct Color has key, store {
25
+ id: UID,
26
+ name: String,
27
+ r: u8,
28
+ g: u8,
29
+ b: u8,
30
+ img_url: String,
31
+ }
32
+
33
+ /// One-Time-Witness for the module.
34
+ struct SUIDOUBLE_COLOR has drop {}
35
+
36
+ /// In the module initializer we claim the `Publisher` object
37
+ /// to then create a `Display`. The `Display` is initialized with
38
+ /// a set of fields (but can be modified later) and published via
39
+ /// the `update_version` call.
40
+ ///
41
+ /// Keys and values are set in the initializer but could also be
42
+ /// set after publishing if a `Publisher` object was created.
43
+ fun init(otw: SUIDOUBLE_COLOR, ctx: &mut TxContext) {
44
+ let keys = vector[
45
+ utf8(b"name"),
46
+ utf8(b"link"),
47
+ utf8(b"image_url"),
48
+ utf8(b"description"),
49
+ utf8(b"project_url"),
50
+ utf8(b"creator"),
51
+ ];
52
+
53
+ let values = vector[
54
+ utf8(b"{name}"),
55
+ // For `link` we can build a URL using an `id` property
56
+ utf8(b"https://suidouble-color.herokuapp.com/color/{id}"),
57
+ utf8(b"{img_url}"),
58
+ // Description is static for all `Color` objects.
59
+ utf8(b"What a nice color. Isn't it?"),
60
+ // Project URL is usually static
61
+ utf8(b"https://suidouble-color.herokuapp.com/"),
62
+ // Creator field can be any
63
+ utf8(b"Jeka")
64
+ ];
65
+
66
+ // Claim the `Publisher` for the package!
67
+ let publisher = package::claim(otw, ctx);
68
+
69
+ // Get a new `Display` object for the `Color` type.
70
+ let display = display::new_with_fields<Color>(
71
+ &publisher, keys, values, ctx
72
+ );
73
+
74
+ // Commit first version of `Display` to apply changes.
75
+ display::update_version(&mut display);
76
+
77
+ transfer::public_transfer(publisher, sender(ctx));
78
+ transfer::public_transfer(display, sender(ctx));
79
+ }
80
+
81
+ /// Anyone can mint their `Color`!
82
+ public entry fun mint(name: String, r: u8, g: u8, b: u8, ctx: &mut TxContext) {
83
+ assert!(r >= 0 && r <= 255, EInvalidColor);
84
+ assert!(g >= 0 && g <= 255, EInvalidColor);
85
+ assert!(b >= 0 && b <= 255, EInvalidColor);
86
+
87
+ let id = object::new(ctx);
88
+
89
+ emit(ColorCreated { id: object::uid_to_inner(&id), r, g, b, });
90
+
91
+ // constructing the smallest (1x1) GIF with the color of RGB
92
+ let gif_start = vector<u8>[71, 73, 70, 56, 57, 97, 1, 0, 1, 0, 128, 1, 0];
93
+ let gif_end = vector<u8>[0, 0, 0, 33, 249, 4, 1, 10, 0, 1, 0, 44, 0, 0, 0, 0, 1, 0, 1, 0, 0, 2, 2, 68, 1, 0, 59];
94
+
95
+ insert(&mut gif_start, r, 13); // appending R
96
+ insert(&mut gif_start, g, 14); // appending G
97
+ insert(&mut gif_start, b, 15); // appending B
98
+ append(&mut gif_start, gif_end);
99
+
100
+ let img_url = encode(&gif_start);
101
+
102
+ let base_prefix = b"data:image/gif;base64,";
103
+ let as_string = utf8(base_prefix);
104
+ string::append(&mut as_string, utf8(img_url));
105
+
106
+ //
107
+ let color = Color { id, name, img_url: as_string, r: r, g: g, b: b };
108
+
109
+ transfer::transfer(color, tx_context::sender(ctx));
110
+ }
111
+
112
+ // thanks to: https://github.com/movefuns/movefuns/blob/dd1f4443c6bf0bc761b27e28fb6ba00f10636840/stdlib/sources/base64.move#L2
113
+ const TABLE: vector<u8> = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
114
+
115
+ public fun encode(str: &vector<u8>): vector<u8> {
116
+ if (vector::is_empty(str)) {
117
+ return vector::empty<u8>()
118
+ };
119
+ let size = vector::length(str);
120
+ let eq: u8 = 61;
121
+ let res = vector::empty<u8>();
122
+
123
+ let m = 0 ;
124
+ while (m < size ) {
125
+ vector::push_back(&mut res, *vector::borrow(&TABLE, (((*vector::borrow(str, m) & 0xfc) >> 2) as u64)));
126
+ if ( m + 3 >= size) {
127
+ if ( size % 3 == 1) {
128
+ vector::push_back(&mut res, *vector::borrow(&TABLE, (((*vector::borrow(str, m) & 0x03) << 4) as u64)));
129
+ vector::push_back(&mut res, eq);
130
+ vector::push_back(&mut res, eq);
131
+ }else if (size % 3 == 2) {
132
+ vector::push_back(&mut res, *vector::borrow(&TABLE, ((((*vector::borrow(str, m) & 0x03) << 4) + ((*vector::borrow(str, m + 1) & 0xf0) >> 4)) as u64)));
133
+ vector::push_back(&mut res, *vector::borrow(&TABLE, (((*vector::borrow(str, m + 1) & 0x0f) << 2) as u64)));
134
+ vector::push_back(&mut res, eq);
135
+ }else {
136
+ vector::push_back(&mut res, *vector::borrow(&TABLE, ((((*vector::borrow(str, m) & 0x03) << 4) + ((*vector::borrow(str, m + 1) & 0xf0) >> 4)) as u64)));
137
+ vector::push_back(&mut res, *vector::borrow(&TABLE, ((((*vector::borrow(str, m + 1) & 0x0f) << 2) + ((*vector::borrow(str, m + 2) & 0xc0) >> 6)) as u64)));
138
+ vector::push_back(&mut res, *vector::borrow(&TABLE, ((*vector::borrow(str, m + 2) & 0x3f) as u64)));
139
+ };
140
+ }else {
141
+ vector::push_back(&mut res, *vector::borrow(&TABLE, ((((*vector::borrow(str, m) & 0x03) << 4) + ((*vector::borrow(str, m + 1) & 0xf0) >> 4)) as u64)));
142
+ vector::push_back(&mut res, *vector::borrow(&TABLE, ((((*vector::borrow(str, m + 1) & 0x0f) << 2) + ((*vector::borrow(str, m + 2) & 0xc0) >> 6)) as u64)));
143
+ vector::push_back(&mut res, *vector::borrow(&TABLE, ((*vector::borrow(str, m + 2) & 0x3f) as u64)));
144
+ };
145
+ m = m + 3;
146
+ };
147
+
148
+ return res
149
+ }
150
+ }