Skip to content

Commit

Permalink
Merge branch 'master' into tl/polish_basic_bitcoin
Browse files Browse the repository at this point in the history
  • Loading branch information
THLO committed Sep 25, 2023
2 parents 80749cb + a77af17 commit e3a5c6e
Show file tree
Hide file tree
Showing 16 changed files with 624 additions and 581 deletions.
8 changes: 7 additions & 1 deletion motoko/encrypted-notes-dapp-vetkd/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@ Please also see the [README of the original encrypted-notes-dapp](../encrypted-n
This example uses an [**insecure** implementation](../../rust/vetkd/src/system_api) of [the proposed vetKD system API](https://github.com/dfinity/interface-spec/pull/158) in a pre-compiled form via the [vetkd_system_api.wasm](./vetkd_system_api.wasm). **Do not use this in production or for sensitive data**! This example is solely provided **for demonstration purposes** to collect feedback on the mentioned vetKD system API.

## Manual local deployment
1. For **Motoko** deployment set environmental variable:
1. Choose which implementation to use by setting a respective environment variable. You can choose Motoko or Rust.

For **Motoko** deployment use
```sh
export BUILD_ENV=motoko
```
For **Rust** deployment use
```sh
export BUILD_ENV=rust
```
2. To generate `$BUILD_ENV`-specific files (i.e., Motoko or Rust) run:
```sh
sh ./pre_deploy.sh
Expand Down
6 changes: 1 addition & 5 deletions motoko/encrypted-notes-dapp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,7 @@ Follow the steps below to deploy this sample project.
cd examples/motoko/encrypted-notes-dapp
```

or

```
cd examples/rust/encrypted-notes-dapp
```
This project folder contains the files for both Motoko and Rust development.

### Step 2: Set an environmental variable reflecting which backend canister you'll be using:
For Motoko deployment run:
Expand Down
24 changes: 18 additions & 6 deletions motoko/send_http_get/src/send_http_get_backend/Types.mo
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Types {

public type Timestamp = Nat64;

//1. Type that describes the Request arguments for an HTTPS Outcall
public type Timestamp = Nat64;
//1. Type that describes the Request arguments for an HTTPS outcall
//See: https://internetcomputer.org/docs/current/references/ic-interface-spec/#ic-http_request
public type HttpRequestArgs = {
url : Text;
Expand Down Expand Up @@ -38,22 +38,34 @@ module Types {

//2.1 This type describes a function called "TransformRawResponse" used in line 14 above
//"If provided, the calling canister itself must export this function."
//In this minimal example for a GET request, we declare the type for completeness, but
//In this minimal example for a `GET` request, we declare the type for completeness, but
//we do not use this function. We will pass "null" to the HTTP request.
public type TransformRawResponseFunction = {
function : shared query TransformArgs -> async HttpResponsePayload;
context : Blob;
};

//2.2 This type describes the arguments the transform function needs
//2.2 These type describes the arguments the transform function needs
public type TransformArgs = {
response : HttpResponsePayload;
context : Blob;
};

public type CanisterHttpResponsePayload = {
status : Nat;
headers : [HttpHeader];
body : [Nat8];
};

public type TransformContext = {
function : shared query TransformArgs -> async HttpResponsePayload;
context : Blob;
};


//3. Declaring the IC management canister which we use to make the HTTPS outcall
public type IC = actor {
http_request : HttpRequestArgs -> async HttpResponsePayload;
};
}

}
40 changes: 35 additions & 5 deletions motoko/send_http_get/src/send_http_get_backend/main.mo
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import Types "Types";
actor {

//This method sends a GET request to a URL with a free API we can test.
//This method returns Coinbase data on the exchange rate between BTC and ICP
//for a certain day. The data will look like this:
//This method returns Coinbase data on the exchange rate between USD and ICP
//for a certain day.
//The API response looks like this:
// [
// [
Expand All @@ -28,6 +28,29 @@ actor {
// 243.5678 <-- volume of ICP traded
// ],
// ]

//function to transform the response
public query func transform(raw : Types.TransformArgs) : async Types.CanisterHttpResponsePayload {
let transformed : Types.CanisterHttpResponsePayload = {
status = raw.response.status;
body = raw.response.body;
headers = [
{
name = "Content-Security-Policy";
value = "default-src 'self'";
},
{ name = "Referrer-Policy"; value = "strict-origin" },
{ name = "Permissions-Policy"; value = "geolocation=(self)" },
{
name = "Strict-Transport-Security";
value = "max-age=63072000";
},
{ name = "X-Frame-Options"; value = "DENY" },
{ name = "X-Content-Type-Options"; value = "nosniff" },
];
};
transformed;
};

public func get_icp_usd_exchange() : async Text {

Expand All @@ -40,6 +63,7 @@ actor {
// 2.1 Setup the URL and its query parameters
let ONE_MINUTE : Nat64 = 60;
let start_timestamp : Types.Timestamp = 1682978460; //May 1, 2023 22:01:00 GMT
let end_timestamp : Types.Timestamp = 1682978520;//May 1, 2023 22:02:00 GMT
let host : Text = "api.pro.coinbase.com";
let url = "https://" # host # "/products/ICP-USD/candles?start=" # Nat64.toText(start_timestamp) # "&end=" # Nat64.toText(start_timestamp) # "&granularity=" # Nat64.toText(ONE_MINUTE);

Expand All @@ -49,14 +73,20 @@ actor {
{ name = "User-Agent"; value = "exchange_rate_canister" },
];

// 2.2.1 Transform context
let transform_context : Types.TransformContext = {
function = transform;
context = Blob.fromArray([]);
};

// 2.3 The HTTP request
let http_request : Types.HttpRequestArgs = {
url = url;
max_response_bytes = null; //optional for request
headers = request_headers;
body = null; //optional for request
method = #get;
transform = null; //optional for request
transform = ?transform_context;
};

//3. ADD CYCLES TO PAY FOR HTTP REQUEST
Expand All @@ -68,7 +98,7 @@ actor {
//The way Cycles.add() works is that it adds those cycles to the next asynchronous call
//"Function add(amount) indicates the additional amount of cycles to be transferred in the next remote call"
//See: https://internetcomputer.org/docs/current/references/ic-interface-spec/#ic-http_request
Cycles.add(220_131_200_000); //minimum cycles needed to pass the CI tests. Cycles needed will vary on many things size of http response, subnetc, etc...).
Cycles.add(230_949_972_000);

//4. MAKE HTTPS REQUEST AND WAIT FOR RESPONSE
//Since the cycles were added above, we can just call the IC management canister with HTTPS outcalls below
Expand All @@ -85,7 +115,7 @@ actor {
// body : [Nat8];
// };

//We need to decode that [Na8] array that is the body into readable text.
//We need to decode that [Nat8] array that is the body into readable text.
//To do this, we:
// 1. Convert the [Nat8] into a Blob
// 2. Use Blob.decodeUtf8() method to convert the Blob to a ?Text optional
Expand Down
2 changes: 1 addition & 1 deletion motoko/send_http_post/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ upgrade: build
.SILENT: test
test: install
dfx canister call send_http_post_backend send_http_post_request \
| grep 'success.*true' && echo 'PASS'
| grep 'Hello' && echo 'PASS'

.PHONY: clean
.SILENT: clean
Expand Down
20 changes: 15 additions & 5 deletions motoko/send_http_post/src/send_http_post_backend/Types.mo
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module Types {

public type Timestamp = Nat64;

//1. Type that describes the Request arguments for an HTTPS Outcall
//1. Type that describes the Request arguments for an HTTPS outcall
//See: https://internetcomputer.org/docs/current/references/ic-interface-spec/#ic-http_request
public type HttpRequestArgs = {
url : Text;
Expand Down Expand Up @@ -35,7 +33,6 @@ module Types {
//modify headers, etc. "
//See: https://internetcomputer.org/docs/current/references/ic-interface-spec/#ic-http_request


//2.1 This type describes a function called "TransformRawResponse" used in line 14 above
//"If provided, the calling canister itself must export this function."
//In this minimal example for a GET request, we declare the type for completeness, but
Expand All @@ -51,8 +48,21 @@ module Types {
context : Blob;
};

public type CanisterHttpResponsePayload = {
status : Nat;
headers : [HttpHeader];
body : [Nat8];
};

public type TransformContext = {
function : shared query TransformArgs -> async HttpResponsePayload;
context : Blob;
};


//3. Declaring the IC management canister which we use to make the HTTPS outcall
public type IC = actor {
http_request : HttpRequestArgs -> async HttpResponsePayload;
};
}

}
53 changes: 46 additions & 7 deletions motoko/send_http_post/src/send_http_post_backend/main.mo
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,40 @@ import Types "Types";

actor {

//function to transform the response
public query func transform(raw : Types.TransformArgs) : async Types.CanisterHttpResponsePayload {
let transformed : Types.CanisterHttpResponsePayload = {
status = raw.response.status;
body = raw.response.body;
headers = [
{
name = "Content-Security-Policy";
value = "default-src 'self'";
},
{
name = "Referrer-Policy";
value = "strict-origin"
},
{
name = "Permissions-Policy";
value = "geolocation=(self)" },
{
name = "Strict-Transport-Security";
value = "max-age=63072000";
},
{
name = "X-Frame-Options";
value = "DENY"
},
{
name = "X-Content-Type-Options";
value = "nosniff"
},
];
};
transformed;
};

//PULIC METHOD
//This method sends a POST request to a URL with a free API we can test.
public func send_http_post_request() : async Text {
Expand All @@ -22,8 +56,8 @@ actor {

// 2.1 Setup the URL and its query parameters
//This URL is used because it allows us to inspect the HTTP request sent from the canister
let host : Text = "en8d7aepyq2ko.x.pipedream.net";
let url = "https://en8d7aepyq2ko.x.pipedream.net/";
let host : Text = "putsreq.com";
let url = "https://putsreq.com/aL1QS5IbaQd4NTqN3a81"; //HTTP that accepts IPV6

// 2.2 prepare headers for the system http_request call

Expand All @@ -45,6 +79,12 @@ actor {
let request_body_as_nat8: [Nat8] = Blob.toArray(request_body_as_Blob); // e.g [34, 34,12, 0]


// 2.2.1 Transform context
let transform_context : Types.TransformContext = {
function = transform;
context = Blob.fromArray([]);
};

// 2.3 The HTTP request
let http_request : Types.HttpRequestArgs = {
url = url;
Expand All @@ -53,7 +93,7 @@ actor {
//note: type of `body` is ?[Nat8] so we pass it here as "?request_body_as_nat8" instead of "request_body_as_nat8"
body = ?request_body_as_nat8;
method = #post;
transform = null; //optional for request
transform = ?transform_context;
};

//3. ADD CYCLES TO PAY FOR HTTP REQUEST
Expand All @@ -63,7 +103,7 @@ actor {

//The way Cycles.add() works is that it adds those cycles to the next asynchronous call
//See: https://internetcomputer.org/docs/current/references/ic-interface-spec/#ic-http_request
Cycles.add(220_131_200_000); //minimum cycles needed to pass the CI tests. Cycles needed will vary on many things size of http response, subnetc, etc...).
Cycles.add(230_850_258_000);

//4. MAKE HTTPS REQUEST AND WAIT FOR RESPONSE
//Since the cycles were added above, we can just call the IC management canister with HTTPS outcalls below
Expand All @@ -86,14 +126,13 @@ actor {
// 2. Use Blob.decodeUtf8() method to convert the Blob to a ?Text optional
// 3. We use Motoko syntax "Let... else" to unwrap what is returned from Text.decodeUtf8()
let response_body: Blob = Blob.fromArray(http_response.body);
let decoded_text: Text = switch (Text.decodeUtf8(response_body)) {
let decoded_text: Text = switch (Text.decodeUtf8(response_body)) {
case (null) { "No value returned" };
case (?y) { y };
};

//6. RETURN RESPONSE OF THE BODY
let response_url: Text = "https://public.requestbin.com/r/en8d7aepyq2ko/";
let result: Text = decoded_text # ". See more info of the request sent at at: " # response_url;
let result: Text = decoded_text # ". See more info of the request sent at: " # url # "/inspect";
result
};

Expand Down
11 changes: 7 additions & 4 deletions rust/basic_bitcoin/src/basic_bitcoin/src/bitcoin_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,13 +279,16 @@ fn sha256(data: &[u8]) -> Vec<u8> {
hasher.update(data);
hasher.finalize().to_vec()
}
fn ripemd160(data: &[u8]) -> Vec<u8> {
let mut hasher = ripemd::Ripemd160::new();
hasher.update(data);
hasher.finalize().to_vec()
}

// Converts a public key to a P2PKH address.
fn public_key_to_p2pkh_address(network: BitcoinNetwork, public_key: &[u8]) -> String {
// sha256 + ripmd160
let mut hasher = ripemd::Ripemd160::new();
hasher.update(sha256(public_key));
let result = hasher.finalize();
// SHA-256 & RIPEMD-160
let result = ripemd160(&sha256(public_key));

let prefix = match network {
BitcoinNetwork::Testnet | BitcoinNetwork::Regtest => 0x6f,
Expand Down
Loading

0 comments on commit e3a5c6e

Please sign in to comment.