Skip to content

Commit

Permalink
[snarkyjs] - Add public_key_checked and Account_id.Digest helper, add…
Browse files Browse the repository at this point in the history
… to test script
  • Loading branch information
MartinMinkov committed Jul 19, 2022
1 parent a611d3e commit e8e36cc
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/lib/mina_base/account_id.ml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ module Digest = struct

let of_field = Fn.id

let to_field_unsafe = Fn.id

module Assert = struct
let equal : t -> t -> unit = Field.Assert.equal
end
Expand Down
2 changes: 2 additions & 0 deletions src/lib/mina_base/account_id.mli
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ module Digest : sig

val of_field : Pickles.Impls.Step.Field.t -> t

val to_field_unsafe : t -> Pickles.Impls.Step.Field.t

module Assert : sig
val equal : t -> t -> unit
end
Expand Down
17 changes: 9 additions & 8 deletions src/lib/snarky_js_bindings/lib/snarky_js_bindings_lib.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2430,6 +2430,12 @@ module Ledger = struct
let create_new_account_exn (t : L.t) account_id account =
L.create_new_account t account_id account |> Or_error.ok_exn

let public_key_checked (pk : public_key) :
Signature_lib.Public_key.Compressed.var =
let x = pk##.g##.x##.value in
let y = pk##.g##.y##.value in
Signature_lib.Public_key.compress_var (x, y) |> Impl.run_checked

let public_key (pk : public_key) : Signature_lib.Public_key.Compressed.t =
{ x = to_unchecked pk##.g##.x##.value
; is_odd = Bigint.(test_bit (of_field (to_unchecked pk##.g##.y##.value)) 0)
Expand All @@ -2452,8 +2458,7 @@ module Ledger = struct
|> Field.constant |> to_js_field

let account_id_checked pk token =
Mina_base.Account_id.Checked.create
(Signature_lib.Public_key.Compressed.var_of_t (public_key pk))
Mina_base.Account_id.Checked.create (public_key_checked pk)
(token_id_checked token)

let account_id pk token =
Expand Down Expand Up @@ -2864,16 +2869,12 @@ module Ledger = struct
|> Mina_base.Account_id.public_key
|> Signature_lib.Public_key.Compressed.to_string |> Js.string

let forest_digest_of_field_checked :
Field.t -> Parties.Digest.Forest.Checked.t =
Obj.magic

let custom_token_id_checked pk token =
Mina_base.Account_id.Checked.derive_token_id
~owner:
(Mina_base.Account_id.Checked.create
(Signature_lib.Public_key.Compressed.var_of_t (public_key pk))
(Mina_base.Account_id.Checked.create (public_key_checked pk)
(token_id_checked token) )
|> Mina_base.Account_id.Digest.Checked.to_field_unsafe |> to_js_field

let custom_token_id_unchecked pk token =
Mina_base.Account_id.derive_token_id
Expand Down
11 changes: 10 additions & 1 deletion src/lib/snarky_js_bindings/test_module/simple-zkapp-token.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
Permissions,
Party,
UInt64,
partiesToJson,
Ledger,
Token,
getDefaultTokenId,
Expand Down Expand Up @@ -130,6 +129,16 @@ console.log("---CUSTOM TOKEN", Ledger.fieldToBase58(customToken.id));
console.log(`---TOKEN SYMBOL ${Mina.getAccount(zkappAddress).tokenSymbol}`);
console.log("---TOKEN ACCOUNT1", tokenAccount1.toBase58());
console.log("---TOKEN ACCOUNT2", tokenAccount2.toBase58());
console.log(
"---CUSTOM TOKEN CHECKED",
Ledger.fieldToBase58(
Ledger.customTokenIdChecked(zkappAddress, getDefaultTokenId())
)
);
console.log(
"---CUSTOM TOKEN UNCHECKED",
Ledger.fieldToBase58(Ledger.customTokenId(zkappAddress, getDefaultTokenId()))
);

console.log("----------token minting----------");
tx = await Local.transaction(feePayer, () => {
Expand Down

0 comments on commit e8e36cc

Please sign in to comment.