forked from stellar/soroban-example-dapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert.ts
136 lines (120 loc) · 3.53 KB
/
convert.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import BigNumber from 'bignumber.js';
import * as SorobanClient from 'soroban-client';
let xdr = SorobanClient.xdr;
export function scvalToBigNumber(scval: SorobanClient.xdr.ScVal | undefined): BigNumber {
switch (scval?.switch()) {
case undefined: {
return BigNumber(0);
}
case xdr.ScValType.scvU63(): {
const {high, low} = scval.u63();
return bigNumberFromBytes(false, high, low);
}
case xdr.ScValType.scvU32(): {
return BigNumber(scval.u32());
}
case xdr.ScValType.scvI32(): {
return BigNumber(scval.i32());
}
case xdr.ScValType.scvObject(): {
let obj = scval.obj()!;
switch (obj.switch()) {
case xdr.ScObjectType.scoU64(): {
const {high, low} = obj.u64();
return bigNumberFromBytes(false, high, low);
}
case xdr.ScObjectType.scoI64(): {
const {high, low} = obj.i64();
return bigNumberFromBytes(true, high, low);
}
case xdr.ScObjectType.scoU128(): {
const parts = obj.u128();
const a = parts.hi();
const b = parts.lo();
return bigNumberFromBytes(false, a.high, a.low, b.high, b.low);
}
case xdr.ScObjectType.scoI128(): {
const parts = obj.i128();
const a = parts.hi();
const b = parts.lo();
return bigNumberFromBytes(true, a.high, a.low, b.high, b.low);
}
default:
throw new Error(`Invalid type for scvalToBigNumber: ${obj.switch().name}`);
}
}
default: {
throw new Error(`Invalid type for scvalToBigNumber: ${scval?.switch().name}`);
}
};
}
function bigNumberFromBytes(signed: boolean, ...bytes: (string | number | bigint)[]): BigNumber {
let sign = 1;
if (signed && bytes[0] === 0x80) {
// top bit is set, negative number.
sign = -1;
bytes[0] &= 0x7f;
}
let b = BigInt(0);
for (let byte of bytes) {
b <<= BigInt(8);
b |= BigInt(byte);
}
return BigNumber(b.toString()).multipliedBy(sign);
}
export function bigNumberToI128(value: BigNumber): SorobanClient.xdr.ScVal {
const b: bigint = BigInt(value.toFixed(0));
const buf = bigintToBuf(b);
if (buf.length > 16) {
throw new Error("BigNumber overflows i128");
}
if (value.isNegative()) {
// Clear the top bit
buf[0] &= 0x7f;
}
// left-pad with zeros up to 16 bytes
let padded = Buffer.alloc(16);
buf.copy(padded, padded.length-buf.length);
console.debug({value: value.toString(), padded});
if (value.isNegative()) {
// Set the top bit
padded[0] |= 0x80;
}
const hi = new xdr.Uint64(
bigNumberFromBytes(false, ...padded.slice(4, 8)).toNumber(),
bigNumberFromBytes(false, ...padded.slice(0, 4)).toNumber()
);
const lo = new xdr.Uint64(
bigNumberFromBytes(false, ...padded.slice(12, 16)).toNumber(),
bigNumberFromBytes(false, ...padded.slice(8, 12)).toNumber()
);
return xdr.ScVal.scvObject(xdr.ScObject.scoI128(new xdr.Int128Parts({lo, hi})));
}
function bigintToBuf(bn: bigint): Buffer {
var hex = BigInt(bn).toString(16).replace(/^-/, '');
if (hex.length % 2) { hex = '0' + hex; }
var len = hex.length / 2;
var u8 = new Uint8Array(len);
var i = 0;
var j = 0;
while (i < len) {
u8[i] = parseInt(hex.slice(j, j+2), 16);
i += 1;
j += 2;
}
if (bn < BigInt(0)) {
// Set the top bit
u8[0] |= 0x80;
}
return Buffer.from(u8);
}
export function xdrUint64ToNumber(value: SorobanClient.xdr.Uint64): number {
let b = 0;
b |= value.high;
b <<= 8;
b |= value.low;
return b;
}
export function scvalToString(value: SorobanClient.xdr.ScVal): string | undefined {
return value.obj()?.bin().toString();
}