-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add 'bytesToHex', 'HexBlob' and 'castHexBlob' utils
add 'fromHexBlob' to some existing OpaqueString types, simplify their tests
- Loading branch information
1 parent
d67debb
commit dc33f15
Showing
11 changed files
with
119 additions
and
55 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { HexBlob } from '../../Cardano/util'; | ||
|
||
export const bytesToHex = (bytes: Uint8Array): HexBlob => HexBlob(Buffer.from(bytes).toString('hex')); |
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,2 +1,3 @@ | ||
export * from './replaceNullsToUndefineds'; | ||
export * from './isNotNil'; | ||
export * from './bytesToHex'; |
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,55 +1,51 @@ | ||
/* eslint-disable max-len */ | ||
import { Cardano } from '../../../src'; | ||
|
||
jest.mock('../../../src/Cardano/util/primitives', () => { | ||
const actual = jest.requireActual('../../../src/Cardano/util/primitives'); | ||
return { | ||
Hash32ByteBase16: jest.fn().mockImplementation((...args) => actual.Hash32ByteBase16(...args)), | ||
typedHex: jest.fn().mockImplementation((...args) => actual.typedHex(...args)) | ||
}; | ||
}); | ||
|
||
describe('Cardano/types/Key', () => { | ||
it('Ed25519PublicKey() accepts a valid public key hex string and is implemented using util.typedHex', () => { | ||
it('Ed25519PublicKey() accepts a valid public key hex string', () => { | ||
expect(() => | ||
Cardano.Ed25519PublicKey('6199186adb51974690d7247d2646097d2c62763b767b528816fb7ed3f9f55d39') | ||
).not.toThrow(); | ||
expect(Cardano.util.typedHex).toBeCalledWith( | ||
'6199186adb51974690d7247d2646097d2c62763b767b528816fb7ed3f9f55d39', | ||
64 | ||
); | ||
expect(() => | ||
Cardano.Ed25519PublicKey.fromHexBlob( | ||
Cardano.util.HexBlob('6199186adb51974690d7247d2646097d2c62763b767b528816fb7ed3f9f55d39') | ||
) | ||
).not.toThrow(); | ||
}); | ||
|
||
it('Ed25519KeyHash() accepts a key hash hex string and is implemented using util.Hash32ByteBase16', () => { | ||
it('Ed25519KeyHash() accepts a key hash hex string', () => { | ||
expect(() => | ||
Cardano.Ed25519KeyHash('6199186adb51974690d7247d2646097d2c62763b767b528816fb7ed3f9f55d39') | ||
).not.toThrow(); | ||
expect(Cardano.util.Hash32ByteBase16).toBeCalledWith( | ||
'6199186adb51974690d7247d2646097d2c62763b767b528816fb7ed3f9f55d39' | ||
); | ||
}); | ||
|
||
it('Bip32PublicKey() accepts a valid public key hex string and is implemented using util.typedHex', () => { | ||
it('Bip32PublicKey() accepts a valid public key hex string', () => { | ||
expect(() => | ||
Cardano.Bip32PublicKey( | ||
'6199186adb51974690d7247d2646097d2c62763b767b528816fb7ed3f9f55d396199186adb51974690d7247d2646097d2c62763b767b528816fb7ed3f9f55d39' | ||
) | ||
).not.toThrow(); | ||
expect(Cardano.util.typedHex).toBeCalledWith( | ||
'6199186adb51974690d7247d2646097d2c62763b767b528816fb7ed3f9f55d396199186adb51974690d7247d2646097d2c62763b767b528816fb7ed3f9f55d39', | ||
128 | ||
); | ||
expect(() => | ||
Cardano.Bip32PublicKey.fromHexBlob( | ||
Cardano.util.HexBlob( | ||
'6199186adb51974690d7247d2646097d2c62763b767b528816fb7ed3f9f55d396199186adb51974690d7247d2646097d2c62763b767b528816fb7ed3f9f55d39' | ||
) | ||
) | ||
).not.toThrow(); | ||
}); | ||
|
||
it('Bip32PrivateKey() accepts a valid public key hex string and is implemented using util.typedHex', () => { | ||
it('Bip32PrivateKey() accepts a valid public key hex string', () => { | ||
expect(() => | ||
Cardano.Bip32PrivateKey( | ||
'6199186adb51974690d7247d2646097d2c62763b767b528816fb7ed3f9f55d36199186adb51974690d7247d2646097d2c62763b767b528816fb7ed3f9f55d3996199186adb51974690d7247d2646097d2c62763b767b528816fb7ed3f9f55d39' | ||
) | ||
).not.toThrow(); | ||
expect(Cardano.util.typedHex).toBeCalledWith( | ||
'6199186adb51974690d7247d2646097d2c62763b767b528816fb7ed3f9f55d36199186adb51974690d7247d2646097d2c62763b767b528816fb7ed3f9f55d3996199186adb51974690d7247d2646097d2c62763b767b528816fb7ed3f9f55d39', | ||
192 | ||
); | ||
expect(() => | ||
Cardano.Bip32PrivateKey.fromHexBlob( | ||
Cardano.util.HexBlob( | ||
'6199186adb51974690d7247d2646097d2c62763b767b528816fb7ed3f9f55d36199186adb51974690d7247d2646097d2c62763b767b528816fb7ed3f9f55d3996199186adb51974690d7247d2646097d2c62763b767b528816fb7ed3f9f55d39' | ||
) | ||
) | ||
).not.toThrow(); | ||
}); | ||
}); |
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,30 +1,20 @@ | ||
import { Ed25519Signature, TransactionId, util } from '../../../src/Cardano'; | ||
|
||
jest.mock('../../../src/Cardano/util/primitives', () => { | ||
const actual = jest.requireActual('../../../src/Cardano/util/primitives'); | ||
return { | ||
Hash32ByteBase16: jest.fn().mockImplementation((...args) => actual.Hash32ByteBase16(...args)), | ||
typedHex: jest.fn().mockImplementation((...args) => actual.typedHex(...args)) | ||
}; | ||
}); | ||
import { Ed25519Signature, TransactionId } from '../../../src/Cardano'; | ||
import { HexBlob } from '../../../src/Cardano/util'; | ||
|
||
describe('Cardano/types/Transaction', () => { | ||
it('TransactionId accepts a valid transaction hash hex string and is implemented with util.Hash32ByteBase16', () => { | ||
it('TransactionId accepts a valid transaction hash hex string', () => { | ||
expect(() => TransactionId('3e33018e8293d319ef5b3ac72366dd28006bd315b715f7e7cfcbd3004129b80d')).not.toThrow(); | ||
expect(util.Hash32ByteBase16).toBeCalledWith('3e33018e8293d319ef5b3ac72366dd28006bd315b715f7e7cfcbd3004129b80d'); | ||
expect(() => | ||
TransactionId.fromHexBlob(HexBlob('3e33018e8293d319ef5b3ac72366dd28006bd315b715f7e7cfcbd3004129b80d')) | ||
).not.toThrow(); | ||
}); | ||
|
||
it('Ed25519Signature() accepts a valid signature hex string and is implemented using util.typedHex', () => { | ||
it('Ed25519Signature() accepts a valid signature hex string', () => { | ||
expect(() => | ||
Ed25519Signature( | ||
// eslint-disable-next-line max-len | ||
'709f937c4ce152c81f8406c03279ff5a8556a12a8657e40a578eaaa6223d2e6a2fece39733429e3ec73a6c798561b5c2d47d82224d656b1d964cfe8b5fdffe09' | ||
) | ||
).not.toThrow(); | ||
expect(util.typedHex).toBeCalledWith( | ||
// eslint-disable-next-line max-len | ||
'709f937c4ce152c81f8406c03279ff5a8556a12a8657e40a578eaaa6223d2e6a2fece39733429e3ec73a6c798561b5c2d47d82224d656b1d964cfe8b5fdffe09', | ||
128 | ||
); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { util } from '../../src/util'; | ||
|
||
test('bytesToHex', () => { | ||
expect(util.bytesToHex(Buffer.from('abc'))).toBe('616263'); | ||
}); |