-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from sonicdex/development
Development
- Loading branch information
Showing
11 changed files
with
72 additions
and
138 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,20 +102,3 @@ | |
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
/** @preserve | ||
* Counter block mode compatible with Dr Brian Gladman fileenc.c | ||
* derived from CryptoJS.mode.CTR | ||
* Jan Hruby [email protected] | ||
*/ | ||
|
||
/** @preserve | ||
(c) 2012 by Cédric Mesnil. All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: | ||
|
||
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. | ||
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,103 +1,16 @@ | ||
import { Principal } from '@dfinity/principal'; | ||
import crc32 from 'buffer-crc32'; | ||
import { Buffer } from 'buffer'; | ||
import crypto from "crypto-js"; | ||
|
||
|
||
import { AccountIdentifier, SubAccount } from '@dfinity/ledger-icp'; | ||
// @ts-ignore | ||
window.Buffer = Buffer; | ||
const SUB_ACCOUNT_ZERO = Buffer.alloc(32); | ||
const ACCOUNT_DOMAIN_SEPERATOR = '\x0Aaccount-id'; | ||
|
||
const to32bits = (num) => { | ||
const b = new ArrayBuffer(4); | ||
new DataView(b).setUint32(0, num); | ||
return Array.from(new Uint8Array(b)); | ||
}; | ||
|
||
const getTokenIdentifier = (canisterId, tokenIndex) => { | ||
const padding = Buffer.from('\x0Atid'); | ||
const array = new Uint8Array([ | ||
...padding, | ||
...Principal.fromText(canisterId).toUint8Array(), | ||
...to32bits(tokenIndex), | ||
]); | ||
return Principal.fromUint8Array(array).toText(); | ||
}; | ||
|
||
const intToHex = (val) => | ||
val < 0 ? (Number(val) >>> 0).toString(16) : Number(val).toString(16); | ||
|
||
// We generate a CRC32 checksum, and trnasform it into a hexString | ||
const generateChecksum = (hash) => { | ||
const crc = crc32.unsigned(Buffer.from(hash)); | ||
const hex = intToHex(crc); | ||
return hex.padStart(8, '0'); | ||
}; | ||
|
||
const byteArrayToWordArray = (byteArray) => { | ||
const wordArray = []; | ||
let i; | ||
for (i = 0; i < byteArray.length; i += 1) { | ||
wordArray[(i / 4) | 0] |= byteArray[i] << (24 - 8 * i); | ||
} | ||
// eslint-disable-next-line | ||
const result = crypto.lib.WordArray.create(wordArray, byteArray.length); | ||
return result; | ||
}; | ||
|
||
const wordToByteArray = (word, length) => { | ||
const byteArray = []; | ||
const xFF = 0xff; | ||
if (length > 0) byteArray.push(word >>> 24); | ||
if (length > 1) byteArray.push((word >>> 16) & xFF); | ||
if (length > 2) byteArray.push((word >>> 8) & xFF); | ||
if (length > 3) byteArray.push(word & xFF); | ||
|
||
return byteArray; | ||
}; | ||
|
||
const wordArrayToByteArray = (wordArray, length) => { | ||
if ( | ||
wordArray.hasOwnProperty('sigBytes') && | ||
wordArray.hasOwnProperty('words') | ||
) { | ||
length = wordArray.sigBytes; | ||
wordArray = wordArray.words; | ||
} | ||
|
||
let result = []; | ||
let bytes; | ||
let i = 0; | ||
while (length > 0) { | ||
bytes = wordToByteArray(wordArray[i], Math.min(4, length)); | ||
length -= bytes.length; | ||
result = [...result, bytes]; | ||
i++; | ||
} | ||
return [].concat.apply([], result); | ||
}; | ||
|
||
const getAccountIdentifier = (principalId, subAccount='') => { | ||
const getAccountIdentifier = (principalId, subAccount = 0) => { | ||
try { | ||
var principal = Principal.from(principalId); | ||
const sha = crypto.algo.SHA224.create(); | ||
sha.update(ACCOUNT_DOMAIN_SEPERATOR); // Internally parsed with UTF-8, like go does | ||
sha.update(byteArrayToWordArray(principal.toUint8Array())); | ||
const subBuffer = Buffer.from(SUB_ACCOUNT_ZERO); | ||
if (subAccount) { | ||
subBuffer.writeUInt32BE(subAccount); | ||
} | ||
sha.update(byteArrayToWordArray(subBuffer)); | ||
const hash = sha.finalize(); | ||
const byteArray = wordArrayToByteArray(hash, 28); | ||
const checksum = generateChecksum(byteArray); | ||
const val = checksum + hash.toString(); | ||
return val; | ||
var accId = AccountIdentifier.fromPrincipal({ principal: Principal.from(principalId), subAccount: SubAccount.fromID(subAccount) }) | ||
return accId.toHex(); | ||
} catch (error) { | ||
console.log(error) | ||
return false | ||
} | ||
}; | ||
|
||
export { getTokenIdentifier, getAccountIdentifier } | ||
export { getAccountIdentifier } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,35 @@ | ||
import { Actor, HttpAgent } from '@dfinity/agent'; | ||
import { getAccountIdentifier } from '../libs/identifier-utils'; | ||
import { AuthClient } from "@dfinity/auth-client"; | ||
import { Signer } from '@dfinity/oisy-wallet-signer/signer'; | ||
import {IcpWallet} from '@dfinity/oisy-wallet-signer/icp-wallet'; | ||
|
||
import { IcpWallet } from '@dfinity/oisy-wallet-signer/icp-wallet'; | ||
|
||
export const oisyWallet = { | ||
readyState: "Loadable", url: "https://identity.ic0.app", | ||
authClient: false, | ||
connectWallet: async function (connectObj = { whitelist: [], host: '' }) { | ||
var self = this, returnData = {}; | ||
var self = this, returnData = {} | ||
return new Promise(async (resolve, reject) => { | ||
const wallet = await IcpWallet.connect({ | ||
url: 'https://staging.oisy.com/sign' | ||
try { | ||
const wallet = await IcpWallet.connect({ | ||
url: 'https://oisy.com/sign', host: connectObj.host, | ||
}); | ||
const { allPermissionsGranted } = await wallet.requestPermissionsNotGranted(); | ||
if (!allPermissionsGranted) { | ||
return; | ||
} | ||
const accounts = await wallet.accounts(); | ||
const account = accounts?.[0]; | ||
var sid = await getAccountIdentifier(account.owner); | ||
const signer = Signer.init({ owner: account.owner, host: connectObj.host }); | ||
|
||
}); | ||
|
||
console.log(wallet); | ||
self.getPrincipal = async function () { return account.owner } | ||
self.disConnectWallet = async function () { await wallet.disconnect() } | ||
// console.log( { accountId: sid, principalId: account.owner } ) | ||
const returnData = { accountId: sid, principalId: account.owner } | ||
resolve(returnData); | ||
} catch (error) { | ||
resolve(false); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters