Skip to content

Commit 588c52c

Browse files
committed
fix confusing elided lifetimes
1 parent f144eca commit 588c52c

File tree

8 files changed

+86
-106
lines changed

8 files changed

+86
-106
lines changed
-95 KB
Binary file not shown.
-13.8 KB
Binary file not shown.

examples/stable/test/end_to_end/candid_rpc/ledger_canister/src/ledger_canister/index.ts

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1+
import { getCrc32 } from '@dfinity/principal';
12
import { call, IDL, Principal, query, update } from 'azle';
23
import {
34
AccountIdentifierByteBuf,
45
Archives,
5-
binaryAddressFromAddress,
66
Decimals,
77
GetBlocksArgs,
8-
hexAddressFromPrincipal,
98
Name,
109
QueryBlocksResponse,
1110
Result_6 as TransferResult,
@@ -15,6 +14,7 @@ import {
1514
TransferArgs,
1615
TransferFee
1716
} from 'azle/canisters/nns_icp_ledger/idl';
17+
import jsSHA from 'jssha';
1818

1919
export default class {
2020
@update(
@@ -162,3 +162,80 @@ function getIcpCanisterPrincipal(): string {
162162
throw new Error('process.env.ICP_CANISTER_PRINCIPAL is undefined');
163163
}
164164
}
165+
166+
function hexAddressFromPrincipal(
167+
principal: Principal,
168+
subaccount: number
169+
): string {
170+
return addressFromPrincipal(principal, subaccount);
171+
}
172+
173+
function binaryAddressFromAddress(address: string): Uint8Array {
174+
return Uint8Array.from(
175+
address.match(/.{1,2}/g)?.map((x) => parseInt(x, 16)) ?? []
176+
);
177+
}
178+
179+
function _binaryAddressFromPrincipal(
180+
principal: Principal,
181+
subaccount: number
182+
): Uint8Array {
183+
const address = addressFromPrincipal(principal, subaccount);
184+
return Uint8Array.from(
185+
address.match(/.{1,2}/g)?.map((x) => parseInt(x, 16)) ?? []
186+
);
187+
}
188+
189+
// https://github.com/Toniq-Labs/extendable-token/blob/86eabb7336ea259876be9be830fb69b03046ea14/motoko/util/AccountIdentifier.mo
190+
// https://github.com/Toniq-Labs/entrepot-app/blob/5004eb5cb3c98805b665d3fa24d95483ce2a8cda/src/ic/utils.js
191+
// Some functions below licensed as follows
192+
// MIT License
193+
194+
// Copyright (c) 2022 Toniq Labs
195+
196+
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
197+
198+
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
199+
200+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
201+
function addressFromPrincipal(
202+
principal: Principal,
203+
subaccount: number
204+
): string {
205+
const prefixBytes = new Uint8Array([
206+
10, 97, 99, 99, 111, 117, 110, 116, 45, 105, 100
207+
]); // \0xAaccount-id
208+
const principalBytes = principal.toUint8Array();
209+
const subaccountBytes = getSubAccountArray(subaccount);
210+
211+
const hash = new jsSHA('SHA-224', 'UINT8ARRAY')
212+
.update(
213+
Uint8Array.from([
214+
...prefixBytes,
215+
...principalBytes,
216+
...subaccountBytes
217+
])
218+
)
219+
.getHash('UINT8ARRAY');
220+
const checksum = to32Bits(getCrc32(hash));
221+
222+
return toHexString(new Uint8Array([...checksum, ...hash]));
223+
}
224+
225+
function getSubAccountArray(subaccount: number): number[] {
226+
return Array(28)
227+
.fill(0)
228+
.concat(to32Bits(subaccount ? subaccount : 0));
229+
}
230+
231+
function to32Bits(number: number): number[] {
232+
let b = new ArrayBuffer(4);
233+
new DataView(b).setUint32(0, number);
234+
return Array.from(new Uint8Array(b));
235+
}
236+
237+
function toHexString(byteArray: Uint8Array): string {
238+
return Array.from(byteArray, (byte) => {
239+
return `0${(byte & 0xff).toString(16)}`.slice(-2);
240+
}).join('');
241+
}

src/experimental/build/commands/build/wasm_binary/rust/experimental_canister_template/src/stable_b_tree_map.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{cell::RefCell, collections::BTreeMap};
22

3-
use ic_stable_structures::{storable::Bound, StableBTreeMap, Storable};
3+
use ic_stable_structures::{StableBTreeMap, Storable, storable::Bound};
44

55
use crate::Memory;
66

@@ -17,7 +17,7 @@ pub struct AzleStableBTreeMapKey {
1717
}
1818

1919
impl Storable for AzleStableBTreeMapKey {
20-
fn to_bytes(&self) -> std::borrow::Cow<[u8]> {
20+
fn to_bytes(&self) -> std::borrow::Cow<'_, [u8]> {
2121
std::borrow::Cow::Borrowed(&self.bytes)
2222
}
2323

@@ -36,7 +36,7 @@ pub struct AzleStableBTreeMapValue {
3636
}
3737

3838
impl Storable for AzleStableBTreeMapValue {
39-
fn to_bytes(&self) -> std::borrow::Cow<[u8]> {
39+
fn to_bytes(&self) -> std::borrow::Cow<'_, [u8]> {
4040
std::borrow::Cow::Borrowed(&self.bytes)
4141
}
4242

src/stable/build/commands/build/wasm_binary/rust/stable_canister_template/src/stable_b_tree_map.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use std::{borrow::Cow, cell::RefCell, collections::BTreeMap};
22

3-
use ic_stable_structures::{storable::Bound, StableBTreeMap, Storable};
3+
use ic_stable_structures::{StableBTreeMap, Storable, storable::Bound};
44
use rquickjs::{Ctx, Result};
55

6-
use crate::{ic::throw_error, Memory};
6+
use crate::{Memory, ic::throw_error};
77

88
#[allow(unused)]
99
pub type AzleStableBTreeMap =
@@ -19,7 +19,7 @@ pub struct AzleStableBTreeMapKey {
1919
}
2020

2121
impl Storable for AzleStableBTreeMapKey {
22-
fn to_bytes(&self) -> Cow<[u8]> {
22+
fn to_bytes(&self) -> Cow<'_, [u8]> {
2323
Cow::Borrowed(&self.bytes)
2424
}
2525

@@ -38,7 +38,7 @@ pub struct AzleStableBTreeMapValue {
3838
}
3939

4040
impl Storable for AzleStableBTreeMapValue {
41-
fn to_bytes(&self) -> Cow<[u8]> {
41+
fn to_bytes(&self) -> Cow<'_, [u8]> {
4242
Cow::Borrowed(&self.bytes)
4343
}
4444

src/stable/lib/canisters/nns_icp_ledger/idl/README.md

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,3 @@
77
2. Find the Candid on that page and copy it
88
3. Paste the Candid into `index.did`
99
4. From this directory run `npx azle generate index.did > index.ts`
10-
5. Add the following to the bottom of `index.ts`:
11-
12-
```typescript
13-
export {
14-
binaryAddressFromAddress,
15-
binaryAddressFromPrincipal,
16-
hexAddressFromPrincipal
17-
} from './address';
18-
```

src/stable/lib/canisters/nns_icp_ledger/idl/address/index.ts

Lines changed: 0 additions & 82 deletions
This file was deleted.

src/stable/lib/canisters/nns_icp_ledger/idl/index.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,9 +1012,3 @@ export const init: init = ({ IDL }) => {
10121012
});
10131013
return [LedgerCanisterPayload];
10141014
};
1015-
1016-
export {
1017-
binaryAddressFromAddress,
1018-
binaryAddressFromPrincipal,
1019-
hexAddressFromPrincipal
1020-
} from './address';

0 commit comments

Comments
 (0)