Skip to content

Commit

Permalink
merge with dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Henrique Nogara committed Mar 14, 2022
2 parents 874d77d + 34bb77a commit 35d9f5a
Show file tree
Hide file tree
Showing 200 changed files with 5,810 additions and 2,203 deletions.
6 changes: 5 additions & 1 deletion .github/actions/publish/publish-wasm/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ runs:
override: true
target: wasm32-unknown-unknown

# Download a pre-compiled wasm-bindgen binary.
- name: Install wasm-bindgen-cli
uses: jetli/wasm-bindgen-action@24ba6f9fff570246106ac3f80f35185600c3f6c9

- name: Set up Node.js
uses: actions/setup-node@v2
with:
Expand All @@ -40,4 +44,4 @@ runs:
NODE_AUTH_TOKEN: ${{ inputs.npm-token }}
# will publish 'latest' tag if no tag is passed
run: npm publish $(if [ ${{ inputs.tag }} != '' ]; then echo --tag ${{ inputs.tag }}; fi) --access public
working-directory: bindings/wasm
working-directory: bindings/wasm
20 changes: 12 additions & 8 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ jobs:
sccache-path: ${{ matrix.sccache-path }}

- name: Setup sccache
uses: './.github/actions/rust/sccache/setup-sccache'
with:
uses: './.github/actions/rust/sccache/setup-sccache'
with:
os: ${{matrix.os}}

- name: Build
uses: actions-rs/cargo@v1
with:
Expand All @@ -119,7 +119,7 @@ jobs:
- name: Stop sccache
uses: './.github/actions/rust/sccache/stop-sccache'
with:
with:
os: ${{matrix.os}}

build-and-test-libjose:
Expand Down Expand Up @@ -187,17 +187,21 @@ jobs:
with:
toolchain: stable
target: wasm32-unknown-unknown

- name: Setup sccache
uses: './.github/actions/rust/sccache/setup-sccache'
with:
uses: './.github/actions/rust/sccache/setup-sccache'
with:
os: ${{matrix.os}}

- name: Set up Node.js
uses: actions/setup-node@v1
with:
node-version: 16.x

# Download a pre-compiled wasm-bindgen binary.
- name: Install wasm-bindgen-cli
uses: jetli/wasm-bindgen-action@24ba6f9fff570246106ac3f80f35185600c3f6c9

- name: Install JS dependencies
run: npm install
working-directory: bindings/wasm
Expand All @@ -216,5 +220,5 @@ jobs:

- name: Stop sccache
uses: './.github/actions/rust/sccache/stop-sccache'
with:
with:
os: ${{matrix.os}}
4 changes: 4 additions & 0 deletions .github/workflows/clippy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ jobs:
override: true
components: clippy

# Download a pre-compiled wasm-bindgen binary.
- name: Install wasm-bindgen-cli
uses: jetli/wasm-bindgen-action@24ba6f9fff570246106ac3f80f35185600c3f6c9

- name: core clippy check
uses: actions-rs/clippy-check@v1
with:
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ jobs:
override: true
components: llvm-tools-preview

# Download a pre-compiled wasm-bindgen binary.
- name: Install wasm-bindgen-cli
uses: jetli/wasm-bindgen-action@24ba6f9fff570246106ac3f80f35185600c3f6c9

- name: Install cargo-binutils
uses: actions-rs/[email protected]
with:
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ members = [
"identity",
"identity-comm",
"identity-account",
"identity-account-storage",
"identity-core",
"identity-credential",
"identity-did",
"identity-diff",
"identity-iota",
"identity-iota-core",

"examples",
]
Expand Down
4 changes: 3 additions & 1 deletion bindings/stronghold-nodejs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ publish = false
crate-type = ["cdylib"]

[dependencies]
identity = { version = "=0.5.0-dev.4", path = "../../identity", default-features = false, features = ["account", "stronghold", "async", "send-sync-storage"] }
identity-account-storage = { version = "=0.5.0-dev.4", path = "../../identity-account-storage", default-features = false, features = ["stronghold", "send-sync-storage"] }
identity-core = { version = "=0.5.0-dev.4", path = "../../identity-core", default-features = false }
identity-iota-core = { version = "=0.5.0-dev.4", path = "../../identity-iota-core", default-features = false }
napi = { version = "2.0.3", default-features = false, features = ["napi4", "tokio_rt", "serde-json"] }
napi-derive = { version = "2.0.7" }
serde = { version = "1.0", default-features = false, features = ["derive"] }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2020-2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use identity::account::ChainState;
use identity_account_storage::identity::ChainState;
use napi::Result;
use napi_derive::napi;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2020-2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use identity::account::IdentityState;
use identity_account_storage::identity::IdentityState;
use napi::Result;
use napi_derive::napi;

Expand Down
12 changes: 6 additions & 6 deletions bindings/stronghold-nodejs/src/account/storage/stronghold.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright 2020-2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use identity::account::Storage;
use identity::account::Stronghold;
use identity::core::decode_b58;
use identity::core::encode_b58;
use identity::crypto::PrivateKey;
use identity::crypto::PublicKey;
use identity_account_storage::storage::Storage;
use identity_account_storage::storage::Stronghold;
use identity_core::crypto::PrivateKey;
use identity_core::crypto::PublicKey;
use identity_core::utils::decode_b58;
use identity_core::utils::encode_b58;
use napi::bindgen_prelude::Error;
use napi::Result;
use napi_derive::napi;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2020-2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use identity::account::KeyLocation;
use identity_account_storage::types::KeyLocation;
use napi::Result;
use napi_derive::napi;

Expand Down
2 changes: 1 addition & 1 deletion bindings/stronghold-nodejs/src/account/types/signature.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2020-2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use identity::account::Signature;
use identity_account_storage::types::Signature;
use napi::Result;
use napi_derive::napi;

Expand Down
2 changes: 1 addition & 1 deletion bindings/stronghold-nodejs/src/did/napi_did.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2020-2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use identity::iota::IotaDID;
use identity_iota_core::did::IotaDID;
use napi::Result;
use napi_derive::napi;

Expand Down
8 changes: 4 additions & 4 deletions bindings/stronghold-nodejs/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright 2020-2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use identity::account::Result as AccountResult;
use identity::core::Result as CoreResult;
use identity_account_storage::Result as AccountStorageResult;
use identity_core::Result as CoreResult;
use napi::bindgen_prelude::Error;
use napi::Result;
use serde_json::Result as SerdeResult;
Expand All @@ -12,9 +12,9 @@ pub trait NapiResult<T> {
fn napi_result(self) -> Result<T>;
}

impl<T> NapiResult<T> for AccountResult<T> {
impl<T> NapiResult<T> for AccountStorageResult<T> {
fn napi_result(self) -> Result<T> {
self.map_err(|account_error| Error::from_reason(account_error.to_string()))
self.map_err(|account_storage_error| Error::from_reason(account_storage_error.to_string()))
}
}

Expand Down
70 changes: 38 additions & 32 deletions bindings/wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,20 @@ npm install @iota/identity-wasm@dev

## Build

Alternatively, you can build the bindings if you have Rust installed. If not, refer to [rustup.rs](https://rustup.rs) for the installation. Then install the necessary dependencies using:
Alternatively, you can build the bindings if you have Rust installed. If not, refer to [rustup.rs](https://rustup.rs) for the installation.

Install [`wasm-bindgen-cli`](https://github.com/rustwasm/wasm-bindgen). A manual installation is required because we use the [Weak References](https://rustwasm.github.io/wasm-bindgen/reference/weak-references.html) feature, which [`wasm-pack` does not expose](https://github.com/rustwasm/wasm-pack/issues/930).

```bash
cargo install --force wasm-bindgen-cli
```

Then, install the necessary dependencies using:
```bash
npm install
```

and then build the bindings for `node.js` with
and build the bindings for `node.js` with

```bash
npm run build:nodejs
Expand Down Expand Up @@ -56,36 +64,34 @@ cat \
```javascript
const identity = require('@iota/identity-wasm/node')

// Choose the Tangle network to publish on.
const network = identity.Network.mainnet();
// const network = identity.Network.devnet();

// Generate a new Ed25519 KeyPair.
const key = new identity.KeyPair(identity.KeyType.Ed25519);

// Create a new DID Document using the KeyPair for the default VerificationMethod.
const doc = new identity.Document(key, network.name);

// Sign the DID Document with the private key.
doc.signSelf(key, doc.defaultSigningMethod().id);

// Create a default client instance for the network.
const config = identity.Config.fromNetwork(network);
const client = identity.Client.fromConfig(config);

// Publish the DID Document to the IOTA Tangle
// The message can be viewed at https://explorer.iota.org/<mainnet|devnet>/transaction/<messageId>
client.publishDocument(doc)
.then((receipt) => {
const explorer = identity.ExplorerUrl.mainnet();
// const explorer = identity.ExplorerUrl.devnet(); // if using the devnet
console.log("Tangle Message Receipt:", receipt);
console.log("Tangle Exporer Url:", explorer.resolverUrl(doc.id));
})
.catch((error) => {
console.error("Error: ", error);
throw error
})
async function main() {
// Choose the Tangle network to publish on.
const network = identity.Network.mainnet();
// const network = identity.Network.devnet();

// Generate a new Ed25519 KeyPair.
const key = new identity.KeyPair(identity.KeyType.Ed25519);

// Create a new DID Document using the KeyPair for the default VerificationMethod.
const doc = new identity.Document(key, network.name);

// Sign the DID Document with the private key.
doc.signSelf(key, doc.defaultSigningMethod().id);

// Create a default client instance for the network.
const client = await identity.Client.fromConfig({network: network});

// Publish the DID Document to the IOTA Tangle.
const receipt = await client.publishDocument(doc);

// The message can be viewed at https://explorer.iota.org/<mainnet|devnet>/identity-resolver/<did>
const explorer = identity.ExplorerUrl.mainnet();
// const explorer = identity.ExplorerUrl.devnet(); // if using the devnet
console.log("Tangle Message Receipt:", receipt);
console.log("Tangle Explorer Url:", explorer.resolverUrl(doc.id));
}

main()
```

## Web Setup
Expand Down
60 changes: 52 additions & 8 deletions bindings/wasm/build/lints.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,57 @@

// Aborts the build process if disallowed occurences are found in identity_wasm.js
/** Aborts the build process if disallowed occurrences are found in identity_wasm.js **/
function lintBigInt(content) {
if (content.includes("BigInt64Array") || content.includes("BigUint64Array")) {
console.error("Build artifacts should not include BigInt64Array/BigUint64Array imports")
console.error("to ensure React Native/WebKit compatibility.")
console.error("Remove any u64 and i64 occurrence from the public Wasm interface.")
console.error("See: https://github.com/iotaledger/identity.rs/issues/362")
process.exit(1)
throw(
"Build artifacts should not include BigInt64Array/BigUint64Array imports\n" +
"to ensure React Native/WebKit compatibility.\n" +
"Remove any u64 and i64 occurrence from the public Wasm interface.\n" +
"See: https://github.com/iotaledger/identity.rs/issues/362\n"
);
}
}

/**
* Rejects any `<obj>.ptr = 0;` occurrence, excluding `this.ptr = 0;` in `free()` implementations.
*
* Prevents generated code that nulls out Wasm pointers without de-registering the finalizer, since they cause
* runtime errors during automatic garbage collection from WasmRefCell thinking the instance is still borrowed.
*
* Functions which take owned parameters cause this situation; the solution is to borrow and clone the parameter
* instead.
**/
function lintPtrNullWithoutFree(content) {
// Find line numbers of offending code.
const lines = content.split(/\r?\n/);
const matches = lines.flatMap(function (line, number) {
if (/(?<!this).ptr = 0;/.test(line)) {
return [(number + 1) + " " + line.trim()];
} else {
return [];
}
});
if (matches.length > 0) {
throw(`ERROR: generated Javascript should not include 'obj.ptr = 0;'.
When weak references are enabled with '--weak-refs', WasmRefCell in wasm-bindgen causes
runtime errors from automatic garbage collection trying to free objects taken as owned parameters.
Matches:
${matches}
SUGGESTION: change any exported functions which take an owned parameter (excluding flat enums) to use a borrow instead.
See: https://github.com/rustwasm/wasm-bindgen/pull/2677`);
}
}

/** Runs all custom lints on the generated code. Exits the process immediately with code 1 if any fail. **/
function lintAll(content) {
try {
lintBigInt(content);
lintPtrNullWithoutFree(content);
} catch (err) {
console.error("Custom lint failed!");
console.error(err);
process.exit(1);
}
}

exports.lintBigInt = lintBigInt;
exports.lintAll = lintAll;
Loading

0 comments on commit 35d9f5a

Please sign in to comment.