-
Notifications
You must be signed in to change notification settings - Fork 435
Expand file tree
/
Copy pathUtils.mo
More file actions
90 lines (81 loc) · 3.1 KB
/
Copy pathUtils.mo
File metadata and controls
90 lines (81 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import Result "mo:core/Result";
import Nat8 "mo:core/Nat8";
import Runtime "mo:core/Runtime";
import Char "mo:core/Char";
import Text "mo:core/Text";
import Iter "mo:core/Iter";
import Array "mo:core/Array";
import IC "mo:ic/Types";
module {
type Result<Ok, Err> = Result.Result<Ok, Err>;
type SchnorrAux = IC.SchnorrAux;
/// Returns the value of the result and traps if there isn't any value to return.
public func get_ok<T>(result : Result<T, Text>) : T {
switch result {
case (#ok value) value;
case (#err error) Runtime.trap("pattern failed: " # error);
};
};
/// Returns the value of the result and traps with a custom message if there isn't any value to return.
public func get_ok_expect<T>(result : Result<T, Text>, expect : Text) : T {
switch result {
case (#ok value) value;
case (#err error) {
Runtime.trap(expect # " pattern failed: " # error);
};
};
};
/// Unwraps the value of the option.
public func unwrap<T>(option : ?T) : T {
switch option {
case (?value) value;
case null Runtime.unreachable();
};
};
// Returns the hexadecimal representation of a `Nat8` considered as a `Nat4`.
func nat4ToText(nat4 : Nat8) : Text {
(
switch nat4 {
case 0 '0';
case 1 '1';
case 2 '2';
case 3 '3';
case 4 '4';
case 5 '5';
case 6 '6';
case 7 '7';
case 8 '8';
case 9 '9';
case 10 'a';
case 11 'b';
case 12 'c';
case 13 'd';
case 14 'e';
case 15 'f';
case _ Runtime.unreachable();
}
).toText();
};
/// Returns the hexadecimal representation of a `Nat8`.
func nat8ToText(byte : Nat8) : Text {
let leftNat4 = byte >> 4;
let rightNat4 = byte & 15;
nat4ToText(leftNat4) # nat4ToText(rightNat4);
};
/// Returns the hexadecimal representation of a byte array.
public func bytesToText(bytes : [Nat8]) : Text {
bytes.values().map(func(n : Nat8) : Text { nat8ToText(n) }).join("");
};
/// Mock ECDSA signer that returns a 64-byte placeholder signature.
/// Used only during fee estimation to get the correct transaction size
/// before invoking the real (expensive) threshold signing operation.
public func mock_sign_with_ecdsa(_key_name : Text, _derivation_path : [Blob], _message_hash : Blob) : async Blob {
Array.repeat(255 : Nat8, 64).toBlob();
};
/// Mock Schnorr signer that returns a 64-byte placeholder signature.
/// Used only during fee estimation to get the correct transaction size
/// before invoking the real (expensive) threshold signing operation.
public func mock_sign_with_schnorr(_key_name : Text, _derivation_path : [Blob], _message_hash : Blob, _aux : ?SchnorrAux) : async Blob {
Array.repeat(255 : Nat8, 64).toBlob();
};
};