2024-05-08 13:16:23 +02:00
|
|
|
import nacl from '../../deps/nacl-fast'
|
|
|
|
import utils from '../../deps/utils'
|
|
|
|
import Base58 from '../../deps/Base58'
|
2022-01-10 20:53:13 -08:00
|
|
|
|
|
|
|
const signArbitrary = (arbitraryBytesBase58, arbitraryBytesForSigningBase58, nonce, keyPair) => {
|
2023-01-13 09:52:14 +01:00
|
|
|
if (!arbitraryBytesBase58) {
|
|
|
|
throw new Error('ArbitraryBytesBase58 not defined')
|
|
|
|
}
|
2022-01-10 20:53:13 -08:00
|
|
|
|
2023-01-13 09:52:14 +01:00
|
|
|
if (!nonce) {
|
|
|
|
throw new Error('Nonce not defined')
|
|
|
|
}
|
2022-01-10 20:53:13 -08:00
|
|
|
|
2023-01-13 09:52:14 +01:00
|
|
|
if (!keyPair) {
|
|
|
|
throw new Error('keyPair not defined')
|
|
|
|
}
|
2022-01-10 20:53:13 -08:00
|
|
|
|
2023-01-13 09:52:14 +01:00
|
|
|
const arbitraryBytes = Base58.decode(arbitraryBytesBase58)
|
|
|
|
const _arbitraryBytesBuffer = Object.keys(arbitraryBytes).map(function (key) { return arbitraryBytes[key]; })
|
|
|
|
const arbitraryBytesBuffer = new Uint8Array(_arbitraryBytesBuffer)
|
|
|
|
const arbitraryBytesForSigning = Base58.decode(arbitraryBytesForSigningBase58)
|
|
|
|
const _arbitraryBytesForSigningBuffer = Object.keys(arbitraryBytesForSigning).map(function (key) { return arbitraryBytesForSigning[key]; })
|
|
|
|
const arbitraryBytesForSigningBuffer = new Uint8Array(_arbitraryBytesForSigningBuffer)
|
|
|
|
const _nonce = utils.int32ToBytes(nonce)
|
2024-05-08 13:16:23 +02:00
|
|
|
|
2023-01-13 09:52:14 +01:00
|
|
|
arbitraryBytesBuffer.set(_nonce, 112)
|
|
|
|
arbitraryBytesForSigningBuffer.set(_nonce, 112)
|
2022-01-10 20:53:13 -08:00
|
|
|
|
2023-01-13 09:52:14 +01:00
|
|
|
const signature = nacl.sign.detached(arbitraryBytesForSigningBuffer, keyPair.privateKey)
|
2022-01-10 20:53:13 -08:00
|
|
|
|
2024-03-29 09:00:10 +01:00
|
|
|
return utils.appendBuffer(arbitraryBytesBuffer, signature)
|
2022-01-10 20:53:13 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default signArbitrary
|