2022-08-08 15:33:16 +03:00
|
|
|
import encoding.base58
|
|
|
|
import encoding.hex
|
|
|
|
|
2022-10-20 22:14:33 +03:00
|
|
|
fn test_encode() {
|
2022-08-08 15:33:16 +03:00
|
|
|
for input, expected in {
|
|
|
|
'': ''
|
|
|
|
'6263': '2PMCRQ'
|
|
|
|
'hello world': 'StV1DL6CwTryKyV'
|
|
|
|
'\x00\x00hello world': '11StV1DL6CwTryKyV'
|
|
|
|
} {
|
|
|
|
output := base58.encode(input)
|
2022-11-15 16:53:13 +03:00
|
|
|
println('> input: `${input}` | ${input.bytes().hex()} | => output: `${output}`')
|
2022-08-08 15:33:16 +03:00
|
|
|
assert output == expected
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-20 22:14:33 +03:00
|
|
|
fn test_encode_int() {
|
2022-08-08 15:33:16 +03:00
|
|
|
for input, expected in {
|
|
|
|
0x6263: '8VG'
|
|
|
|
0x61: '2g'
|
|
|
|
0x626262: 'a3gV'
|
|
|
|
0x636363: 'aPEr'
|
|
|
|
} {
|
2022-10-20 22:14:33 +03:00
|
|
|
output := base58.encode_int(input)!
|
2022-11-15 16:53:13 +03:00
|
|
|
println('> input: 0x${input:x} | => output: `${output}`')
|
2022-08-08 15:33:16 +03:00
|
|
|
assert output == expected
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-20 22:14:33 +03:00
|
|
|
fn test_decode() {
|
2022-08-08 15:33:16 +03:00
|
|
|
for output, expected in {
|
|
|
|
'USm3fpXnKG5EUBx2ndxBDMPVciP5hGey2Jh4NDv6gmeo1LkMeiKrLJUUBk6Z': 'The quick brown fox jumps over the lazy dog.'
|
|
|
|
'11StV1DL6CwTryKyV': '\x00\x00hello world'
|
|
|
|
'2NEpo7TZRRrLZSi2U': 'Hello World!'
|
|
|
|
'14cxpo3MBCYYWCgF74SWTdcmxipnGUsPw3': hex.decode('0027b5891b01da2db74cde1689a97a2acbe23d5fb1c0205bf6')?.bytestr()
|
|
|
|
'3vQB7B6MrGQZaxCuFg4oh': hex.decode('68656c6c6f20776f726c64bc62d4b8')?.bytestr()
|
|
|
|
} {
|
2022-10-20 22:14:33 +03:00
|
|
|
input := base58.decode(output)!
|
2022-11-15 16:53:13 +03:00
|
|
|
println('> output: `${output}` | decoded input: `${input}` | bytes: ${input.bytes().hex()}')
|
2022-08-08 15:33:16 +03:00
|
|
|
assert input.bytes().hex() == expected.bytes().hex()
|
|
|
|
}
|
|
|
|
}
|