Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CAIP 10 support (blockchainAccountId) #205

Merged
merged 6 commits into from
Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@
"@babel/preset-typescript": "7.14.5",
"@semantic-release/changelog": "5.0.1",
"@semantic-release/git": "9.0.0",
"@types/crypto-js": "^4.0.2",
"@types/elliptic": "6.4.12",
"@types/jest": "27.0.1",
"@types/ripemd160": "^2.0.0",
"@typescript-eslint/eslint-plugin": "4.28.0",
"@typescript-eslint/parser": "4.28.0",
"codecov": "3.8.2",
Expand Down Expand Up @@ -86,11 +86,11 @@
"@stablelib/xchacha20poly1305": "^1.0.1",
"bech32": "^2.0.0",
"canonicalize": "^1.0.5",
"crypto-js": "^4.1.1",
"did-resolver": "^3.1.1",
"elliptic": "^6.5.4",
"js-sha3": "^0.8.0",
"multiformats": "^9.4.8",
"ripemd160": "^2.0.2",
"uint8arrays": "^3.0.0"
},
"eslintIgnore": [
Expand Down
21 changes: 11 additions & 10 deletions src/blockchains/bip122.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import SHA256 from 'crypto-js/sha256'
import RIPEMD160 from 'crypto-js/ripemd160'
import enc from 'crypto-js/enc-hex'
import RIPEMD160 from 'ripemd160'
import { bytesToBase58 } from '../util'
import { sha256 } from '../Digest'

export const publicKeyToAddress = (publicKeyBuffer: string): string => {
const publicKeyHash = RIPEMD160(SHA256(enc.parse(publicKeyBuffer)))
const step1 = Buffer.from('00' + publicKeyHash.toString(enc), 'hex')
const step2 = SHA256(SHA256(enc.parse(step1.toString('hex'))))
const checksum = step2.toString(enc).substring(0, 8)
const step3 = step1.toString('hex') + checksum
return bytesToBase58(Uint8Array.from(Buffer.from(step3, 'hex')))
export const publicKeyToAddress = (publicKey: string): string => {
const publicKeyBuffer = Uint8Array.from(Buffer.from(publicKey, 'hex'))
const hash = new RIPEMD160().update(Buffer.from(sha256(publicKeyBuffer))).digest()
const step1 = Buffer.concat([Buffer.from('00', 'hex'), hash])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we please not use Buffer it's a nodejs only dependency!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should use Uint8Array instead!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The uint8arrays pkg is your friend.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Buffer error from ripemd160, and ripemd160 also use buffer.

ERROR in ./node_modules/ripemd160/index.js 2:13-37
Module not found: Error: Can't resolve 'buffer' in '/home/runner/work/did-jwt/did-jwt/node_modules/ripemd160'

So use crypto-js/ripemd160 package (or ripemd160.js) only, or looking for other way.

What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, thoughts @mirceanis ? Could crypto-js/ripemd160 cause issues somewhere bc it's js modules?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make and add new ripemd160.ts.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@daoauth I've been trying to test your changes in react-native (that's where most issues arise with new dependencies), and there are still issues with regards to the use of Buffer.
I suppose that they can be worked around by some rn-nodeify magic, but I would like to keep that option as a last resort, since it complicates all setups.

@oed's suggestion to use uint8arrays instead of Buffer refers to the complete replacement of the nodejs-specific Buffer API.

That being said, I'm not yet comfortable taking on the maintenance burden of a RIPEMD160 implementation.
Ideally there should be a Uint8Array friendly RIPEMD160 implementation we could use.
I haven't yet had time to try the variant with crypto-js/ripemd160; but I see there were some build issues with it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mirceanis How about DataView? Can I use it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow, that was fast :)
This new commit seems to work ok in my tests.
I'm still reluctant about taking on the RIPEMD160 implementation, but I'm also not aware of trustworthy alternatives.
@oed any insights appreciated ;)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const checksum = Buffer.from(sha256(sha256(step1)))
.toString('hex')
.substring(0, 8)
const step2 = step1.toString('hex') + checksum
return bytesToBase58(Uint8Array.from(Buffer.from(step2, 'hex')))
}
12 changes: 6 additions & 6 deletions src/blockchains/cosmos.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import SHA256 from 'crypto-js/sha256'
import RIPEMD160 from 'crypto-js/ripemd160'
import enc from 'crypto-js/enc-hex'
import RIPEMD160 from 'ripemd160'
import { sha256 } from '../Digest'
import { bech32 } from 'bech32'

export const publicKeyToAddress = (publicKeyBuffer: string, prefix: string): string => {
const hash = RIPEMD160(SHA256(enc.parse(publicKeyBuffer)))
const words = bech32.toWords(Buffer.from(hash.toString(), 'hex'))
export const publicKeyToAddress = (publicKey: string, prefix: string): string => {
const publicKeyBuffer = Uint8Array.from(Buffer.from(publicKey, 'hex'))
const hash = new RIPEMD160().update(Buffer.from(sha256(publicKeyBuffer))).digest()
const words = bech32.toWords(hash)
return bech32.encode(prefix, words).replace(prefix, '')
}
11 changes: 4 additions & 7 deletions src/blockchains/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@ import { publicKeyToAddress as bip122 } from './bip122'
import { publicKeyToAddress as cosmos } from './cosmos'
import { toEthereumAddress } from '../Digest'

export const verifyBlockchainAccountId = (
publicKeyBuffer: string,
blockchainAccountId: string | undefined
): boolean => {
export const verifyBlockchainAccountId = (publicKey: string, blockchainAccountId: string | undefined): boolean => {
if (blockchainAccountId) {
const chain = blockchainAccountId.split(':')
switch (chain[0]) {
case 'bip122':
chain[chain.length - 1] = bip122(publicKeyBuffer)
chain[chain.length - 1] = bip122(publicKey)
break
case 'cosmos':
chain[chain.length - 1] = cosmos(publicKeyBuffer, chain[1])
chain[chain.length - 1] = cosmos(publicKey, chain[1])
break
case 'eip155':
chain[chain.length - 1] = toEthereumAddress(publicKeyBuffer)
chain[chain.length - 1] = toEthereumAddress(publicKey)
break
default:
return false
Expand Down
38 changes: 26 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1825,11 +1825,6 @@
dependencies:
"@types/node" "*"

"@types/crypto-js@^4.0.2":
version "4.0.2"
resolved "https://registry.yarnpkg.com/@types/crypto-js/-/crypto-js-4.0.2.tgz#4524325a175bf819fec6e42560c389ce1fb92c97"
integrity sha512-sCVniU+h3GcGqxOmng11BRvf9TfN9yIs8KKjB8C8d75W69cpTfZG80gau9yTx5SxF3gvHGbJhdESzzvnjtf3Og==

"@types/[email protected]", "@types/elliptic@^6.4.9":
version "6.4.12"
resolved "https://registry.yarnpkg.com/@types/elliptic/-/elliptic-6.4.12.tgz#e8add831f9cc9a88d9d84b3733ff669b68eaa124"
Expand Down Expand Up @@ -1949,6 +1944,13 @@
resolved "https://registry.yarnpkg.com/@types/retry/-/retry-0.12.0.tgz#2b35eccfcee7d38cd72ad99232fbd58bffb3c84d"
integrity sha512-wWKOClTTiizcZhXnPY4wikVAwmdYHp8q6DmC+EJUzAMsycb7HB32Kh9RN4+0gExjmPmZSAQjgURXIGATPegAvA==

"@types/ripemd160@^2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@types/ripemd160/-/ripemd160-2.0.0.tgz#d33e49cf66edf4668828030d4aa80116bbf8ae81"
integrity sha512-LD6AO/+8cAa1ghXax9NG9iPDLPUEGB2WWPjd//04KYfXxTwHvlDEfL0NRjrM5z9XWBi6WbKw75Are0rDyn3PSA==
dependencies:
"@types/node" "*"

"@types/stack-utils@^2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.0.tgz#7036640b4e21cc2f259ae826ce843d277dad8cff"
Expand Down Expand Up @@ -3195,11 +3197,6 @@ cross-spawn@^7.0.0, cross-spawn@^7.0.2, cross-spawn@^7.0.3:
shebang-command "^2.0.0"
which "^2.0.1"

crypto-js@^4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/crypto-js/-/crypto-js-4.1.1.tgz#9e485bcf03521041bd85844786b83fb7619736cf"
integrity sha512-o2JlM7ydqd3Qk9CA0L4NL6mTzU2sdx96a+oOfPu8Mkl/PK51vSyoi8/rQ8NknZtk44vq15lmhAj9CIAGwgeWKw==

crypto-random-string@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/crypto-random-string/-/crypto-random-string-2.0.0.tgz#ef2a7a966ec11083388369baa02ebead229b30d5"
Expand Down Expand Up @@ -4429,6 +4426,15 @@ has@^1.0.0, has@^1.0.3:
dependencies:
function-bind "^1.1.1"

hash-base@^3.0.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/hash-base/-/hash-base-3.1.0.tgz#55c381d9e06e1d2997a883b4a3fddfe7f0d3af33"
integrity sha512-1nmYp/rhMDiE7AYkDw+lLwlAzz0AntGIe51F3RfFfEqyQ3feY2eI/NcwC6umIQVOASPMsWJLJScWKSSvzL9IVA==
dependencies:
inherits "^2.0.4"
readable-stream "^3.6.0"
safe-buffer "^5.2.0"

hash.js@^1.0.0, hash.js@^1.0.3:
version "1.1.7"
resolved "https://registry.yarnpkg.com/hash.js/-/hash.js-1.1.7.tgz#0babca538e8d4ee4a0f8988d68866537a003cf42"
Expand Down Expand Up @@ -7439,7 +7445,7 @@ read@1, read@^1.0.7, read@~1.0.1, read@~1.0.7:
dependencies:
mute-stream "~0.0.4"

readable-stream@3, readable-stream@^3.0.0:
readable-stream@3, readable-stream@^3.0.0, readable-stream@^3.6.0:
version "3.6.0"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-3.6.0.tgz#337bbda3adc0706bd3e024426a286d4b4b2c9198"
integrity sha512-BViHy7LKeTz4oNnkcLJ+lVSL6vpiFeX6/d3oSH8zCW7UxP2onchk+vTGB143xuFjHS3deTgkKoXXymXqymiIdA==
Expand Down Expand Up @@ -7658,6 +7664,14 @@ rimraf@^3.0.0, rimraf@^3.0.2:
dependencies:
glob "^7.1.3"

ripemd160@^2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/ripemd160/-/ripemd160-2.0.2.tgz#a1c1a6f624751577ba5d07914cbc92850585890c"
integrity sha512-ii4iagi25WusVoiC4B4lq7pbXfAp3D9v5CwfkY33vffw2+pkDjY1D8GaN7spsxvCSx8dkPqOZCEZyfxcmJG2IA==
dependencies:
hash-base "^3.0.0"
inherits "^2.0.1"

rollup-plugin-bundle-size@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/rollup-plugin-bundle-size/-/rollup-plugin-bundle-size-1.0.3.tgz#d245cd988486b4040279f9fd33f357f61673e90f"
Expand Down Expand Up @@ -7734,7 +7748,7 @@ sade@^1.7.4:
dependencies:
mri "^1.1.0"

safe-buffer@^5.0.1, safe-buffer@^5.1.0, safe-buffer@^5.1.2, safe-buffer@~5.2.0:
safe-buffer@^5.0.1, safe-buffer@^5.1.0, safe-buffer@^5.1.2, safe-buffer@^5.2.0, safe-buffer@~5.2.0:
version "5.2.1"
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.2.1.tgz#1eaf9fa9bdb1fdd4ec75f58f9cdb4e6b7827eec6"
integrity sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==
Expand Down