1414// You should have received a copy of the GNU General Public License
1515// along with the Provable SDK library. If not, see <https://www.gnu.org/licenses/>.
1616
17- use crate :: account:: { PrivateKey , Signature , ViewKey } ;
17+ use crate :: {
18+ Field ,
19+ Group ,
20+ Plaintext ,
21+ account:: { PrivateKey , Signature , ViewKey , compute_key:: ComputeKey } ,
22+ from_js_typed_array,
23+ from_wasm_object_array,
24+ js_array_from_fields,
25+ native:: { FieldNative , LiteralNative } ,
26+ to_bits_array_le,
27+ types:: native:: AddressNative ,
28+ } ;
29+ use snarkvm_console:: prelude:: { FromBits , FromBytes , FromFields , ToBits , ToBytes , ToFields } ;
1830
19- use crate :: { account:: compute_key:: ComputeKey , types:: native:: AddressNative } ;
2031use core:: { convert:: TryFrom , fmt, ops:: Deref , str:: FromStr } ;
21- use wasm_bindgen:: prelude:: * ;
32+ use js_sys:: { Array , Uint8Array } ;
33+ use wasm_bindgen:: { convert:: TryFromJsValue , prelude:: * } ;
2234
2335/// Public address of an Aleo account
2436#[ wasm_bindgen]
@@ -50,6 +62,79 @@ impl Address {
5062 compute_key. address ( )
5163 }
5264
65+ /// Get an address from a series of bytes.
66+ ///
67+ /// @param {Uint8Array} bytes A left endian byte array representing the address.
68+ ///
69+ /// @returns {Address} The address object.
70+ #[ wasm_bindgen( js_name = "fromBytesLe" ) ]
71+ pub fn from_bytes_le ( bytes : Uint8Array ) -> Result < Self , String > {
72+ let rust_bytes = bytes. to_vec ( ) ;
73+ let native = AddressNative :: from_bytes_le ( rust_bytes. as_slice ( ) ) . map_err ( |e| e. to_string ( ) ) ?;
74+ Ok ( Self ( native) )
75+ }
76+
77+ /// Get the left endian byte array representation of the address.
78+ #[ wasm_bindgen( js_name = "toBytesLe" ) ]
79+ pub fn to_bytes_le ( & self ) -> Result < Uint8Array , String > {
80+ let rust_bytes = self . 0 . to_bytes_le ( ) . map_err ( |e| e. to_string ( ) ) ?;
81+ Ok ( Uint8Array :: from ( rust_bytes. as_slice ( ) ) )
82+ }
83+
84+ /// Get an address from a series of bits represented as a boolean array.
85+ ///
86+ /// @param {Array} bits A left endian boolean array representing the bits of the address.
87+ ///
88+ /// @returns {Address} The address object.
89+ #[ wasm_bindgen( js_name = "fromBitsLe" ) ]
90+ pub fn from_bits_le ( bits : Array ) -> Result < Self , String > {
91+ let rust_bits = from_js_typed_array ! ( bits, as_bool, "boolean" ) ?;
92+ let native = AddressNative :: from_bits_le ( & rust_bits) . map_err ( |e| e. to_string ( ) ) ?;
93+ Ok ( Self ( native) )
94+ }
95+
96+ /// Get the left endian boolean array representation of the bits of the address.
97+ #[ wasm_bindgen( js_name = "toBitsLe" ) ]
98+ pub fn to_bits_le ( & self ) -> Array {
99+ to_bits_array_le ! ( self )
100+ }
101+
102+ /// Get an address object from an array of fields.
103+ ///
104+ /// @param {Array} fields An array of fields.
105+ ///
106+ /// @returns {Plaintext} The address object.
107+ #[ wasm_bindgen( js_name = "fromFields" ) ]
108+ pub fn from_fields ( fields : Array ) -> Result < Self , String > {
109+ let native_fields = from_wasm_object_array ! ( fields, Field ) ?;
110+ let native = AddressNative :: from_fields ( & native_fields) . map_err ( |e| e. to_string ( ) ) ?;
111+ Ok ( Self ( native) )
112+ }
113+
114+ /// Get the field array representation of the address.
115+ #[ wasm_bindgen( js_name = "toFields" ) ]
116+ pub fn to_fields ( & self ) -> Result < Array , String > {
117+ let native = self . 0 . clone ( ) ;
118+ let native_fields = native. to_fields ( ) . map_err ( |e| e. to_string ( ) ) ?;
119+ Ok ( js_array_from_fields ! ( & native_fields) )
120+ }
121+
122+ /// Get an address object from a group.
123+ ///
124+ /// @param {Group} group The group object.
125+ ///
126+ /// @returns {Address} The address object.
127+ #[ wasm_bindgen( js_name = "fromGroup" ) ]
128+ pub fn from_group ( group : Group ) -> Self {
129+ Self :: from ( group)
130+ }
131+
132+ /// Get the group representation of the address object.
133+ #[ wasm_bindgen( js_name = "toGroup" ) ]
134+ pub fn to_group ( & self ) -> Group {
135+ Group :: from ( self )
136+ }
137+
53138 /// Create an aleo address object from a string representation of an address
54139 ///
55140 /// @param {string} address String representation of an addressm
@@ -67,6 +152,12 @@ impl Address {
67152 self . 0 . to_string ( )
68153 }
69154
155+ /// Get the plaintext representation of the address.
156+ #[ wasm_bindgen( js_name = "toPlaintext" ) ]
157+ pub fn to_plaintext ( & self ) -> Plaintext {
158+ Plaintext :: from ( LiteralNative :: Address ( self . 0 ) )
159+ }
160+
70161 /// Verify a signature for a message signed by the address
71162 ///
72163 /// @param {Uint8Array} Byte array representing a message signed by the address
@@ -114,6 +205,31 @@ impl From<&Address> for AddressNative {
114205 }
115206}
116207
208+ impl From < AddressNative > for Group {
209+ fn from ( value : AddressNative ) -> Self {
210+ let vm_group = value. to_group ( ) ;
211+ Self :: from ( vm_group)
212+ }
213+ }
214+
215+ impl From < Address > for Group {
216+ fn from ( value : Address ) -> Self {
217+ Self :: from ( value. 0 )
218+ }
219+ }
220+
221+ impl From < & AddressNative > for Group {
222+ fn from ( value : & AddressNative ) -> Self {
223+ Self :: from ( value. clone ( ) )
224+ }
225+ }
226+
227+ impl From < & Address > for Group {
228+ fn from ( value : & Address ) -> Self {
229+ Self :: from ( value. 0 )
230+ }
231+ }
232+
117233impl FromStr for Address {
118234 type Err = anyhow:: Error ;
119235
0 commit comments