Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QoL #85

Merged
merged 9 commits into from
Sep 11, 2024
Merged

QoL #85

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion examples/empty.wit

This file was deleted.

4 changes: 2 additions & 2 deletions examples/escrow_with_delay.timeout.wit
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"transfer_or_timeout": {
"value": "Right([0xed, 0xb6, 0x86, 0x50, 0x94, 0x26, 0x0f, 0x85, 0x58, 0x72, 0x82, 0x33, 0xaa, 0xe0, 0x17, 0xdd, 0x09, 0x69, 0xa2, 0xaf, 0xe5, 0xf0, 0x8c, 0x28, 0x2e, 0x1a, 0xb6, 0x59, 0xbf, 0x24, 0x62, 0x68, 0x4c, 0x99, 0xa6, 0x4a, 0x2a, 0x57, 0x24, 0x63, 0x58, 0xa0, 0xd6, 0x32, 0x67, 0x17, 0x78, 0xd0, 0x16, 0xe6, 0xdf, 0x73, 0x81, 0x29, 0x3d, 0xd5, 0xbb, 0x9f, 0x09, 0x99, 0xd3, 0x86, 0x40, 0xd4])",
"type": "Either<[Option<[u8; 64]>; 3], [u8; 64]>"
"value": "Right(0xedb6865094260f8558728233aae017dd0969a2afe5f08c282e1ab659bf2462684c99a64a2a57246358a0d632671778d016e6df7381293dd5bb9f0999d38640d4)",
"type": "Either<[Option<Signature>; 3], Signature>"
}
}
8 changes: 4 additions & 4 deletions examples/hodl_vault.wit
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
"type": "u32"
},
"oracle_sig": {
"value": "[0x90, 0x23, 0x1b, 0x8d, 0xe9, 0x6a, 0x1f, 0x94, 0x0d, 0xdc, 0xf4, 0x06, 0xfe, 0x83, 0x89, 0x41, 0x7c, 0xa8, 0xfb, 0x0b, 0x03, 0x15, 0x16, 0x08, 0xe2, 0xf9, 0x4b, 0x31, 0xb4, 0x43, 0xa7, 0xe0, 0xd2, 0x6a, 0x12, 0xe4, 0x37, 0xdf, 0x69, 0x02, 0x8f, 0x09, 0x02, 0x7c, 0x37, 0xd5, 0xf6, 0x74, 0x2a, 0x10, 0xc1, 0xe8, 0x86, 0x40, 0x61, 0xd1, 0x19, 0xb8, 0xbb, 0xce, 0x96, 0x2d, 0x26, 0xd3]",
"type": "[u8; 64]"
"value": "0x90231b8de96a1f940ddcf406fe8389417ca8fb0b03151608e2f94b31b443a7e0d26a12e437df69028f09027c37d5f6742a10c1e8864061d119b8bbce962d26d3",
"type": "Signature"
},
"owner_sig": {
"value": "[0xf2, 0x34, 0x1f, 0x57, 0x1f, 0x06, 0x92, 0x16, 0xed, 0xfc, 0x72, 0x82, 0x2f, 0x60, 0x94, 0xb8, 0xec, 0x33, 0x9c, 0x2f, 0x72, 0xdc, 0x64, 0xae, 0xa0, 0xee, 0xd1, 0xe3, 0xd6, 0x0a, 0xbf, 0x45, 0x72, 0xfd, 0xd0, 0x46, 0x18, 0xe5, 0xb5, 0xbc, 0x67, 0x2c, 0xcd, 0x71, 0xcf, 0xaf, 0x12, 0x5b, 0x6c, 0x1b, 0x10, 0x1a, 0xec, 0xa3, 0xa7, 0xb9, 0x38, 0xfe, 0x83, 0x93, 0x2a, 0xb3, 0x87, 0x43]",
"type": "[u8; 64]"
"value": "0xf2341f571f069216edfc72822f6094b8ec339c2f72dc64aea0eed1e3d60abf4572fdd04618e5b5bc672ccd71cfaf125b6c1b101aeca3a7b938fe83932ab38743",
"type": "Signature"
}
}
4 changes: 2 additions & 2 deletions examples/htlc.complete.wit
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"complete_or_cancel": {
"value": "Left((0x0000000000000000000000000000000000000000000000000000000000000000, [0xf7, 0x4b, 0x3c, 0xa5, 0x74, 0x64, 0x7f, 0x85, 0x95, 0x62, 0x4b, 0x12, 0x93, 0x24, 0xaf, 0xa2, 0xf3, 0x8b, 0x59, 0x8a, 0x9c, 0x1c, 0x7c, 0xfc, 0x5f, 0x08, 0xa9, 0xc0, 0x36, 0xec, 0x5a, 0xcd, 0x3c, 0x0f, 0xbb, 0x9e, 0xd3, 0xda, 0xe5, 0xca, 0x23, 0xa0, 0xa6, 0x5a, 0x34, 0xb5, 0xd6, 0xcc, 0xcd, 0xd6, 0xba, 0x24, 0x89, 0x85, 0xd6, 0x04, 0x1f, 0x7b, 0x21, 0x26, 0x2b, 0x17, 0xaf, 0x6f]))",
"type": "Either<(u256, [u8; 64]), [u8; 64]>"
"value": "Left((0x0000000000000000000000000000000000000000000000000000000000000000, 0xf74b3ca574647f8595624b129324afa2f38b598a9c1c7cfc5f08a9c036ec5acd3c0fbb9ed3dae5ca23a0a65a34b5d6cccdd6ba248985d6041f7b21262b17af6f))",
"type": "Either<(u256, Signature), Signature>"
}
}
2 changes: 1 addition & 1 deletion examples/htlc.simf
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn sha2(string: u256) -> u256 {
jet::sha_256_ctx_8_finalize(hasher)
}

fn checksig(pk: u256, sig: Signature) {
fn checksig(pk: Pubkey, sig: Signature) {
let msg: u256 = jet::sig_all_hash();
jet::bip_0340_verify((pk, msg), sig);
}
Expand Down
4 changes: 2 additions & 2 deletions examples/last_will.inherit.wit
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"inherit_or_not": {
"value": "Left([0x75, 0x52, 0x01, 0xbb, 0x62, 0xb0, 0xa8, 0xb8, 0xd1, 0x8f, 0xd1, 0x2f, 0xc0, 0x29, 0x51, 0xea, 0x39, 0x98, 0xba, 0x42, 0xbf, 0xc6, 0x66, 0x4d, 0xaa, 0xf8, 0xa0, 0xd2, 0x29, 0x8c, 0xad, 0x43, 0xcd, 0xc2, 0x13, 0x58, 0xc7, 0xc8, 0x2f, 0x37, 0x65, 0x42, 0x75, 0xdc, 0x2f, 0xea, 0x8c, 0x85, 0x8a, 0xdb, 0xe9, 0x7b, 0xac, 0x92, 0x82, 0x8b, 0x49, 0x8a, 0x5a, 0x23, 0x70, 0x04, 0xdb, 0x6f])",
"type": "Either<[u8; 64], Either<[u8; 64], [u8; 64]>>"
"value": "Left(0x755201bb62b0a8b8d18fd12fc02951ea3998ba42bfc6664daaf8a0d2298cad43cdc21358c7c82f37654275dc2fea8c858adbe97bac92828b498a5a237004db6f)",
"type": "Either<Signature, Either<Signature, Signature>>"
}
}
4 changes: 2 additions & 2 deletions examples/p2ms.wit
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"maybe_sigs": {
"value": "[Some([0xf7, 0x4b, 0x3c, 0xa5, 0x74, 0x64, 0x7f, 0x85, 0x95, 0x62, 0x4b, 0x12, 0x93, 0x24, 0xaf, 0xa2, 0xf3, 0x8b, 0x59, 0x8a, 0x9c, 0x1c, 0x7c, 0xfc, 0x5f, 0x08, 0xa9, 0xc0, 0x36, 0xec, 0x5a, 0xcd, 0x3c, 0x0f, 0xbb, 0x9e, 0xd3, 0xda, 0xe5, 0xca, 0x23, 0xa0, 0xa6, 0x5a, 0x34, 0xb5, 0xd6, 0xcc, 0xcd, 0xd6, 0xba, 0x24, 0x89, 0x85, 0xd6, 0x04, 0x1f, 0x7b, 0x21, 0x26, 0x2b, 0x17, 0xaf, 0x6f]), None, Some([0x29, 0xdb, 0xea, 0xb5, 0x62, 0x8a, 0xe4, 0x72, 0xbc, 0xe3, 0xe0, 0x87, 0x28, 0xea, 0xd1, 0x99, 0x7e, 0xf7, 0x89, 0xd4, 0xf0, 0x4b, 0x5b, 0xe3, 0x9c, 0xc0, 0x8b, 0x36, 0x2d, 0xc2, 0x29, 0xf5, 0x53, 0xfd, 0x35, 0x3f, 0x8a, 0x0a, 0xcf, 0xfd, 0xfb, 0xdd, 0xd4, 0x71, 0xd1, 0x5a, 0x0d, 0xda, 0x3b, 0x30, 0x68, 0x42, 0x41, 0x6f, 0xf2, 0x46, 0xbc, 0x07, 0x46, 0x2e, 0x56, 0x67, 0xeb, 0x89])]",
"type": "[Option<[u8; 64]>; 3]"
"value": "[Some(0xf74b3ca574647f8595624b129324afa2f38b598a9c1c7cfc5f08a9c036ec5acd3c0fbb9ed3dae5ca23a0a65a34b5d6cccdd6ba248985d6041f7b21262b17af6f), None, Some(0x29dbeab5628ae472bce3e08728ead1997ef789d4f04b5be39cc08b362dc229f553fd353f8a0acffdfbddd471d15a0dda3b306842416ff246bc07462e5667eb89)]",
"type": "[Option<Signature>; 3]"
}
}
4 changes: 2 additions & 2 deletions examples/p2pk.simf
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
* https://docs.ivylang.org/bitcoin/language/ExampleContracts.html#lockwithpublickey
*/
fn main() {
let pk: u256 = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798; // 1 * G
let pk: Pubkey = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798; // 1 * G
let msg: u256 = jet::sig_all_hash();
let sig: [u8; 64] = witness("sig");
let sig: Signature = witness("sig");
jet::bip_0340_verify((pk, msg), sig)
}
4 changes: 2 additions & 2 deletions examples/p2pk.wit
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sig": {
"value": "[0xf7, 0x4b, 0x3c, 0xa5, 0x74, 0x64, 0x7f, 0x85, 0x95, 0x62, 0x4b, 0x12, 0x93, 0x24, 0xaf, 0xa2, 0xf3, 0x8b, 0x59, 0x8a, 0x9c, 0x1c, 0x7c, 0xfc, 0x5f, 0x08, 0xa9, 0xc0, 0x36, 0xec, 0x5a, 0xcd, 0x3c, 0x0f, 0xbb, 0x9e, 0xd3, 0xda, 0xe5, 0xca, 0x23, 0xa0, 0xa6, 0x5a, 0x34, 0xb5, 0xd6, 0xcc, 0xcd, 0xd6, 0xba, 0x24, 0x89, 0x85, 0xd6, 0x04, 0x1f, 0x7b, 0x21, 0x26, 0x2b, 0x17, 0xaf, 0x6f]",
"type": "[u8; 64]"
"value": "0xf74b3ca574647f8595624b129324afa2f38b598a9c1c7cfc5f08a9c036ec5acd3c0fbb9ed3dae5ca23a0a65a34b5d6cccdd6ba248985d6041f7b21262b17af6f",
"type": "Signature"
}
}
4 changes: 2 additions & 2 deletions examples/p2pkh.simf
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ fn sha2(string: u256) -> u256 {
}

fn main() {
let pk: u256 = witness("pk");
let pk: Pubkey = witness("pk");
let expected_pk_hash: u256 = 0x132f39a98c31baaddba6525f5d43f2954472097fa15265f45130bfdb70e51def; // sha2(1 * G)
let pk_hash: u256 = sha2(pk);
assert!(jet::eq_256(pk_hash, expected_pk_hash));

let msg: u256 = jet::sig_all_hash();
let sig: [u8; 64] = witness("sig");
let sig: Signature = witness("sig");
jet::bip_0340_verify((pk, msg), sig)
}
6 changes: 3 additions & 3 deletions examples/p2pkh.wit
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"pk": {
"value": "0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798",
"type": "u256"
"type": "Pubkey"
},
"sig": {
"value": "[0xf7, 0x4b, 0x3c, 0xa5, 0x74, 0x64, 0x7f, 0x85, 0x95, 0x62, 0x4b, 0x12, 0x93, 0x24, 0xaf, 0xa2, 0xf3, 0x8b, 0x59, 0x8a, 0x9c, 0x1c, 0x7c, 0xfc, 0x5f, 0x08, 0xa9, 0xc0, 0x36, 0xec, 0x5a, 0xcd, 0x3c, 0x0f, 0xbb, 0x9e, 0xd3, 0xda, 0xe5, 0xca, 0x23, 0xa0, 0xa6, 0x5a, 0x34, 0xb5, 0xd6, 0xcc, 0xcd, 0xd6, 0xba, 0x24, 0x89, 0x85, 0xd6, 0x04, 0x1f, 0x7b, 0x21, 0x26, 0x2b, 0x17, 0xaf, 0x6f]",
"type": "[u8; 64]"
"value": "0xf74b3ca574647f8595624b129324afa2f38b598a9c1c7cfc5f08a9c036ec5acd3c0fbb9ed3dae5ca23a0a65a34b5d6cccdd6ba248985d6041f7b21262b17af6f",
"type": "Signature"
}
}
4 changes: 2 additions & 2 deletions examples/presigned_vault.complete.wit
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"hot_or_cold": {
"value": "Left([0xed, 0xb6, 0x86, 0x50, 0x94, 0x26, 0x0f, 0x85, 0x58, 0x72, 0x82, 0x33, 0xaa, 0xe0, 0x17, 0xdd, 0x09, 0x69, 0xa2, 0xaf, 0xe5, 0xf0, 0x8c, 0x28, 0x2e, 0x1a, 0xb6, 0x59, 0xbf, 0x24, 0x62, 0x68, 0x4c, 0x99, 0xa6, 0x4a, 0x2a, 0x57, 0x24, 0x63, 0x58, 0xa0, 0xd6, 0x32, 0x67, 0x17, 0x78, 0xd0, 0x16, 0xe6, 0xdf, 0x73, 0x81, 0x29, 0x3d, 0xd5, 0xbb, 0x9f, 0x09, 0x99, 0xd3, 0x86, 0x40, 0xd4])",
"type": "Either<[u8; 64], [u8; 64]>"
"value": "Left(0xedb6865094260f8558728233aae017dd0969a2afe5f08c282e1ab659bf2462684c99a64a2a57246358a0d632671778d016e6df7381293dd5bb9f0999d38640d4)",
"type": "Either<Signature, Signature>"
}
}
6 changes: 3 additions & 3 deletions examples/presigned_vault.simf
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@
*
* https://docs.ivylang.org/bitcoin/language/ExampleContracts.html#vaultspend
*/
fn checksig(pk: u256, sig: Signature) {
fn checksig(pk: Pubkey, sig: Signature) {
let msg: u256 = jet::sig_all_hash();
jet::bip_0340_verify((pk, msg), sig);
}

fn complete_spend(hot_sig: Signature) {
let timeout: Distance = 1000;
jet::check_lock_distance(timeout);
let hot_pk: u256 = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798; // 1 * G
let hot_pk: Pubkey = 0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798; // 1 * G
checksig(hot_pk, hot_sig);
}

fn cancel_spend(cold_sig: Signature) {
let cold_pk: u256 = 0xc6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5; // 2 * G
let cold_pk: Pubkey = 0xc6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5; // 2 * G
checksig(cold_pk, cold_sig)
}

Expand Down
4 changes: 2 additions & 2 deletions examples/sighash_all_anyprevoutanyscript.simf
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn main() {
// Message
let msg: u256 = jet::sha_256_ctx_8_finalize(ctx);

let pk: u256 = 0xf9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9;
let sig: [u8; 64] = witness("sig");
let pk: Pubkey = 0xf9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9;
let sig: Signature = witness("sig");
jet::bip_0340_verify((pk, msg), sig);
}
4 changes: 2 additions & 2 deletions examples/sighash_all_anyprevoutanyscript.wit
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sig": {
"value": "[0xc9, 0x39, 0xe3, 0x23, 0x44, 0x64, 0x2e, 0x3c, 0x22, 0x8b, 0x01, 0x62, 0x04, 0x18, 0x24, 0xaf, 0xe1, 0x94, 0x29, 0x74, 0x77, 0x18, 0x82, 0x55, 0xd2, 0x63, 0x79, 0x08, 0xa0, 0xd0, 0x0d, 0x1f, 0xdf, 0x59, 0x21, 0x4a, 0x30, 0x4d, 0xdf, 0x18, 0x0b, 0x1a, 0x07, 0x01, 0x9a, 0x60, 0xd8, 0x08, 0xd1, 0x81, 0x70, 0xfb, 0x77, 0xfd, 0x3b, 0x00, 0xec, 0xa7, 0x66, 0x67, 0xd7, 0xaa, 0x26, 0x8e]",
"type": "[u8; 64]"
"value": "0xc939e32344642e3c228b0162041824afe194297477188255d2637908a0d00d1fdf59214a304ddf180b1a07019a60d808d18170fb77fd3b00eca76667d7aa268e",
"type": "Signature"
}
}
4 changes: 2 additions & 2 deletions examples/sighash_none.simf
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn main() {
// Message
let msg: u256 = jet::sha_256_ctx_8_finalize(ctx);

let pk: u256 = 0xf9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9;
let sig: [u8; 64] = witness("sig");
let pk: Pubkey = 0xf9308a019258c31049344f85f89d5229b531c845836f99b08601f113bce036f9;
let sig: Signature = witness("sig");
jet::bip_0340_verify((pk, msg), sig);
}
4 changes: 2 additions & 2 deletions examples/sighash_none.wit
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sig": {
"value": "[0x34, 0x61, 0x52, 0x58, 0x3d, 0x5b, 0x60, 0xb9, 0x72, 0xbb, 0x4c, 0x03, 0xab, 0x67, 0x2e, 0x33, 0x94, 0x31, 0x06, 0x0e, 0x2b, 0x09, 0xc4, 0x47, 0xab, 0x98, 0x3c, 0x65, 0xda, 0xbc, 0x70, 0xa4, 0x59, 0xf3, 0xbe, 0xca, 0x77, 0x88, 0xbf, 0xa5, 0xda, 0x22, 0x1c, 0xf9, 0x92, 0x27, 0xb6, 0x5b, 0x4a, 0xd3, 0x82, 0x1a, 0x20, 0x45, 0xc8, 0x47, 0xee, 0x56, 0xd4, 0x8d, 0xf2, 0x6a, 0xee, 0x9c]",
"type": "[u8; 64]"
"value": "0x346152583d5b60b972bb4c03ab672e339431060e2b09c447ab983c65dabc70a459f3beca7788bfa5da221cf99227b65b4ad3821a2045c847ee56d48df26aee9c",
"type": "Signature"
}
}
8 changes: 4 additions & 4 deletions examples/transfer_with_timeout.transfer.wit
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"sender_sig": {
"value": "[0xf7, 0x4b, 0x3c, 0xa5, 0x74, 0x64, 0x7f, 0x85, 0x95, 0x62, 0x4b, 0x12, 0x93, 0x24, 0xaf, 0xa2, 0xf3, 0x8b, 0x59, 0x8a, 0x9c, 0x1c, 0x7c, 0xfc, 0x5f, 0x08, 0xa9, 0xc0, 0x36, 0xec, 0x5a, 0xcd, 0x3c, 0x0f, 0xbb, 0x9e, 0xd3, 0xda, 0xe5, 0xca, 0x23, 0xa0, 0xa6, 0x5a, 0x34, 0xb5, 0xd6, 0xcc, 0xcd, 0xd6, 0xba, 0x24, 0x89, 0x85, 0xd6, 0x04, 0x1f, 0x7b, 0x21, 0x26, 0x2b, 0x17, 0xaf, 0x6f]",
"type": "[u8; 64]"
"value": "0xf74b3ca574647f8595624b129324afa2f38b598a9c1c7cfc5f08a9c036ec5acd3c0fbb9ed3dae5ca23a0a65a34b5d6cccdd6ba248985d6041f7b21262b17af6f",
"type": "Signature"
},
"transfer_or_timeout": {
"value": "Some([0xf9, 0x14, 0xc8, 0x8a, 0x1e, 0xe8, 0x8b, 0x4b, 0x7b, 0x8e, 0xb5, 0x60, 0x3b, 0x83, 0x5f, 0xa8, 0x79, 0x38, 0x6a, 0xf7, 0x96, 0x28, 0xaa, 0x94, 0x35, 0x22, 0x6f, 0x64, 0xbd, 0xd5, 0x43, 0x13, 0x79, 0x4b, 0x18, 0xe3, 0x04, 0x37, 0x23, 0x18, 0x97, 0xda, 0xb3, 0x86, 0x1d, 0x92, 0x43, 0x7e, 0x0e, 0xfe, 0x03, 0xa4, 0xbb, 0x17, 0xe6, 0x0e, 0x43, 0x69, 0xd1, 0x92, 0xb0, 0xc6, 0x1e, 0xcf])",
"type": "Option<[u8; 64]>"
"value": "Some(0xf914c88a1ee88b4b7b8eb5603b835fa879386af79628aa9435226f64bdd54313794b18e30437231897dab3861d92437e0efe03a4bb17e60e4369d192b0c61ecf)",
"type": "Option<Signature>"
}
}
28 changes: 12 additions & 16 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ impl AbstractSyntaxTree for SingleExpression {
parse::SingleExpressionInner::Decimal(decimal) => {
let ty = ty
.as_integer()
.ok_or(Error::TypeValueMismatch(ty.clone()))
.ok_or(Error::ExpressionUnexpectedType(ty.clone()))
.with_span(from)?;
UIntValue::parse_decimal(decimal, ty)
.with_span(from)
Expand All @@ -751,18 +751,14 @@ impl AbstractSyntaxTree for SingleExpression {
parse::SingleExpressionInner::Binary(bits) => {
let ty = ty
.as_integer()
.ok_or(Error::TypeValueMismatch(ty.clone()))
.ok_or(Error::ExpressionUnexpectedType(ty.clone()))
.with_span(from)?;
let value = UIntValue::parse_binary(bits, ty).with_span(from)?;
SingleExpressionInner::Constant(Value::from(value))
}
parse::SingleExpressionInner::Hexadecimal(bytes) => {
let ty = ty
.as_integer()
.ok_or(Error::TypeValueMismatch(ty.clone()))
.with_span(from)?;
let value = UIntValue::parse_hexadecimal(bytes, ty).with_span(from)?;
SingleExpressionInner::Constant(Value::from(value))
let value = Value::parse_hexadecimal(bytes, ty).with_span(from)?;
SingleExpressionInner::Constant(value)
}
parse::SingleExpressionInner::Witness(name) => {
scope
Expand Down Expand Up @@ -790,10 +786,10 @@ impl AbstractSyntaxTree for SingleExpression {
parse::SingleExpressionInner::Tuple(tuple) => {
let types = ty
.as_tuple()
.ok_or(Error::TypeValueMismatch(ty.clone()))
.ok_or(Error::ExpressionUnexpectedType(ty.clone()))
.with_span(from)?;
if tuple.len() != types.len() {
return Err(Error::TypeValueMismatch(ty.clone())).with_span(from);
return Err(Error::ExpressionUnexpectedType(ty.clone())).with_span(from);
}
tuple
.iter()
Expand All @@ -805,10 +801,10 @@ impl AbstractSyntaxTree for SingleExpression {
parse::SingleExpressionInner::Array(array) => {
let (el_ty, size) = ty
.as_array()
.ok_or(Error::TypeValueMismatch(ty.clone()))
.ok_or(Error::ExpressionUnexpectedType(ty.clone()))
.with_span(from)?;
if array.len() != size {
return Err(Error::TypeValueMismatch(ty.clone())).with_span(from);
return Err(Error::ExpressionUnexpectedType(ty.clone())).with_span(from);
}
array
.iter()
Expand All @@ -819,10 +815,10 @@ impl AbstractSyntaxTree for SingleExpression {
parse::SingleExpressionInner::List(list) => {
let (el_ty, bound) = ty
.as_list()
.ok_or(Error::TypeValueMismatch(ty.clone()))
.ok_or(Error::ExpressionUnexpectedType(ty.clone()))
.with_span(from)?;
if bound.get() <= list.len() {
return Err(Error::TypeValueMismatch(ty.clone())).with_span(from);
return Err(Error::ExpressionUnexpectedType(ty.clone())).with_span(from);
}
list.iter()
.map(|e| Expression::analyze(e, el_ty, scope))
Expand All @@ -832,7 +828,7 @@ impl AbstractSyntaxTree for SingleExpression {
parse::SingleExpressionInner::Either(either) => {
let (ty_l, ty_r) = ty
.as_either()
.ok_or(Error::TypeValueMismatch(ty.clone()))
.ok_or(Error::ExpressionUnexpectedType(ty.clone()))
.with_span(from)?;
match either {
Either::Left(parse_l) => Expression::analyze(parse_l, ty_l, scope)
Expand All @@ -847,7 +843,7 @@ impl AbstractSyntaxTree for SingleExpression {
parse::SingleExpressionInner::Option(maybe_parse) => {
let ty = ty
.as_option()
.ok_or(Error::TypeValueMismatch(ty.clone()))
.ok_or(Error::ExpressionUnexpectedType(ty.clone()))
.with_span(from)?;
match maybe_parse {
Some(parse) => {
Expand Down
16 changes: 8 additions & 8 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ impl From<pest::error::Error<Rule>> for RichError {
pub enum Error {
ListBoundPow2(usize),
BitStringPow2(usize),
HexStringPow2(usize),
HexStringLen(usize),
ForWhileWidthPow2(usize),
CannotParse(String),
Grammar(String),
Expand All @@ -269,7 +269,6 @@ pub enum Error {
// The compiler can only be this precise if it knows a type system at least as expressive as Simplicity's
CannotCompile(String),
JetDoesNotExist(JetName),
TypeValueMismatch(ResolvedType),
InvalidCast(ResolvedType, ResolvedType),
MainNoInputs,
MainNoOutput,
Expand All @@ -279,6 +278,7 @@ pub enum Error {
InvalidNumberOfArguments(usize, usize),
FunctionNotFoldable(FunctionName),
FunctionNotLoopable(FunctionName),
ExpressionUnexpectedType(ResolvedType),
ExpressionTypeMismatch(ResolvedType, ResolvedType),
ExpressionNotConstant,
IntegerOutOfBounds(UIntType),
Expand All @@ -303,9 +303,9 @@ impl fmt::Display for Error {
f,
"Expected a valid bit string length (1, 2, 4, 8, 16, 32, 64, 128, 256), found {len}"
),
Error::HexStringPow2(len) => write!(
Error::HexStringLen(len) => write!(
f,
"Expected a valid hex string length (2, 4, 8, 16, 32, 64), found {len}"
"Expected an even hex string length (0, 2, 4, 6, 8, ...), found {len}"
),
Error::ForWhileWidthPow2(bit_width) => write!(
f,
Expand All @@ -331,10 +331,6 @@ impl fmt::Display for Error {
f,
"Jet `{name}` does not exist"
),
Error::TypeValueMismatch(ty) => write!(
f,
"Value does not match the assigned type `{ty}`"
),
Error::InvalidCast(source, target) => write!(
f,
"Cannot cast values of type `{source}` as values of type `{target}`"
Expand Down Expand Up @@ -371,6 +367,10 @@ impl fmt::Display for Error {
f,
"Expected a signature like `fn {name}(accumulator: A, context: C, counter u{{1,2,4,8,16}}) -> Either<B, A>` for a for-while loop"
),
Error::ExpressionUnexpectedType(ty) => write!(
f,
"Expected expression of type `{ty}`; found something else"
),
Error::ExpressionTypeMismatch(expected, found) => write!(
f,
"Expected expression of type `{expected}`, found type `{found}`"
Expand Down
Loading
Loading